@@ -16,53 +16,46 @@ import XCTest
1616@testable import GoogleGenerativeAI
1717
1818final class JSONValueTests : XCTestCase {
19- let decoder = JSONDecoder ( )
20- let encoder = JSONEncoder ( )
21-
22- let numberKey = " pi "
23- let numberValue = 3.14159
24- let numberValueEncoded = " 3.14159 "
25- let stringKey = " hello "
26- let stringValue = " Hello, world! "
27-
28- override func setUp( ) {
29- encoder. outputFormatting = . sortedKeys
30- }
31-
3219 func testDecodeNull( ) throws {
3320 let jsonData = try XCTUnwrap ( " null " . data ( using: . utf8) )
3421
35- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
22+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
3623
3724 XCTAssertEqual ( jsonObject, . null)
3825 }
3926
4027 func testDecodeNumber( ) throws {
41- let jsonData = try XCTUnwrap ( " \( numberValue) " . data ( using: . utf8) )
28+ let expectedNumber = 3.14159
29+ let jsonData = try XCTUnwrap ( " \( expectedNumber) " . data ( using: . utf8) )
4230
43- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
31+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
4432
45- XCTAssertEqual ( jsonObject, . number( numberValue ) )
33+ XCTAssertEqual ( jsonObject, . number( expectedNumber ) )
4634 }
4735
4836 func testDecodeString( ) throws {
49- let jsonData = try XCTUnwrap ( " \" \( stringValue) \" " . data ( using: . utf8) )
37+ let expectedString = " hello-world "
38+ let jsonData = try XCTUnwrap ( " \" \( expectedString) \" " . data ( using: . utf8) )
5039
51- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
40+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
5241
53- XCTAssertEqual ( jsonObject, . string( stringValue ) )
42+ XCTAssertEqual ( jsonObject, . string( expectedString ) )
5443 }
5544
5645 func testDecodeBool( ) throws {
5746 let expectedBool = true
5847 let jsonData = try XCTUnwrap ( " \( expectedBool) " . data ( using: . utf8) )
5948
60- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
49+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
6150
6251 XCTAssertEqual ( jsonObject, . bool( expectedBool) )
6352 }
6453
6554 func testDecodeObject( ) throws {
55+ let numberKey = " pi "
56+ let numberValue = 3.14159
57+ let stringKey = " hello "
58+ let stringValue = " world "
6659 let expectedObject : JSONObject = [
6760 numberKey: . number( numberValue) ,
6861 stringKey: . string( stringValue) ,
@@ -75,71 +68,18 @@ final class JSONValueTests: XCTestCase {
7568 """
7669 let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
7770
78- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
71+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
7972
8073 XCTAssertEqual ( jsonObject, . object( expectedObject) )
8174 }
8275
8376 func testDecodeArray( ) throws {
77+ let numberValue = 3.14159
8478 let expectedArray : [ JSONValue ] = [ . null, . number( numberValue) ]
8579 let jsonData = try XCTUnwrap ( " [ null, \( numberValue) ] " . data ( using: . utf8) )
8680
87- let jsonObject = try XCTUnwrap ( decoder . decode ( JSONValue . self, from: jsonData) )
81+ let jsonObject = try XCTUnwrap ( JSONDecoder ( ) . decode ( JSONValue . self, from: jsonData) )
8882
8983 XCTAssertEqual ( jsonObject, . array( expectedArray) )
9084 }
91-
92- func testEncodeNull( ) throws {
93- let jsonData = try encoder. encode ( JSONValue . null)
94-
95- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
96- XCTAssertEqual ( json, " null " )
97- }
98-
99- func testEncodeNumber( ) throws {
100- let jsonData = try encoder. encode ( JSONValue . number ( numberValue) )
101-
102- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
103- XCTAssertEqual ( json, " \( numberValue) " )
104- }
105-
106- func testEncodeString( ) throws {
107- let jsonData = try encoder. encode ( JSONValue . string ( stringValue) )
108-
109- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
110- XCTAssertEqual ( json, " \" \( stringValue) \" " )
111- }
112-
113- func testEncodeBool( ) throws {
114- let boolValue = true
115-
116- let jsonData = try encoder. encode ( JSONValue . bool ( boolValue) )
117-
118- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
119- XCTAssertEqual ( json, " \( boolValue) " )
120- }
121-
122- func testEncodeObject( ) throws {
123- let objectValue : JSONObject = [
124- numberKey: . number( numberValue) ,
125- stringKey: . string( stringValue) ,
126- ]
127-
128- let jsonData = try encoder. encode ( JSONValue . object ( objectValue) )
129-
130- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
131- XCTAssertEqual (
132- json,
133- " { \" \( stringKey) \" : \" \( stringValue) \" , \" \( numberKey) \" : \( numberValueEncoded) } "
134- )
135- }
136-
137- func testEncodeArray( ) throws {
138- let arrayValue : [ JSONValue ] = [ . null, . number( numberValue) ]
139-
140- let jsonData = try encoder. encode ( JSONValue . array ( arrayValue) )
141-
142- let json = try XCTUnwrap ( String ( data: jsonData, encoding: . utf8) )
143- XCTAssertEqual ( json, " [null, \( numberValueEncoded) ] " )
144- }
14585}
0 commit comments