@@ -21,6 +21,40 @@ struct TopLevelObjectWrapper<T: Codable & Equatable>: Codable, Equatable {
2121
2222class TestJSONEncoder : XCTestCase {
2323
24+ // MARK: - Encoding Top-Level fragments
25+ func test_encodingTopLevelFragments( ) {
26+
27+ func _testFragment< T: Codable & Equatable > ( value: T , fragment: String ) {
28+ let data : Data
29+ let payload : String
30+
31+ do {
32+ data = try JSONEncoder ( ) . encode ( value)
33+ payload = try XCTUnwrap ( String . init ( decoding: data, as: UTF8 . self) )
34+ XCTAssertEqual ( fragment, payload)
35+ } catch {
36+ XCTFail ( " Failed to encode \( T . self) to JSON: \( error) " )
37+ return
38+ }
39+ do {
40+ let decodedValue = try JSONDecoder ( ) . decode ( T . self, from: data)
41+ XCTAssertEqual ( value, decodedValue)
42+ } catch {
43+ XCTFail ( " Failed to decode \( payload) to \( T . self) : \( error) " )
44+ }
45+ }
46+
47+ _testFragment ( value: 2 , fragment: " 2 " )
48+ _testFragment ( value: false , fragment: " false " )
49+ _testFragment ( value: true , fragment: " true " )
50+ _testFragment ( value: Float ( 1 ) , fragment: " 1 " )
51+ _testFragment ( value: Double ( 2 ) , fragment: " 2 " )
52+ _testFragment ( value: Decimal ( Double . leastNormalMagnitude) , fragment: " 0.0000000000000000000000000000000000000000000000000002225073858507201792 " )
53+ _testFragment ( value: " test " , fragment: " \" test \" " )
54+ let v : Int ? = nil
55+ _testFragment ( value: v, fragment: " null " )
56+ }
57+
2458 // MARK: - Encoding Top-Level Empty Types
2559 func test_encodingTopLevelEmptyStruct( ) {
2660 let empty = EmptyStruct ( )
@@ -34,20 +68,20 @@ class TestJSONEncoder : XCTestCase {
3468
3569 // MARK: - Encoding Top-Level Single-Value Types
3670 func test_encodingTopLevelSingleValueEnum( ) {
37- _testEncodeFailure ( of: Switch . off)
38- _testEncodeFailure ( of: Switch . on)
71+ _testRoundTrip ( of: Switch . off)
72+ _testRoundTrip ( of: Switch . on)
3973
4074 _testRoundTrip ( of: TopLevelArrayWrapper ( Switch . off) )
4175 _testRoundTrip ( of: TopLevelArrayWrapper ( Switch . on) )
4276 }
4377
4478 func test_encodingTopLevelSingleValueStruct( ) {
45- _testEncodeFailure ( of: Timestamp ( 3141592653 ) )
79+ _testRoundTrip ( of: Timestamp ( 3141592653 ) )
4680 _testRoundTrip ( of: TopLevelArrayWrapper ( Timestamp ( 3141592653 ) ) )
4781 }
4882
4983 func test_encodingTopLevelSingleValueClass( ) {
50- _testEncodeFailure ( of: Counter ( ) )
84+ _testRoundTrip ( of: Counter ( ) )
5185 _testRoundTrip ( of: TopLevelArrayWrapper ( Counter ( ) ) )
5286 }
5387
@@ -387,6 +421,11 @@ class TestJSONEncoder : XCTestCase {
387421 }
388422 }
389423
424+ func test_codingOfNil( ) {
425+ let x : Int ? = nil
426+ test_codingOf ( value: x, toAndFrom: " null " )
427+ }
428+
390429 func test_codingOfInt8( ) {
391430 test_codingOf ( value: Int8 ( - 42 ) , toAndFrom: " -42 " )
392431 }
@@ -1259,6 +1298,7 @@ fileprivate struct JSON: Equatable {
12591298extension TestJSONEncoder {
12601299 static var allTests : [ ( String , ( TestJSONEncoder ) -> ( ) throws -> Void ) ] {
12611300 return [
1301+ ( " test_encodingTopLevelFragments " , test_encodingTopLevelFragments) ,
12621302 ( " test_encodingTopLevelEmptyStruct " , test_encodingTopLevelEmptyStruct) ,
12631303 ( " test_encodingTopLevelEmptyClass " , test_encodingTopLevelEmptyClass) ,
12641304 ( " test_encodingTopLevelSingleValueEnum " , test_encodingTopLevelSingleValueEnum) ,
@@ -1282,6 +1322,7 @@ extension TestJSONEncoder {
12821322 ( " test_nestedContainerCodingPaths " , test_nestedContainerCodingPaths) ,
12831323 ( " test_superEncoderCodingPaths " , test_superEncoderCodingPaths) ,
12841324 ( " test_codingOfBool " , test_codingOfBool) ,
1325+ ( " test_codingOfNil " , test_codingOfNil) ,
12851326 ( " test_codingOfInt8 " , test_codingOfInt8) ,
12861327 ( " test_codingOfUInt8 " , test_codingOfUInt8) ,
12871328 ( " test_codingOfInt16 " , test_codingOfInt16) ,
0 commit comments