Add more unit tests

This commit is contained in:
Yasuhiro Hatta 2019-07-06 19:19:49 +09:00
parent 3465c586cf
commit 00e95c021a
1 changed files with 36 additions and 4 deletions

View File

@ -61,6 +61,11 @@ final class ULIDTests: XCTestCase {
XCTAssertEqual(expected, actual!.ulidString) XCTAssertEqual(expected, actual!.ulidString)
} }
func testParseULIDStringError() {
let zero = ""
XCTAssertNil(ULID(ulidString: zero))
}
func testParseULIDData() { func testParseULIDData() {
let expected: [UInt8] = [ let expected: [UInt8] = [
0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0xE5, 0x2D, 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0xE5, 0x2D,
@ -73,6 +78,11 @@ final class ULIDTests: XCTestCase {
XCTAssertEqual(expected, Array(actual!.ulidData)) XCTAssertEqual(expected, Array(actual!.ulidData))
} }
func testParseULIDDataError() {
let zero = Data()
XCTAssertNil(ULID(ulidData: zero))
}
func testULIDDataLength() { func testULIDDataLength() {
let ulid = ULID() let ulid = ULID()
XCTAssertEqual(16, ulid.ulidData.count) XCTAssertEqual(16, ulid.ulidData.count)
@ -127,10 +137,10 @@ final class ULIDTests: XCTestCase {
} }
func testComparable1() { func testComparable1() {
let lhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) let lhs = ULID(ulid: (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
let rhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)) let rhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
XCTAssertTrue(lhs < rhs) XCTAssertFalse(lhs < rhs)
XCTAssertFalse(lhs > rhs) XCTAssertTrue(lhs > rhs)
} }
func testComparable2() { func testComparable2() {
@ -141,6 +151,13 @@ final class ULIDTests: XCTestCase {
} }
func testComparable3() { func testComparable3() {
let lhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
let rhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1))
XCTAssertTrue(lhs < rhs)
XCTAssertFalse(lhs > rhs)
}
func testComparable4() {
let now = Date() let now = Date()
let ulid0 = ULID(timestamp: now.addingTimeInterval(-120)) let ulid0 = ULID(timestamp: now.addingTimeInterval(-120))
let ulid1 = ULID(timestamp: now.addingTimeInterval(-60)) let ulid1 = ULID(timestamp: now.addingTimeInterval(-60))
@ -179,6 +196,21 @@ final class ULIDTests: XCTestCase {
} }
} }
func testDecodableError() {
let json = """
{ "ulid" : "" }
"""
do {
let decoder = JSONDecoder()
_ = try decoder.decode(CodableModel.self, from: json.data(using: .utf8)!)
XCTFail()
} catch DecodingError.dataCorrupted {
// Success
} catch {
XCTFail()
}
}
func testEncodable() { func testEncodable() {
let ulidString = "01D0YHEWR9WMPY4NNTPK1MR1TQ" let ulidString = "01D0YHEWR9WMPY4NNTPK1MR1TQ"
let expected = """ let expected = """