1313// limitations under the License.
1414
1515@testable import FirebaseAILogic
16- import Testing
16+ import XCTest
1717
18- struct GenerableTests {
19- @Test
20- func initializeGenerableTypeFromModelOutput( ) throws {
18+ final class GenerableTests : XCTestCase {
19+ func testInitializeGenerableTypeFromModelOutput( ) throws {
2120 let addressProperties : [ ( String , any ConvertibleToModelOutput ) ] =
2221 [ ( " street " , " 123 Main St " ) , ( " city " , " Anytown " ) , ( " zipCode " , " 12345 " ) ]
2322 let addressModelOutput = ModelOutput (
@@ -31,84 +30,79 @@ struct GenerableTests {
3130
3231 let person = try Person ( modelOutput)
3332
34- #expect ( person. firstName == " John " )
35- #expect ( person. lastName == " Doe " )
36- #expect ( person. age == 40 )
37- #expect ( person. address. street == " 123 Main St " )
38- #expect ( person. address. city == " Anytown " )
39- #expect ( person. address. zipCode == " 12345 " )
33+ XCTAssertEqual ( person. firstName, " John " )
34+ XCTAssertEqual ( person. lastName, " Doe " )
35+ XCTAssertEqual ( person. age, 40 )
36+ XCTAssertEqual ( person. address. street, " 123 Main St " )
37+ XCTAssertEqual ( person. address. city, " Anytown " )
38+ XCTAssertEqual ( person. address. zipCode, " 12345 " )
4039 }
4140
42- @Test
43- func initializeGenerableWithMissingPropertyThrows( ) throws {
41+ func testInitializeGenerableWithMissingPropertyThrows( ) throws {
4442 let properties : [ ( String , any ConvertibleToModelOutput ) ] =
4543 [ ( " firstName " , " John " ) , ( " age " , 40 ) ]
4644 let modelOutput = ModelOutput (
4745 properties: properties, uniquingKeysWith: { _, second in second }
4846 )
4947
50- do {
51- _ = try Person ( modelOutput )
52- Issue . record ( " Did not throw an error. " )
53- } catch let GenerativeModel . GenerationError . decodingFailure ( context ) {
54- #expect ( context . debugDescription . contains ( " lastName " ) )
55- } catch {
56- Issue . record ( " Threw an unexpected error: \( error ) " )
48+ XCTAssertThrowsError ( try Person ( modelOutput ) ) { error in
49+ guard let error = error as? GenerativeModel . GenerationError ,
50+ case let . decodingFailure ( context ) = error else {
51+ XCTFail ( " Threw an unexpected error: \( error ) " )
52+ return
53+ }
54+ XCTAssertContains ( context . debugDescription , " lastName " )
5755 }
5856 }
5957
60- @Test
61- func initializeGenerableFromNonStructureThrows( ) throws {
58+ func testInitializeGenerableFromNonStructureThrows( ) throws {
6259 let modelOutput = ModelOutput ( " not a structure " )
6360
64- do {
65- _ = try Person ( modelOutput )
66- Issue . record ( " Did not throw an error. " )
67- } catch let GenerativeModel . GenerationError . decodingFailure ( context ) {
68- #expect ( context . debugDescription . contains ( " does not contain an object " ) )
69- } catch {
70- Issue . record ( " Threw an unexpected error: \( error ) " )
61+ XCTAssertThrowsError ( try Person ( modelOutput ) ) { error in
62+ guard let error = error as? GenerativeModel . GenerationError ,
63+ case let . decodingFailure ( context ) = error else {
64+ XCTFail ( " Threw an unexpected error: \( error ) " )
65+ return
66+ }
67+ XCTAssertContains ( context . debugDescription , " does not contain an object " )
7168 }
7269 }
7370
74- @Test
75- func initializeGenerableWithTypeMismatchThrows( ) throws {
71+ func testInitializeGenerableWithTypeMismatchThrows( ) throws {
7672 let properties : [ ( String , any ConvertibleToModelOutput ) ] =
7773 [ ( " firstName " , " John " ) , ( " lastName " , " Doe " ) , ( " age " , " forty " ) ]
7874 let modelOutput = ModelOutput (
7975 properties: properties, uniquingKeysWith: { _, second in second }
8076 )
8177
82- do {
83- _ = try Person ( modelOutput )
84- Issue . record ( " Did not throw an error. " )
85- } catch let GenerativeModel . GenerationError . decodingFailure ( context ) {
86- #expect ( context . debugDescription . contains ( " \" forty \" does not contain Int " ) )
87- } catch {
88- Issue . record ( " Threw an unexpected error: \( error ) " )
78+ XCTAssertThrowsError ( try Person ( modelOutput ) ) { error in
79+ guard let error = error as? GenerativeModel . GenerationError ,
80+ case let . decodingFailure ( context ) = error else {
81+ XCTFail ( " Threw an unexpected error: \( error ) " )
82+ return
83+ }
84+ XCTAssertContains ( context . debugDescription , " \" forty \" does not contain Int " )
8985 }
9086 }
9187
92- @Test
93- func initializeGenerableWithLossyNumericConversion( ) throws {
88+ func testInitializeGenerableWithLossyNumericConversion( ) throws {
9489 let properties : [ ( String , any ConvertibleToModelOutput ) ] =
9590 [ ( " firstName " , " John " ) , ( " lastName " , " Doe " ) , ( " age " , 40.5 ) ]
9691 let modelOutput = ModelOutput (
9792 properties: properties, uniquingKeysWith: { _, second in second }
9893 )
9994
100- do {
101- _ = try Person ( modelOutput )
102- Issue . record ( " Did not throw an error. " )
103- } catch let GenerativeModel . GenerationError . decodingFailure ( context ) {
104- #expect ( context . debugDescription . contains ( " 40.5 does not contain Int. " ) )
105- } catch {
106- Issue . record ( " Threw an unexpected error: \( error ) " )
95+ XCTAssertThrowsError ( try Person ( modelOutput ) ) { error in
96+ guard let error = error as? GenerativeModel . GenerationError ,
97+ case let . decodingFailure ( context ) = error else {
98+ XCTFail ( " Threw an unexpected error: \( error ) " )
99+ return
100+ }
101+ XCTAssertContains ( context . debugDescription , " 40.5 does not contain Int. " )
107102 }
108103 }
109104
110- @Test
111- func initializeGenerableWithExtraProperties( ) throws {
105+ func testInitializeGenerableWithExtraProperties( ) throws {
112106 let addressProperties : [ ( String , any ConvertibleToModelOutput ) ] =
113107 [ ( " street " , " 123 Main St " ) , ( " city " , " Anytown " ) , ( " zipCode " , " 12345 " ) ]
114108 let addressModelOutput = ModelOutput (
@@ -128,16 +122,15 @@ struct GenerableTests {
128122
129123 let person = try Person ( modelOutput)
130124
131- #expect ( person. firstName == " John " )
132- #expect ( person. lastName == " Doe " )
133- #expect ( person. age == 40 )
134- #expect ( person. address. street == " 123 Main St " )
135- #expect ( person. address. city == " Anytown " )
136- #expect ( person. address. zipCode == " 12345 " )
125+ XCTAssertEqual ( person. firstName, " John " )
126+ XCTAssertEqual ( person. lastName, " Doe " )
127+ XCTAssertEqual ( person. age, 40 )
128+ XCTAssertEqual ( person. address. street, " 123 Main St " )
129+ XCTAssertEqual ( person. address. city, " Anytown " )
130+ XCTAssertEqual ( person. address. zipCode, " 12345 " )
137131 }
138132
139- @Test
140- func initializeGenerableWithMissingOptionalProperty( ) throws {
133+ func testInitializeGenerableWithMissingOptionalProperty( ) throws {
141134 let addressProperties : [ ( String , any ConvertibleToModelOutput ) ] =
142135 [ ( " street " , " 123 Main St " ) , ( " city " , " Anytown " ) , ( " zipCode " , " 12345 " ) ]
143136 let addressModelOutput = ModelOutput (
@@ -151,17 +144,16 @@ struct GenerableTests {
151144
152145 let person = try Person ( modelOutput)
153146
154- #expect ( person. firstName == " John " )
155- #expect ( person. lastName == " Doe " )
156- #expect ( person. age == 40 )
157- #expect ( person. middleName == nil )
158- #expect ( person. address. street == " 123 Main St " )
159- #expect ( person. address. city == " Anytown " )
160- #expect ( person. address. zipCode == " 12345 " )
147+ XCTAssertEqual ( person. firstName, " John " )
148+ XCTAssertEqual ( person. lastName, " Doe " )
149+ XCTAssertEqual ( person. age, 40 )
150+ XCTAssertNil ( person. middleName)
151+ XCTAssertEqual ( person. address. street, " 123 Main St " )
152+ XCTAssertEqual ( person. address. city, " Anytown " )
153+ XCTAssertEqual ( person. address. zipCode, " 12345 " )
161154 }
162155
163- @Test
164- func convertGenerableTypeToModelOutput( ) throws {
156+ func testConvertGenerableTypeToModelOutput( ) throws {
165157 let address = Address ( street: " 456 Oak Ave " , city: " Someplace " , zipCode: " 54321 " )
166158 let person = Person (
167159 firstName: " Jane " ,
@@ -174,45 +166,44 @@ struct GenerableTests {
174166 let modelOutput = person. modelOutput
175167
176168 guard case let . structure( properties, orderedKeys) = modelOutput. kind else {
177- Issue . record ( " Model output is not a structure. " )
169+ XCTFail ( " Model output is not a structure. " )
178170 return
179171 }
180- let firstNameProperty = try #require ( properties [ " firstName " ] )
172+ let firstNameProperty = try XCTUnwrap ( properties [ " firstName " ] )
181173 guard case let . string( firstName) = firstNameProperty. kind else {
182- Issue . record ( " The 'firstName' property is not a string: \( firstNameProperty. kind) " )
174+ XCTFail ( " The 'firstName' property is not a string: \( firstNameProperty. kind) " )
183175 return
184176 }
185- #expect ( firstName == person. firstName)
186- #expect ( try modelOutput. value ( forProperty: " firstName " ) == person. firstName)
187- let middleNameProperty = try #require ( properties [ " middleName " ] )
177+ XCTAssertEqual ( firstName, person. firstName)
178+ XCTAssertEqual ( try modelOutput. value ( forProperty: " firstName " ) , person. firstName)
179+ let middleNameProperty = try XCTUnwrap ( properties [ " middleName " ] )
188180 guard case let . string( middleName) = middleNameProperty. kind else {
189- Issue . record ( " The 'middleName' property is not a string: \( middleNameProperty. kind) " )
181+ XCTFail ( " The 'middleName' property is not a string: \( middleNameProperty. kind) " )
190182 return
191183 }
192- #expect ( middleName == person. middleName)
193- #expect ( try modelOutput. value ( forProperty: " middleName " ) == person. middleName)
194- let lastNameProperty = try #require ( properties [ " lastName " ] )
184+ XCTAssertEqual ( middleName, person. middleName)
185+ XCTAssertEqual ( try modelOutput. value ( forProperty: " middleName " ) , person. middleName)
186+ let lastNameProperty = try XCTUnwrap ( properties [ " lastName " ] )
195187 guard case let . string( lastName) = lastNameProperty. kind else {
196- Issue . record ( " The 'lastName' property is not a string: \( lastNameProperty. kind) " )
188+ XCTFail ( " The 'lastName' property is not a string: \( lastNameProperty. kind) " )
197189 return
198190 }
199- #expect ( lastName == person. lastName)
200- #expect ( try modelOutput. value ( forProperty: " lastName " ) == person. lastName)
201- let ageProperty = try #require ( properties [ " age " ] )
191+ XCTAssertEqual ( lastName, person. lastName)
192+ XCTAssertEqual ( try modelOutput. value ( forProperty: " lastName " ) , person. lastName)
193+ let ageProperty = try XCTUnwrap ( properties [ " age " ] )
202194 guard case let . number( age) = ageProperty. kind else {
203- Issue . record ( " The 'age' property is not a number: \( ageProperty. kind) " )
195+ XCTFail ( " The 'age' property is not a number: \( ageProperty. kind) " )
204196 return
205197 }
206- #expect ( Int ( age) == person. age)
207- #expect ( try modelOutput. value ( forProperty: " age " ) == person. age)
198+ XCTAssertEqual ( Int ( age) , person. age)
199+ XCTAssertEqual ( try modelOutput. value ( forProperty: " age " ) , person. age)
208200 let addressProperty : Address = try modelOutput. value ( forProperty: " address " )
209- #expect ( addressProperty == person. address)
210- #expect ( try modelOutput. value ( ) == person)
211- #expect ( orderedKeys == [ " firstName " , " middleName " , " lastName " , " age " , " address " ] )
201+ XCTAssertEqual ( addressProperty, person. address)
202+ XCTAssertEqual ( try modelOutput. value ( ) , person)
203+ XCTAssertEqual ( orderedKeys, [ " firstName " , " middleName " , " lastName " , " age " , " address " ] )
212204 }
213205
214- @Test
215- func convertGenerableWithNilOptionalPropertyToModelOutput( ) throws {
206+ func testConvertGenerableWithNilOptionalPropertyToModelOutput( ) throws {
216207 let address = Address ( street: " 789 Pine Ln " , city: " Nowhere " , zipCode: " 00000 " )
217208 let person = Person (
218209 firstName: " Jane " ,
@@ -225,38 +216,37 @@ struct GenerableTests {
225216 let modelOutput = person. modelOutput
226217
227218 guard case let . structure( properties, orderedKeys) = modelOutput. kind else {
228- Issue . record ( " Model output is not a structure. " )
219+ XCTFail ( " Model output is not a structure. " )
229220 return
230221 }
231- #expect ( properties [ " middleName " ] == nil )
232- #expect ( orderedKeys == [ " firstName " , " lastName " , " age " , " address " ] )
222+ XCTAssertNil ( properties [ " middleName " ] )
223+ XCTAssertEqual ( orderedKeys, [ " firstName " , " lastName " , " age " , " address " ] )
233224 }
234225
235- @Test
236226 func testPersonJSONSchema( ) throws {
237227 let schema = Person . jsonSchema
238228
239229 guard case let . object( _, _, properties) = schema. kind else {
240- Issue . record ( " Schema kind is not an object. " )
230+ XCTFail ( " Schema kind is not an object. " )
241231 return
242232 }
243233
244- #expect ( properties. count == 5 )
245- let firstName = try #require ( properties. first { $0. name == " firstName " } )
246- #expect ( ObjectIdentifier ( firstName. type) == ObjectIdentifier ( String . self) )
247- #expect ( firstName. isOptional == false )
248- let middleName = try #require ( properties. first { $0. name == " middleName " } )
249- #expect ( ObjectIdentifier ( middleName. type) == ObjectIdentifier ( String . self) )
250- #expect ( middleName. isOptional == true )
251- let lastName = try #require ( properties. first { $0. name == " lastName " } )
252- #expect ( ObjectIdentifier ( lastName. type) == ObjectIdentifier ( String . self) )
253- #expect ( lastName. isOptional == false )
254- let age = try #require ( properties. first { $0. name == " age " } )
255- #expect ( ObjectIdentifier ( age. type) == ObjectIdentifier ( Int . self) )
256- #expect ( age. isOptional == false )
257- let address = try #require ( properties. first { $0. name == " address " } )
258- #expect ( ObjectIdentifier ( address. type) == ObjectIdentifier ( Address . self) )
259- #expect ( address. isOptional == false )
234+ XCTAssertEqual ( properties. count, 5 )
235+ let firstName = try XCTUnwrap ( properties. first { $0. name == " firstName " } )
236+ XCTAssert ( firstName. type == String . self)
237+ XCTAssertFalse ( firstName. isOptional)
238+ let middleName = try XCTUnwrap ( properties. first { $0. name == " middleName " } )
239+ XCTAssert ( middleName. type == String . self)
240+ XCTAssertTrue ( middleName. isOptional)
241+ let lastName = try XCTUnwrap ( properties. first { $0. name == " lastName " } )
242+ XCTAssert ( lastName. type == String . self)
243+ XCTAssertFalse ( lastName. isOptional)
244+ let age = try XCTUnwrap ( properties. first { $0. name == " age " } )
245+ XCTAssert ( age. type == Int . self)
246+ XCTAssertFalse ( age. isOptional)
247+ let address = try XCTUnwrap ( properties. first { $0. name == " address " } )
248+ XCTAssert ( address. type == Address . self)
249+ XCTAssertFalse ( address. isOptional)
260250 }
261251}
262252
0 commit comments