@@ -10,6 +10,8 @@ import Testing
1010@Suite ( " Conditional Coder Tests " )
1111struct ConditionalCoderTests {
1212
13+ // MARK: Prefix & Suffix
14+
1315 /// A decoder-only coder that prefixes decoded strings.
1416 struct PrefixDecoder : HelperCoder {
1517 let prefix : String
@@ -36,6 +38,8 @@ struct ConditionalCoderTests {
3638 }
3739 }
3840
41+ // MARK: Wrappers
42+
3943 /// Wrapper for decoding tests.
4044 struct DecodingWrapper : Decodable {
4145 let value : String ?
@@ -76,57 +80,64 @@ struct ConditionalCoderTests {
7680 }
7781 }
7882
79- // MARK: - decodeIfPresent Tests
80-
81- /// Tests that `decodeIfPresent` uses the decoder coder and returns value.
82- @Test ( " Decodes from JSON successfully (ConditionalCoderTests #34) " , . tags( . conditionalCoder, . decoding) )
83- func decodeIfPresentReturnsValue( ) throws {
84- let json = #"{"value": "test"}"#
85- let data = json. data ( using: . utf8) !
86-
87- let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self, from: data)
88- #expect( result. value == " decoded:test " )
89- }
83+ // MARK: Decode if present
9084
91- /// Tests that `decodeIfPresent` returns nil when value is null.
92- @Test ( " Decodes from JSON successfully (ConditionalCoderTests #35) " , . tags( . conditionalCoder, . decoding) )
93- func decodeIfPresentReturnsNilForNull( ) throws {
94- let json = #"{"value": null}"#
95- let data = json. data ( using: . utf8) !
85+ @Suite ( " Coder Tests → Decode if Present " )
86+ struct DecodeIfPresentCoderTests {
87+ /// Tests that `decodeIfPresent` uses the decoder coder and returns value.
88+ @Test ( " Decodes from JSON successfully (ConditionalCoderTests #34) " , . tags( . conditionalCoder, . decoding) )
89+ func decodeIfPresentReturnsValue( ) throws {
90+ let json = #"{"value": "test"}"#
91+ let data = json. data ( using: . utf8) !
92+
93+ let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self, from: data)
94+ #expect( result. value == " decoded:test " )
95+ }
9696
97- let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self , from : data )
98- #expect ( result . value == nil )
99- }
100-
101- /// Tests that `decodeIfPresent` returns nil when key is missing.
102- @ Test ( " Decodes from JSON successfully (ConditionalCoderTests #36) " , . tags ( . conditionalCoder , . decoding ) )
103- func decodeIfPresentReturnsNilForMissingKey ( ) throws {
104- let json = #"{}"#
105- let data = json . data ( using : . utf8 ) !
97+ /// Tests that `decodeIfPresent` returns nil when value is null.
98+ @ Test ( " Decodes from JSON successfully (ConditionalCoderTests #35) " , . tags ( . conditionalCoder , . decoding ) )
99+ func decodeIfPresentReturnsNilForNull ( ) throws {
100+ let json = #"{"value": null}"#
101+ let data = json . data ( using : . utf8 ) !
102+
103+ let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self , from : data )
104+ #expect ( result . value == nil )
105+ }
106106
107- let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self, from: data)
108- #expect( result. value == nil )
107+ /// Tests that `decodeIfPresent` returns nil when key is missing.
108+ @Test ( " Decodes from JSON successfully (ConditionalCoderTests #36) " , . tags( . conditionalCoder, . decoding) )
109+ func decodeIfPresentReturnsNilForMissingKey( ) throws {
110+ let json = #"{}"#
111+ let data = json. data ( using: . utf8) !
112+
113+ let result = try JSONDecoder ( ) . decode ( DecodingWrapper . self, from: data)
114+ #expect( result. value == nil )
115+ }
109116 }
110117
111- // MARK: - encodeIfPresent Tests
118+ // MARK: Encode if present
112119
113- /// Tests that `encodeIfPresent` uses the encoder coder when value is present.
114- @Test ( " Encodes to JSON successfully (ConditionalCoderTests #7) " , . tags( . conditionalCoder, . encoding) )
115- func encodeIfPresentEncodesValue( ) throws {
116- let wrapper = EncodingWrapper ( value: " test " )
117- let data = try JSONEncoder ( ) . encode ( wrapper)
118- let json = String ( data: data, encoding: . utf8) !
119-
120- #expect( json. contains ( " test:encoded " ) )
121- }
122-
123- /// Tests that `encodeIfPresent` skips encoding when value is nil.
124- @Test ( " Encodes to JSON successfully (ConditionalCoderTests #8) " , . tags( . conditionalCoder, . encoding) )
125- func encodeIfPresentSkipsNil( ) throws {
126- let wrapper = EncodingWrapper ( value: nil )
127- let data = try JSONEncoder ( ) . encode ( wrapper)
128- let json = String ( data: data, encoding: . utf8) !
120+ @Suite ( " Coder Tests → Encode if Present " )
121+ struct EncodeIfPresentCoderTests {
122+ /// Tests that `encodeIfPresent` uses the encoder coder
123+ /// when value is present.
124+ @Test ( " Encodes to JSON successfully (ConditionalCoderTests #7) " , . tags( . conditionalCoder, . encoding) )
125+ func encodeIfPresentEncodesValue( ) throws {
126+ let wrapper = EncodingWrapper ( value: " test " )
127+ let data = try JSONEncoder ( ) . encode ( wrapper)
128+ let json = String ( data: data, encoding: . utf8) !
129+
130+ #expect( json. contains ( " test:encoded " ) )
131+ }
129132
130- #expect( json == " {} " )
133+ /// Tests that `encodeIfPresent` skips encoding when value is nil.
134+ @Test ( " Encodes to JSON successfully (ConditionalCoderTests #8) " , . tags( . conditionalCoder, . encoding) )
135+ func encodeIfPresentSkipsNil( ) throws {
136+ let wrapper = EncodingWrapper ( value: nil )
137+ let data = try JSONEncoder ( ) . encode ( wrapper)
138+ let json = String ( data: data, encoding: . utf8) !
139+
140+ #expect( json == " {} " )
141+ }
131142 }
132143}
0 commit comments