|
| 1 | +// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -Xfrontend -disable-availability-checking -I %S/Inputs) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: reflection |
| 5 | + |
| 6 | +@_spi(Reflection) import Swift |
| 7 | +import SimpleStructs |
| 8 | +import StdlibUnittest |
| 9 | + |
| 10 | +func checkFieldsWithKeyPath<T>( |
| 11 | + of type: T.Type, |
| 12 | + options: _EachFieldOptions = [], |
| 13 | + fields: [String: PartialKeyPath<T>] |
| 14 | +) { |
| 15 | + var count = 0 |
| 16 | + |
| 17 | + _forEachFieldWithKeyPath(of: T.self, options: options) { |
| 18 | + charPtr, keyPath in |
| 19 | + count += 1 |
| 20 | + |
| 21 | + let fieldName = String(cString: charPtr) |
| 22 | + if fieldName == "" { |
| 23 | + expectTrue(false, "Empty field name") |
| 24 | + return true |
| 25 | + } |
| 26 | + guard let checkKeyPath = fields[fieldName] else { |
| 27 | + expectTrue(false, "Unexpected field '\(fieldName)'") |
| 28 | + return true |
| 29 | + } |
| 30 | + |
| 31 | + expectTrue(checkKeyPath == keyPath) |
| 32 | + return true |
| 33 | + } |
| 34 | + |
| 35 | + expectEqual(fields.count, count) |
| 36 | +} |
| 37 | + |
| 38 | +var ForEachFieldTestSuite = TestSuite("ForEachField") |
| 39 | + |
| 40 | +ForEachFieldTestSuite.test("HasPrivateFieldsOnly") { |
| 41 | + checkFieldsWithKeyPath( |
| 42 | + of: HasPrivateFieldsOnly.self, |
| 43 | + fields: [:] |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +ForEachFieldTestSuite.test("HasPublicFieldsOnly") { |
| 48 | + checkFieldsWithKeyPath( |
| 49 | + of: HasPublicFieldsOnly.self, |
| 50 | + fields: [ |
| 51 | + "publ1": \HasPublicFieldsOnly.publ1, |
| 52 | + "publ2": \HasPublicFieldsOnly.publ2 |
| 53 | + ] |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +ForEachFieldTestSuite.test("HasPrivatePublicProtectedFields") { |
| 58 | + checkFieldsWithKeyPath( |
| 59 | + of: HasPrivatePublicProtectedFields.self, |
| 60 | + fields: [ |
| 61 | + "publ1": \HasPrivatePublicProtectedFields.publ1, |
| 62 | + "publ2": \HasPrivatePublicProtectedFields.publ2 |
| 63 | + ] |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +ForEachFieldTestSuite.test("Outer") { |
| 68 | + checkFieldsWithKeyPath( |
| 69 | + of: Outer.self, |
| 70 | + fields: [ |
| 71 | + "publStruct": \Outer.publStruct |
| 72 | + ] |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +runAllTests() |
0 commit comments