@@ -18,70 +18,35 @@ final class TLVCodingTests: XCTestCase {
1818 ( " testUUID " , testUUID) ,
1919 ( " testDate " , testDate) ,
2020 ( " testDateSecondsSince1970 " , testDateSecondsSince1970) ,
21- ( " testOutputFormatting " , testOutputFormatting)
21+ ( " testOutputFormatting " , testOutputFormatting) ,
22+ ( " testNil " , testNil)
2223 ]
2324
2425 func testCodable( ) {
2526
26- func test < T: Codable & Equatable > ( _ value: T , _ data: Data , shouldFail: Bool = false ) {
27-
28- var didFail = false
29- var encoder = TLVEncoder ( )
30- encoder. log = { print ( " Encoder: " , $0) }
31- do {
32- let encodedData = try encoder. encode ( value)
33- if shouldFail == false {
34- XCTAssertEqual ( encodedData, data, " Invalid data \( Array ( encodedData) ) " )
35- }
36- } catch {
37- dump ( error)
38- if shouldFail == false {
39- XCTFail ( " Could not encode \( value) " )
40- }
41- didFail = true
42- }
43-
44- var decoder = TLVDecoder ( )
45- decoder. log = { print ( " Decoder: " , $0) }
46- do {
47- let decodedValue = try decoder. decode ( T . self, from: data)
48- XCTAssertEqual ( decodedValue, value)
49- } catch {
50- dump ( error)
51- if shouldFail == false {
52- XCTFail ( " Could not decode \( value) " )
53- }
54- didFail = true
55- }
56-
57- if shouldFail {
58- XCTAssert ( didFail, " No error thrown " )
59- }
60- }
61-
62- test ( Person ( gender: . male, name: " Coleman " ) ,
27+ compare ( Person ( gender: . male, name: " Coleman " ) ,
6328 Data ( [ 0 , 1 , 0 , 1 , 7 , 67 , 111 , 108 , 101 , 109 , 97 , 110 ] ) )
6429
65- test ( Person ( gender: . male, name: " Coleman " ) ,
30+ compare ( Person ( gender: . male, name: " Coleman " ) ,
6631 Data ( [ 0 , 1 ] ) ,
6732 shouldFail: true
6833 )
6934
70- test ( Person ( gender: . male, name: " Coleman " ) ,
35+ compare ( Person ( gender: . male, name: " Coleman " ) ,
7136 Data ( [ 0 , 2 , 0 ] ) ,
7237 shouldFail: true
7338 )
7439
75- test ( Person ( gender: . male, name: " " ) ,
40+ compare ( Person ( gender: . male, name: " " ) ,
7641 Data ( [ 0 , 1 , 0 , 1 , 0 ] ) )
7742
78- test ( ProvisioningState ( state: . idle, result: . notAvailible) ,
43+ compare ( ProvisioningState ( state: . idle, result: . notAvailible) ,
7944 Data ( [ 0x01 , 0x01 , 0x00 , 0x02 , 0x01 , 0x00 ] ) )
8045
81- test ( ProvisioningState ( state: . provisioning, result: . notAvailible) ,
46+ compare ( ProvisioningState ( state: . provisioning, result: . notAvailible) ,
8247 Data ( [ 0x01 , 0x01 , 0x01 , 0x02 , 0x01 , 0x00 ] ) )
8348
84- test ( Numeric (
49+ compare ( Numeric (
8550 boolean: true ,
8651 int: - 10 ,
8752 uint: 10 ,
@@ -97,7 +62,7 @@ final class TLVCodingTests: XCTestCase {
9762 uint64: 30_000 ) ,
9863 Data ( [ 0 , 1 , 1 , 1 , 4 , 246 , 255 , 255 , 255 , 2 , 4 , 10 , 0 , 0 , 0 , 3 , 4 , 146 , 203 , 143 , 63 , 4 , 8 , 114 , 138 , 142 , 228 , 242 , 255 , 37 , 64 , 5 , 1 , 127 , 6 , 2 , 56 , 255 , 7 , 4 , 48 , 248 , 255 , 255 , 8 , 8 , 224 , 177 , 255 , 255 , 255 , 255 , 255 , 255 , 9 , 1 , 255 , 10 , 2 , 44 , 1 , 11 , 4 , 184 , 11 , 0 , 0 , 12 , 8 , 48 , 117 , 0 , 0 , 0 , 0 , 0 , 0 ] ) )
9964
100- test (
65+ compare (
10166 CustomEncodable (
10267 data: nil ,
10368 uuid: nil ,
@@ -107,7 +72,7 @@ final class TLVCodingTests: XCTestCase {
10772 Data ( [ ] )
10873 )
10974
110- test (
75+ compare (
11176 Profile (
11277 person: Person (
11378 gender: . male,
@@ -124,7 +89,7 @@ final class TLVCodingTests: XCTestCase {
12489 1 , 14 , 0 , 12 , 0 , 1 , 0 , 1 , 7 , 67 , 111 , 108 , 101 , 109 , 97 , 110 ] )
12590 )
12691
127- test (
92+ compare (
12893 Profile (
12994 person: Person (
13095 gender: . male,
@@ -168,23 +133,23 @@ final class TLVCodingTests: XCTestCase {
168133 ] )
169134 )
170135
171- test (
136+ compare (
172137 Binary (
173138 data: Data ( [ 0x01 , 0x02 , 0x03 , 0x04 ] ) ,
174139 value: . one
175140 ) ,
176141 Data ( [ 0 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 1 , 0 ] )
177142 )
178143
179- test (
144+ compare (
180145 PrimitiveArray (
181146 strings: [ " 1 " , " two " , " three " , " " ] ,
182147 integers: [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
183148 ) ,
184149 Data ( [ 0 , 17 , 0 , 1 , 49 , 1 , 3 , 116 , 119 , 111 , 2 , 5 , 116 , 104 , 114 , 101 , 101 , 3 , 0 , 1 , 60 , 0 , 4 , 1 , 0 , 0 , 0 , 1 , 4 , 2 , 0 , 0 , 0 , 2 , 4 , 3 , 0 , 0 , 0 , 3 , 4 , 4 , 0 , 0 , 0 , 4 , 4 , 5 , 0 , 0 , 0 , 5 , 4 , 6 , 0 , 0 , 0 , 6 , 4 , 7 , 0 , 0 , 0 , 7 , 4 , 8 , 0 , 0 , 0 , 8 , 4 , 9 , 0 , 0 , 0 , 9 , 4 , 10 , 0 , 0 , 0 ] )
185150 )
186151
187- test (
152+ compare (
188153 DeviceInformation (
189154 identifier: UUID ( uuidString: " B83DD6F4-A429-41B3-945A-3E0EE5915CA1 " ) !,
190155 buildVersion: DeviceInformation . BuildVersion ( rawValue: 1 ) ,
@@ -195,12 +160,12 @@ final class TLVCodingTests: XCTestCase {
195160 Data ( [ 0 , 16 , 184 , 61 , 214 , 244 , 164 , 41 , 65 , 179 , 148 , 90 , 62 , 14 , 229 , 145 , 92 , 161 , 1 , 8 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 3 , 1 , 2 , 3 , 3 , 1 , 2 , 4 , 1 , 7 ] )
196161 )
197162
198- test (
163+ compare (
199164 CryptoRequest ( secret: CryptoData ( ) ) ,
200165 Data ( [ 0 , 32 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 ] )
201166 )
202167
203- test (
168+ compare (
204169 CustomEncodableArray ( elements: [
205170 . value(
206171 CustomEncodableArray . Value (
@@ -374,6 +339,72 @@ final class TLVCodingTests: XCTestCase {
374339
375340 XCTAssertNotEqual ( try encoder. encode ( value) , try encoder. encode ( valueUnordered) )
376341 }
342+
343+ func testNil( ) {
344+
345+ let data = Data ( [
346+ 0 , 0 ,
347+ 1 , 0 ,
348+ 2 , 0 ,
349+ 3 , 0
350+ ] )
351+
352+ let value = CustomEncodable (
353+ data: nil ,
354+ uuid: nil ,
355+ number: nil ,
356+ date: nil
357+ )
358+
359+ var decoder = TLVDecoder ( )
360+ decoder. log = { print ( " Decoder: " , $0) }
361+ do {
362+ let decodedValue = try decoder. decode ( type ( of: value) , from: data)
363+ XCTAssertEqual ( decodedValue, value)
364+ } catch {
365+ dump ( error)
366+ XCTFail ( " Could not decode \( value) " )
367+ }
368+ }
369+ }
370+
371+ private extension TLVCodingTests {
372+
373+ func compare < T: Codable & Equatable > ( _ value: T , _ data: Data , shouldFail: Bool = false ) {
374+
375+ var didFail = false
376+ var encoder = TLVEncoder ( )
377+ encoder. log = { print ( " Encoder: " , $0) }
378+ do {
379+ let encodedData = try encoder. encode ( value)
380+ if shouldFail == false {
381+ XCTAssertEqual ( encodedData, data, " Invalid data \( Array ( encodedData) ) " )
382+ }
383+ } catch {
384+ dump ( error)
385+ if shouldFail == false {
386+ XCTFail ( " Could not encode \( value) " )
387+ }
388+ didFail = true
389+ }
390+
391+ var decoder = TLVDecoder ( )
392+ decoder. log = { print ( " Decoder: " , $0) }
393+ do {
394+ let decodedValue = try decoder. decode ( T . self, from: data)
395+ XCTAssertEqual ( decodedValue, value)
396+ } catch {
397+ dump ( error)
398+ if shouldFail == false {
399+ XCTFail ( " Could not decode \( value) " )
400+ }
401+ didFail = true
402+ }
403+
404+ if shouldFail {
405+ XCTAssert ( didFail, " No error thrown " )
406+ }
407+ }
377408}
378409
379410// MARK: - Supporting Types
0 commit comments