Skip to content

Commit dd394d9

Browse files
committed
Added tests for codable Range types
1 parent 3631959 commit dd394d9

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

test/stdlib/CodableTests.swift

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,25 @@ func debugDescription<T>(_ value: T) -> String {
4949
}
5050
}
5151

52-
func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (Data) throws -> T, lineNumber: Int) where T : Equatable {
52+
func performEncodeAndDecode<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (T.Type, Data) throws -> T, lineNumber: Int) -> T {
53+
5354
let data: Data
5455
do {
5556
data = try encode(value)
5657
} catch {
5758
fatalError("\(#file):\(lineNumber): Unable to encode \(T.self) <\(debugDescription(value))>: \(error)")
5859
}
5960

60-
let decoded: T
6161
do {
62-
decoded = try decode(data)
62+
return try decode(T.self, data)
6363
} catch {
6464
fatalError("\(#file):\(lineNumber): Unable to decode \(T.self) <\(debugDescription(value))>: \(error)")
6565
}
66+
}
67+
68+
func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (T.Type, Data) throws -> T, lineNumber: Int) where T : Equatable {
69+
70+
let decoded = performEncodeAndDecode(of: value, encode: encode, decode: decode, lineNumber: lineNumber)
6671

6772
expectEqual(value, decoded, "\(#file):\(lineNumber): Decoded \(T.self) <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
6873
}
@@ -77,12 +82,12 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T, lineNumber: I
7782
return try encoder.encode(value)
7883
}
7984

80-
let decode = { (_ data: Data) throws -> T in
85+
let decode = { (_ type: T.Type, _ data: Data) throws -> T in
8186
let decoder = JSONDecoder()
8287
decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: inf,
8388
negativeInfinity: negInf,
8489
nan: nan)
85-
return try decoder.decode(T.self, from: data)
90+
return try decoder.decode(type, from: data)
8691
}
8792

8893
expectRoundTripEquality(of: value, encode: encode, decode: decode, lineNumber: lineNumber)
@@ -93,8 +98,8 @@ func expectRoundTripEqualityThroughPlist<T : Codable>(for value: T, lineNumber:
9398
return try PropertyListEncoder().encode(value)
9499
}
95100

96-
let decode = { (_ data: Data) throws -> T in
97-
return try PropertyListDecoder().decode(T.self, from: data)
101+
let decode = { (_ type: T.Type,_ data: Data) throws -> T in
102+
return try PropertyListDecoder().decode(type, from: data)
98103
}
99104

100105
expectRoundTripEquality(of: value, encode: encode, decode: decode, lineNumber: lineNumber)
@@ -351,6 +356,21 @@ class TestCodable : TestCodableSuper {
351356
}
352357
}
353358

359+
// MARK: - ClosedRange
360+
func test_ClosedRange_JSON() {
361+
let value = 0...Int.max
362+
let decoded = performEncodeAndDecode(of: value, encode: { try JSONEncoder().encode($0) }, decode: { try JSONDecoder().decode($0, from: $1) }, lineNumber: #line)
363+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded ClosedRange upperBound <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
364+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded ClosedRange lowerBound <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
365+
}
366+
367+
func test_ClosedRange_Plist() {
368+
let value = 0...Int.max
369+
let decoded = performEncodeAndDecode(of: value, encode: { try PropertyListEncoder().encode($0) }, decode: { try PropertyListDecoder().decode($0, from: $1) }, lineNumber: #line)
370+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded ClosedRange upperBound <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
371+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded ClosedRange lowerBound <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
372+
}
373+
354374
// MARK: - DateComponents
355375
lazy var dateComponents: Set<Calendar.Component> = [
356376
.era, .year, .month, .day, .hour, .minute, .second, .nanosecond,
@@ -522,6 +542,45 @@ class TestCodable : TestCodableSuper {
522542
}
523543
}
524544

545+
// MARK: - PartialRangeFrom
546+
func test_PartialRangeFrom_JSON() {
547+
let value = 0...
548+
let decoded = performEncodeAndDecode(of: value, encode: { try JSONEncoder().encode($0) }, decode: { try JSONDecoder().decode($0, from: $1) }, lineNumber: #line)
549+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded PartialRangeFrom <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
550+
}
551+
552+
func test_PartialRangeFrom_Plist() {
553+
let value = 0...
554+
let decoded = performEncodeAndDecode(of: value, encode: { try PropertyListEncoder().encode($0) }, decode: { try PropertyListDecoder().decode($0, from: $1) }, lineNumber: #line)
555+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded PartialRangeFrom <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
556+
}
557+
558+
// MARK: - PartialRangeThrough
559+
func test_PartialRangeThrough_JSON() {
560+
let value = ...Int.max
561+
let decoded = performEncodeAndDecode(of: value, encode: { try JSONEncoder().encode($0) }, decode: { try JSONDecoder().decode($0, from: $1) }, lineNumber: #line)
562+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded PartialRangeThrough <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
563+
}
564+
565+
func test_PartialRangeThrough_Plist() {
566+
let value = ...Int.max
567+
let decoded = performEncodeAndDecode(of: value, encode: { try PropertyListEncoder().encode($0) }, decode: { try PropertyListDecoder().decode($0, from: $1) }, lineNumber: #line)
568+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded PartialRangeThrough <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
569+
}
570+
571+
// MARK: - PartialRangeUpTo
572+
func test_PartialRangeUpTo_JSON() {
573+
let value = ..<Int.max
574+
let decoded = performEncodeAndDecode(of: value, encode: { try JSONEncoder().encode($0) }, decode: { try JSONDecoder().decode($0, from: $1) }, lineNumber: #line)
575+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded PartialRangeUpTo <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
576+
}
577+
578+
func test_PartialRangeUpTo_Plist() {
579+
let value = ..<Int.max
580+
let decoded = performEncodeAndDecode(of: value, encode: { try PropertyListEncoder().encode($0) }, decode: { try PropertyListDecoder().decode($0, from: $1) }, lineNumber: #line)
581+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded PartialRangeUpTo <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
582+
}
583+
525584
// MARK: - PersonNameComponents
526585
@available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *)
527586
lazy var personNameComponentsValues: [Int : PersonNameComponents] = [
@@ -544,6 +603,21 @@ class TestCodable : TestCodableSuper {
544603
}
545604
}
546605

606+
// MARK: - Range
607+
func test_Range_JSON() {
608+
let value = 0..<Int.max
609+
let decoded = performEncodeAndDecode(of: value, encode: { try JSONEncoder().encode($0) }, decode: { try JSONDecoder().decode($0, from: $1) }, lineNumber: #line)
610+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded Range upperBound <\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
611+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded Range lowerBound<\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
612+
}
613+
614+
func test_Range_Plist() {
615+
let value = 0..<Int.max
616+
let decoded = performEncodeAndDecode(of: value, encode: { try PropertyListEncoder().encode($0) }, decode: { try PropertyListDecoder().decode($0, from: $1) }, lineNumber: #line)
617+
expectEqual(value.upperBound, decoded.upperBound, "\(#file):\(#line): Decoded Range upperBound<\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
618+
expectEqual(value.lowerBound, decoded.lowerBound, "\(#file):\(#line): Decoded Range lowerBound<\(debugDescription(decoded))> not equal to original <\(debugDescription(value))>")
619+
}
620+
547621
// MARK: - TimeZone
548622
lazy var timeZoneValues: [Int : TimeZone] = [
549623
#line : TimeZone(identifier: "America/Los_Angeles")!,
@@ -754,6 +828,8 @@ var tests = [
754828
"test_CGRect_Plist" : TestCodable.test_CGRect_Plist,
755829
"test_CGVector_JSON" : TestCodable.test_CGVector_JSON,
756830
"test_CGVector_Plist" : TestCodable.test_CGVector_Plist,
831+
"test_ClosedRange_JSON" : TestCodable.test_ClosedRange_JSON,
832+
"test_ClosedRange_Plist" : TestCodable.test_ClosedRange_Plist,
757833
"test_DateComponents_JSON" : TestCodable.test_DateComponents_JSON,
758834
"test_DateComponents_Plist" : TestCodable.test_DateComponents_Plist,
759835
"test_Decimal_JSON" : TestCodable.test_Decimal_JSON,
@@ -766,6 +842,14 @@ var tests = [
766842
"test_Locale_Plist" : TestCodable.test_Locale_Plist,
767843
"test_NSRange_JSON" : TestCodable.test_NSRange_JSON,
768844
"test_NSRange_Plist" : TestCodable.test_NSRange_Plist,
845+
"test_PartialRangeFrom_JSON" : TestCodable.test_PartialRangeFrom_JSON,
846+
"test_PartialRangeFrom_Plist" : TestCodable.test_PartialRangeFrom_Plist,
847+
"test_PartialRangeThrough_JSON" : TestCodable.test_PartialRangeThrough_JSON,
848+
"test_PartialRangeThrough_Plist" : TestCodable.test_PartialRangeThrough_Plist,
849+
"test_PartialRangeUpTo_JSON" : TestCodable.test_PartialRangeUpTo_JSON,
850+
"test_PartialRangeUpTo_Plist" : TestCodable.test_PartialRangeUpTo_Plist,
851+
"test_Range_JSON" : TestCodable.test_Range_JSON,
852+
"test_Range_Plist" : TestCodable.test_Range_Plist,
769853
"test_TimeZone_JSON" : TestCodable.test_TimeZone_JSON,
770854
"test_TimeZone_Plist" : TestCodable.test_TimeZone_Plist,
771855
"test_URL_JSON" : TestCodable.test_URL_JSON,

0 commit comments

Comments
 (0)