Skip to content

Commit

Permalink
use typealias to increase test maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeem committed Aug 2, 2024
1 parent 9b14e19 commit e6e7b88
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 338 deletions.
106 changes: 55 additions & 51 deletions Tests/EnvironmentDecoderTests/EnvironmentDecoderCustomTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@ import Testing

struct EnvironmentDecoderCustomTypeTests {
@Test func base64DataTest() throws {
#expect(try decode("", as: Data.self) == Data())
#expect(try decode("AQIDBAUGBwg=", as: Data.self) == Data([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
#expect(try decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2ch", as: Data.self)
== Data("The quick brown fox jumped over the lazy dog!".utf8))
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [Data].self)
== ["The", "quick", "brown", "fox"].map { Data($0.utf8) })
typealias T = Data
#expect(try decode("", as: T.self) == T())
#expect(try decode("AQIDBAUGBwg=", as: T.self) == T([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
#expect(try decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2ch", as: T.self)
== T("The quick brown fox jumped over the lazy dog!".utf8))
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [T].self)
== ["The", "quick", "brown", "fox"].map { T($0.utf8) })
#expect(throws: Swift.DecodingError.self) {
try decode(" ", as: Data.self)
try decode(" ", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("invalid-base-64", as: Data.self)
try decode("invalid-base-64", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode(nil, as: Data.self)
try decode(nil, as: T.self)
}
}

@Test func delegatedDataTest() throws {
typealias T = Data
let decoder = EnvironmentDecoder(dataDecodingStrategy: .deferredToData)
#expect(try decode("", as: Data.self, with: decoder) == Data())
#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]))
#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]))
#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]))
#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)
== Data("The quick brown fox jumped over the lazy dog!".utf8))
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [Data].self)
== ["The", "quick", "brown", "fox"].map { Data($0.utf8) })
#expect(try decode("", as: T.self, with: decoder) == T())
#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]))
#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]))
#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]))
#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)
== T("The quick brown fox jumped over the lazy dog!".utf8))
#expect(try decode("VGhl,cXVpY2s=,YnJvd24=,Zm94", as: [T].self)
== ["The", "quick", "brown", "fox"].map { T($0.utf8) })
#expect(throws: Swift.DecodingError.self) {
try decode(" ", as: Data.self)
try decode(" ", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("invalid,int,array", as: Data.self)
try decode("invalid,int,array", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode(nil, as: Data.self)
Expand Down Expand Up @@ -105,69 +107,71 @@ struct EnvironmentDecoderCustomTypeTests {
}

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

#expect(try decode("0", as: Decimal.self).formatted(formatter) == "0")
#expect(try decode("+0", as: Decimal.self).formatted(formatter) == "0")
#expect(try decode("-0", as: Decimal.self).formatted(formatter) == "0")
#expect(try decode("3.1415926", as: Decimal.self).formatted(formatter) == "3.1415926")
#expect(try decode("+3.1415926", as: Decimal.self).formatted(formatter) == "3.1415926")
#expect(try decode("-3.1415926", as: Decimal.self).formatted(formatter) == "-3.1415926")
#expect(try decode("3", as: Decimal.self).formatted(formatter) == "3")
#expect(try decode("+3", as: Decimal.self).formatted(formatter) == "3")
#expect(try decode("-3", as: Decimal.self).formatted(formatter) == "-3")
#expect(try decode("2837.5e-2", as: Decimal.self).formatted(formatter) == "28.375")
#expect(try decode("-2837.5e-2", as: Decimal.self).formatted(formatter) == "-28.375")
#expect(try decode(" 5.0", as: Decimal.self).formatted(formatter) == "5")
#expect(try decode("42degrees", as: Decimal.self).formatted(formatter) == "42")
#expect(try decode(" 1.23", as: Decimal.self).formatted(formatter) == "1.23")
#expect(try decode("1.23 ", as: Decimal.self).formatted(formatter) == "1.23")
#expect(try decode("3,3.14,-3", as: [Decimal].self).map { $0.formatted(formatter) } == ["3", "3.14", "-3"])
#expect(try decode("0", as: T.self).formatted(formatter) == "0")
#expect(try decode("+0", as: T.self).formatted(formatter) == "0")
#expect(try decode("-0", as: T.self).formatted(formatter) == "0")
#expect(try decode("3.1415926", as: T.self).formatted(formatter) == "3.1415926")
#expect(try decode("+3.1415926", as: T.self).formatted(formatter) == "3.1415926")
#expect(try decode("-3.1415926", as: T.self).formatted(formatter) == "-3.1415926")
#expect(try decode("3", as: T.self).formatted(formatter) == "3")
#expect(try decode("+3", as: T.self).formatted(formatter) == "3")
#expect(try decode("-3", as: T.self).formatted(formatter) == "-3")
#expect(try decode("2837.5e-2", as: T.self).formatted(formatter) == "28.375")
#expect(try decode("-2837.5e-2", as: T.self).formatted(formatter) == "-28.375")
#expect(try decode(" 5.0", as: T.self).formatted(formatter) == "5")
#expect(try decode("42degrees", as: T.self).formatted(formatter) == "42")
#expect(try decode(" 1.23", as: T.self).formatted(formatter) == "1.23")
#expect(try decode("1.23 ", as: T.self).formatted(formatter) == "1.23")
#expect(try decode("3,3.14,-3", as: [T].self).map { $0.formatted(formatter) } == ["3", "3.14", "-3"])
#expect(throws: Swift.DecodingError.self) {
try decode("inf", as: Decimal.self)
try decode("inf", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("infinity", as: Decimal.self)
try decode("infinity", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("nan", as: Decimal.self)
try decode("nan", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("1.23e17802", as: Decimal.self)
try decode("1.23e17802", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("-1.23e17802", as: Decimal.self)
try decode("-1.23e17802", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("±2.0", as: Decimal.self)
try decode("±2.0", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode(nil, as: Decimal.self)
try decode(nil, as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("", as: Decimal.self)
try decode("", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("helloworld", as: Decimal.self)
try decode("helloworld", as: T.self)
}
}

@Test func urlTest() throws {
#expect(try decode("https://example.com", as: URL.self).absoluteString == "https://example.com")
#expect(try decode("example.com", as: URL.self).absoluteString == "example.com")
#expect(try decode("a.example.com,b.example.com", as: [URL].self).map(\.absoluteString) == ["a.example.com", "b.example.com"])
typealias T = URL
#expect(try decode("https://example.com", as: T.self).absoluteString == "https://example.com")
#expect(try decode("example.com", as: T.self).absoluteString == "example.com")
#expect(try decode("a.example.com,b.example.com", as: [T].self).map(\.absoluteString) == ["a.example.com", "b.example.com"])
#expect(throws: Swift.DecodingError.self) {
try decode("", as: URL.self)
try decode("", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode(nil, as: URL.self)
try decode(nil, as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode(" https://example.com", as: URL.self)
try decode(" https://example.com", as: T.self)
}
#expect(throws: Swift.DecodingError.self) {
try decode("https://example.com ", as: URL.self)
try decode("https://example.com ", as: T.self)
}
}
}
Loading

0 comments on commit e6e7b88

Please sign in to comment.