Skip to content

Commit e6e7b88

Browse files
committed
use typealias to increase test maintainability
1 parent 9b14e19 commit e6e7b88

File tree

2 files changed

+356
-338
lines changed

2 files changed

+356
-338
lines changed

Tests/EnvironmentDecoderTests/EnvironmentDecoderCustomTypeTests.swift

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,40 @@ import Testing
44

55
struct EnvironmentDecoderCustomTypeTests {
66
@Test func base64DataTest() throws {
7-
#expect(try decode("", as: Data.self) == Data())
8-
#expect(try decode("AQIDBAUGBwg=", as: Data.self) == Data([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
9-
#expect(try decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2ch", as: Data.self)
10-
== Data("The quick brown fox jumped over the lazy dog!".utf8))
11-
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [Data].self)
12-
== ["The", "quick", "brown", "fox"].map { Data($0.utf8) })
7+
typealias T = Data
8+
#expect(try decode("", as: T.self) == T())
9+
#expect(try decode("AQIDBAUGBwg=", as: T.self) == T([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
10+
#expect(try decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2ch", as: T.self)
11+
== T("The quick brown fox jumped over the lazy dog!".utf8))
12+
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [T].self)
13+
== ["The", "quick", "brown", "fox"].map { T($0.utf8) })
1314
#expect(throws: Swift.DecodingError.self) {
14-
try decode(" ", as: Data.self)
15+
try decode(" ", as: T.self)
1516
}
1617
#expect(throws: Swift.DecodingError.self) {
17-
try decode("invalid-base-64", as: Data.self)
18+
try decode("invalid-base-64", as: T.self)
1819
}
1920
#expect(throws: Swift.DecodingError.self) {
20-
try decode(nil, as: Data.self)
21+
try decode(nil, as: T.self)
2122
}
2223
}
2324

2425
@Test func delegatedDataTest() throws {
26+
typealias T = Data
2527
let decoder = EnvironmentDecoder(dataDecodingStrategy: .deferredToData)
26-
#expect(try decode("", as: Data.self, with: decoder) == Data())
27-
#expect(try decode("1,2,3,4,5,6,7,8", as: Data.self, with: decoder) == Data([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
28-
#expect(try decode("0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8", as: Data.self, with: decoder) == Data([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
29-
#expect(try decode("0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08", as: Data.self, with: decoder) == Data([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
30-
#expect(try decode("0x54,0x68,0x65,0x20,0x71,0x75,0x69,0x63,0x6b,0x20,0x62,0x72,0x6f,0x77,0x6e,0x20,0x66,0x6f,0x78,0x20,0x6a,0x75,0x6d,0x70,0x65,0x64,0x20,0x6f,0x76,0x65,0x72,0x20,0x74,0x68,0x65,0x20,0x6c,0x61,0x7a,0x79,0x20,0x64,0x6f,0x67,0x21", as: Data.self, with: decoder)
31-
== Data("The quick brown fox jumped over the lazy dog!".utf8))
32-
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [Data].self)
33-
== ["The", "quick", "brown", "fox"].map { Data($0.utf8) })
28+
#expect(try decode("", as: T.self, with: decoder) == T())
29+
#expect(try decode("1,2,3,4,5,6,7,8", as: T.self, with: decoder) == T([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
30+
#expect(try decode("0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8", as: T.self, with: decoder) == T([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
31+
#expect(try decode("0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08", as: T.self, with: decoder) == T([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
32+
#expect(try decode("0x54,0x68,0x65,0x20,0x71,0x75,0x69,0x63,0x6b,0x20,0x62,0x72,0x6f,0x77,0x6e,0x20,0x66,0x6f,0x78,0x20,0x6a,0x75,0x6d,0x70,0x65,0x64,0x20,0x6f,0x76,0x65,0x72,0x20,0x74,0x68,0x65,0x20,0x6c,0x61,0x7a,0x79,0x20,0x64,0x6f,0x67,0x21", as: T.self, with: decoder)
33+
== T("The quick brown fox jumped over the lazy dog!".utf8))
34+
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [T].self)
35+
== ["The", "quick", "brown", "fox"].map { T($0.utf8) })
3436
#expect(throws: Swift.DecodingError.self) {
35-
try decode(" ", as: Data.self)
37+
try decode(" ", as: T.self)
3638
}
3739
#expect(throws: Swift.DecodingError.self) {
38-
try decode("invalid,int,array", as: Data.self)
40+
try decode("invalid,int,array", as: T.self)
3941
}
4042
#expect(throws: Swift.DecodingError.self) {
4143
try decode(nil, as: Data.self)
@@ -105,69 +107,71 @@ struct EnvironmentDecoderCustomTypeTests {
105107
}
106108

107109
@Test func decimalTest() throws {
110+
typealias T = Decimal
108111
let formatter = Decimal.FormatStyle(locale: .init(identifier: ""))
109112
.precision(.integerAndFractionLength(integerLimits: 1...10, fractionLimits: 0...10))
110113

111-
#expect(try decode("0", as: Decimal.self).formatted(formatter) == "0")
112-
#expect(try decode("+0", as: Decimal.self).formatted(formatter) == "0")
113-
#expect(try decode("-0", as: Decimal.self).formatted(formatter) == "0")
114-
#expect(try decode("3.1415926", as: Decimal.self).formatted(formatter) == "3.1415926")
115-
#expect(try decode("+3.1415926", as: Decimal.self).formatted(formatter) == "3.1415926")
116-
#expect(try decode("-3.1415926", as: Decimal.self).formatted(formatter) == "-3.1415926")
117-
#expect(try decode("3", as: Decimal.self).formatted(formatter) == "3")
118-
#expect(try decode("+3", as: Decimal.self).formatted(formatter) == "3")
119-
#expect(try decode("-3", as: Decimal.self).formatted(formatter) == "-3")
120-
#expect(try decode("2837.5e-2", as: Decimal.self).formatted(formatter) == "28.375")
121-
#expect(try decode("-2837.5e-2", as: Decimal.self).formatted(formatter) == "-28.375")
122-
#expect(try decode(" 5.0", as: Decimal.self).formatted(formatter) == "5")
123-
#expect(try decode("42degrees", as: Decimal.self).formatted(formatter) == "42")
124-
#expect(try decode(" 1.23", as: Decimal.self).formatted(formatter) == "1.23")
125-
#expect(try decode("1.23 ", as: Decimal.self).formatted(formatter) == "1.23")
126-
#expect(try decode("3,3.14,-3", as: [Decimal].self).map { $0.formatted(formatter) } == ["3", "3.14", "-3"])
114+
#expect(try decode("0", as: T.self).formatted(formatter) == "0")
115+
#expect(try decode("+0", as: T.self).formatted(formatter) == "0")
116+
#expect(try decode("-0", as: T.self).formatted(formatter) == "0")
117+
#expect(try decode("3.1415926", as: T.self).formatted(formatter) == "3.1415926")
118+
#expect(try decode("+3.1415926", as: T.self).formatted(formatter) == "3.1415926")
119+
#expect(try decode("-3.1415926", as: T.self).formatted(formatter) == "-3.1415926")
120+
#expect(try decode("3", as: T.self).formatted(formatter) == "3")
121+
#expect(try decode("+3", as: T.self).formatted(formatter) == "3")
122+
#expect(try decode("-3", as: T.self).formatted(formatter) == "-3")
123+
#expect(try decode("2837.5e-2", as: T.self).formatted(formatter) == "28.375")
124+
#expect(try decode("-2837.5e-2", as: T.self).formatted(formatter) == "-28.375")
125+
#expect(try decode(" 5.0", as: T.self).formatted(formatter) == "5")
126+
#expect(try decode("42degrees", as: T.self).formatted(formatter) == "42")
127+
#expect(try decode(" 1.23", as: T.self).formatted(formatter) == "1.23")
128+
#expect(try decode("1.23 ", as: T.self).formatted(formatter) == "1.23")
129+
#expect(try decode("3,3.14,-3", as: [T].self).map { $0.formatted(formatter) } == ["3", "3.14", "-3"])
127130
#expect(throws: Swift.DecodingError.self) {
128-
try decode("inf", as: Decimal.self)
131+
try decode("inf", as: T.self)
129132
}
130133
#expect(throws: Swift.DecodingError.self) {
131-
try decode("infinity", as: Decimal.self)
134+
try decode("infinity", as: T.self)
132135
}
133136
#expect(throws: Swift.DecodingError.self) {
134-
try decode("nan", as: Decimal.self)
137+
try decode("nan", as: T.self)
135138
}
136139
#expect(throws: Swift.DecodingError.self) {
137-
try decode("1.23e17802", as: Decimal.self)
140+
try decode("1.23e17802", as: T.self)
138141
}
139142
#expect(throws: Swift.DecodingError.self) {
140-
try decode("-1.23e17802", as: Decimal.self)
143+
try decode("-1.23e17802", as: T.self)
141144
}
142145
#expect(throws: Swift.DecodingError.self) {
143-
try decode("±2.0", as: Decimal.self)
146+
try decode("±2.0", as: T.self)
144147
}
145148
#expect(throws: Swift.DecodingError.self) {
146-
try decode(nil, as: Decimal.self)
149+
try decode(nil, as: T.self)
147150
}
148151
#expect(throws: Swift.DecodingError.self) {
149-
try decode("", as: Decimal.self)
152+
try decode("", as: T.self)
150153
}
151154
#expect(throws: Swift.DecodingError.self) {
152-
try decode("helloworld", as: Decimal.self)
155+
try decode("helloworld", as: T.self)
153156
}
154157
}
155158

156159
@Test func urlTest() throws {
157-
#expect(try decode("https://example.com", as: URL.self).absoluteString == "https://example.com")
158-
#expect(try decode("example.com", as: URL.self).absoluteString == "example.com")
159-
#expect(try decode("a.example.com,b.example.com", as: [URL].self).map(\.absoluteString) == ["a.example.com", "b.example.com"])
160+
typealias T = URL
161+
#expect(try decode("https://example.com", as: T.self).absoluteString == "https://example.com")
162+
#expect(try decode("example.com", as: T.self).absoluteString == "example.com")
163+
#expect(try decode("a.example.com,b.example.com", as: [T].self).map(\.absoluteString) == ["a.example.com", "b.example.com"])
160164
#expect(throws: Swift.DecodingError.self) {
161-
try decode("", as: URL.self)
165+
try decode("", as: T.self)
162166
}
163167
#expect(throws: Swift.DecodingError.self) {
164-
try decode(nil, as: URL.self)
168+
try decode(nil, as: T.self)
165169
}
166170
#expect(throws: Swift.DecodingError.self) {
167-
try decode(" https://example.com", as: URL.self)
171+
try decode(" https://example.com", as: T.self)
168172
}
169173
#expect(throws: Swift.DecodingError.self) {
170-
try decode("https://example.com ", as: URL.self)
174+
try decode("https://example.com ", as: T.self)
171175
}
172176
}
173177
}

0 commit comments

Comments
 (0)