Updated error handling for Failable instances tests
This commit is contained in:
parent
012fcbec0a
commit
a3d86b2ba7
|
@ -3,29 +3,29 @@ import XCTest
|
|||
|
||||
final class FailableTests: XCTestCase {
|
||||
func testInit()throws {
|
||||
var story: Failable<String, EmptyValidation<String>> = try "Hello world...".failable()
|
||||
var story: NonFailable<String> = "Hello world...".failable()
|
||||
XCTAssertEqual(story.value, "Hello world...")
|
||||
|
||||
story = try "Long long ago...".failable()
|
||||
story = "Long long ago...".failable()
|
||||
XCTAssertEqual(story.value, "Long long ago...")
|
||||
}
|
||||
|
||||
func testSet()throws {
|
||||
var story: Failable<String, EmptyValidation<String>> = try "Hello world...".failable()
|
||||
try story <~ "Long long ago..."
|
||||
var story: NonFailable<String> = "Hello world...".failable()
|
||||
story <~ "Long long ago..."
|
||||
|
||||
XCTAssertEqual(story.value, "Long long ago...")
|
||||
}
|
||||
|
||||
func testKeyPathSubscript()throws {
|
||||
let string = "Hello World"
|
||||
let failable: Failable<String, EmptyValidation<String>> = try string.failable()
|
||||
let failable: NonFailable<String> = string.failable()
|
||||
|
||||
XCTAssertEqual(failable[keyPath: \.count], string.count)
|
||||
}
|
||||
|
||||
func testEncode()throws {
|
||||
let data: Failable<[String: String], EmptyValidation<[String: String]>> = try ["key": "value"].failable()
|
||||
let data = Failable(["key": "value"], EmptyValidation<[String: String]>.self)
|
||||
let json = try String(data: JSONEncoder().encode(data), encoding: .utf8)
|
||||
|
||||
XCTAssertEqual(json, "{\"key\":\"value\"}")
|
||||
|
@ -37,7 +37,7 @@ final class FailableTests: XCTestCase {
|
|||
"key": "value"
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
let object = try JSONDecoder().decode(Failable<[String: String], EmptyValidation<[String: String]>>.self, from: json)
|
||||
let object = try JSONDecoder().decode(NonFailable<[String: String]>.self, from: json)
|
||||
|
||||
XCTAssertEqual(object.value, ["key": "value"])
|
||||
}
|
||||
|
|
|
@ -17,29 +17,32 @@ internal typealias StringLengthArray = ElementValidation<[String], Length1028<St
|
|||
|
||||
final class CollectionTests: XCTestCase {
|
||||
func testLengthValidation()throws {
|
||||
try XCTAssertThrowsError(Failable<[Bool], LengthRange10To1028<[Bool]>>([]))
|
||||
try XCTAssertThrowsError(Failable([], LengthRange10To1028<[Bool]>.self).get())
|
||||
|
||||
var bools = try Failable<[Bool], LengthRange10To1028<[Bool]>>([true, true, true, false, false, false, true, true, false, false])
|
||||
var bools = Failable([true, true, true, false, false, false, true, true, false, false], LengthRange10To1028<[Bool]>.self)
|
||||
XCTAssertEqual(bools.value, [true, true, true, false, false, false, true, true, false, false])
|
||||
|
||||
try XCTAssertThrowsError(bools <~ Array(repeating: true, count: 5))
|
||||
try XCTAssertThrowsError(bools <~ Array(repeating: false, count: 5))
|
||||
try XCTAssertThrowsError(bools <~ Array(repeating: true, count: 1029))
|
||||
try XCTAssertThrowsError(bools <~ Array(repeating: false, count: 1029))
|
||||
bools <~ Array(repeating: true, count: 5)
|
||||
bools <~ Array(repeating: false, count: 5)
|
||||
bools <~ Array(repeating: true, count: 1029)
|
||||
bools <~ Array(repeating: false, count: 1029)
|
||||
try XCTAssertThrowsError(bools.get())
|
||||
|
||||
let array = Array(repeating: true, count: 1028)
|
||||
try bools <~ array
|
||||
bools <~ array
|
||||
XCTAssertEqual(bools.value, array)
|
||||
}
|
||||
|
||||
func testElementValidation()throws {
|
||||
let tooLong = String(repeating: "x", count: 1029)
|
||||
let longest = String(repeating: "g", count: 1028)
|
||||
var strings = try Failable<[String], StringLengthArray>(["G", "D", "A", "E"])
|
||||
var strings = Failable<[String], StringLengthArray>(["G", "D", "A", "E"])
|
||||
XCTAssertEqual(strings.value, ["G", "D", "A", "E"])
|
||||
|
||||
try XCTAssertThrowsError(strings <~ ["G", "O", "O", tooLong])
|
||||
try strings <~ ["G", "OOOO", "World", longest]
|
||||
strings <~ ["G", "O", "O", tooLong]
|
||||
try XCTAssertThrowsError(strings.get())
|
||||
|
||||
strings <~ ["G", "OOOO", "World", longest]
|
||||
XCTAssertEqual(strings.value, ["G", "OOOO", "World", longest])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,34 +24,40 @@ final class ComparableTests: XCTestCase {
|
|||
func testNumberThousand()throws {
|
||||
var int = Failable<Int, NumberThousand>(5_000)
|
||||
|
||||
try int <~ 9_999
|
||||
try int <~ 1_000
|
||||
int <~ 9_999
|
||||
int <~ 1_000
|
||||
try XCTAssertNoThrow(int.get())
|
||||
|
||||
try XCTAssertThrowsError(int <~ 999)
|
||||
try XCTAssertThrowsError(int <~ 10_000)
|
||||
int <~ 999
|
||||
int <~ 10_000
|
||||
try XCTAssertThrowsError(int.get())
|
||||
}
|
||||
|
||||
func testGreaterThan()throws {
|
||||
var int = Failable<Int, GreaterThan>(5_000)
|
||||
|
||||
try int <~ 1_000
|
||||
try int <~ 10_000
|
||||
try int <~ Int.max
|
||||
int <~ 1_000
|
||||
int <~ 10_000
|
||||
int <~ Int.max
|
||||
try XCTAssertNoThrow(int.get())
|
||||
|
||||
try XCTAssertThrowsError(int <~ 999)
|
||||
try XCTAssertThrowsError(int <~ 0)
|
||||
try XCTAssertThrowsError(int <~ Int.min)
|
||||
int <~ 999
|
||||
int <~ 0
|
||||
int <~ Int.min
|
||||
try XCTAssertThrowsError(int.get())
|
||||
}
|
||||
|
||||
func testLessThan()throws {
|
||||
var int = Failable<Int, LessThan>(5_000)
|
||||
|
||||
try int <~ 9_999
|
||||
try int <~ 999
|
||||
try int <~ 0
|
||||
try int <~ Int.min
|
||||
int <~ 9_999
|
||||
int <~ 999
|
||||
int <~ 0
|
||||
int <~ Int.min
|
||||
try XCTAssertNoThrow(int.get())
|
||||
|
||||
try XCTAssertThrowsError(int <~ 10_000)
|
||||
try XCTAssertThrowsError(int <~ Int.max)
|
||||
int <~ 10_000
|
||||
int <~ Int.max
|
||||
try XCTAssertThrowsError(int.get())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,24 +5,28 @@ typealias OptionalStringLength = NotNilValidate<LengthRange10To1028<String>>
|
|||
|
||||
final class OptionalTests: XCTestCase {
|
||||
func testNotNil()throws {
|
||||
var optional = try Failable<Bool?, NotNil<Bool>>.init(false)
|
||||
var optional: Failable<Bool?, NotNil<Bool>> = false
|
||||
|
||||
try optional <~ true
|
||||
try optional <~ false
|
||||
optional <~ true
|
||||
optional <~ false
|
||||
try XCTAssertNoThrow(optional.get())
|
||||
|
||||
try XCTAssertThrowsError(optional <~ nil)
|
||||
optional <~ nil
|
||||
try XCTAssertThrowsError(optional.get())
|
||||
}
|
||||
|
||||
func testNotNilValidate()throws {
|
||||
var optional = try Failable<String?, OptionalStringLength>.init("Hello World")
|
||||
var optional: Failable<String?, OptionalStringLength> = "Hello World"
|
||||
|
||||
try optional <~ "Long long ago"
|
||||
try optional <~ String(repeating: "x", count: 10)
|
||||
try optional <~ String(repeating: "x", count: 1028)
|
||||
try optional <~ nil
|
||||
optional <~ "Long long ago"
|
||||
optional <~ String(repeating: "x", count: 10)
|
||||
optional <~ String(repeating: "x", count: 1028)
|
||||
optional <~ nil
|
||||
try XCTAssertNoThrow(optional.get())
|
||||
|
||||
try XCTAssertThrowsError(optional <~ String(repeating: "x", count: 9))
|
||||
try XCTAssertThrowsError(optional <~ String(repeating: "x", count: 1029))
|
||||
optional <~ String(repeating: "x", count: 9)
|
||||
optional <~ String(repeating: "x", count: 1029)
|
||||
try XCTAssertThrowsError(optional.get())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,16 +7,18 @@ internal struct USPhoneNumber: RegexValidation {
|
|||
|
||||
final class StringTests: XCTestCase {
|
||||
func testUSPhoneNumber()throws {
|
||||
var number: Failable<String, USPhoneNumber> = try "731-943-4316".failable()
|
||||
var number: Failable<String, USPhoneNumber> = "731-943-4316".failable()
|
||||
|
||||
try number <~ "(731)-943-4316"
|
||||
try number <~ "1-731-943-4316"
|
||||
try number <~ "7319434316"
|
||||
number <~ "(731)-943-4316"
|
||||
number <~ "1-731-943-4316"
|
||||
number <~ "7319434316"
|
||||
try XCTAssertNoThrow(number.get())
|
||||
|
||||
try XCTAssertThrowsError(number <~ "")
|
||||
try XCTAssertThrowsError(number <~ "943-4316")
|
||||
try XCTAssertThrowsError(number <~ "1-800-EAT-MEAT")
|
||||
try XCTAssertThrowsError(number <~ "4316-943-731")
|
||||
number <~ ""
|
||||
number <~ "943-4316"
|
||||
number <~ "1-800-EAT-MEAT"
|
||||
number <~ "4316-943-731"
|
||||
try XCTAssertThrowsError(number.get())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue