|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +class TestUUID : XCTestCase { |
| 11 | + |
| 12 | + static var allTests: [(String, (TestUUID) -> () throws -> Void)] { |
| 13 | + return [ |
| 14 | + ("test_UUIDEquality", test_UUIDEquality), |
| 15 | + ("test_UUIDInvalid", test_UUIDInvalid), |
| 16 | + ("test_UUIDuuidString", test_UUIDuuidString), |
| 17 | + ("test_UUIDdescription", test_UUIDdescription), |
| 18 | + ("test_UUIDNSCoding", test_UUIDNSCoding), |
| 19 | + ] |
| 20 | + } |
| 21 | + |
| 22 | + func test_UUIDEquality() { |
| 23 | + let uuidA = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F") |
| 24 | + let uuidB = UUID(uuidString: "e621e1f8-c36c-495a-93fc-0c247a3e6e5f") |
| 25 | + let uuidC = UUID(uuid: (0xe6,0x21,0xe1,0xf8,0xc3,0x6c,0x49,0x5a,0x93,0xfc,0x0c,0x24,0x7a,0x3e,0x6e,0x5f)) |
| 26 | + let uuidD = UUID() |
| 27 | + |
| 28 | + XCTAssertEqual(uuidA, uuidB, "String case must not matter.") |
| 29 | + XCTAssertEqual(uuidA, uuidC, "A UUID initialized with a string must be equal to the same UUID initialized with its UnsafePointer<UInt8> equivalent representation.") |
| 30 | + XCTAssertNotEqual(uuidC, uuidD, "Two different UUIDs must not be equal.") |
| 31 | + } |
| 32 | + |
| 33 | + func test_UUIDInvalid() { |
| 34 | + let uuid = UUID(uuidString: "Invalid UUID") |
| 35 | + XCTAssertNil(uuid, "The convenience initializer `init?(uuidString string:)` must return nil for an invalid UUID string.") |
| 36 | + } |
| 37 | + |
| 38 | + // `uuidString` should return an uppercase string |
| 39 | + // See: https://bugs.swift.org/browse/SR-865 |
| 40 | + func test_UUIDuuidString() { |
| 41 | + let uuid = UUID(uuid: (0xe6,0x21,0xe1,0xf8,0xc3,0x6c,0x49,0x5a,0x93,0xfc,0x0c,0x24,0x7a,0x3e,0x6e,0x5f)) |
| 42 | + XCTAssertEqual(uuid.uuidString, "E621E1F8-C36C-495A-93FC-0C247A3E6E5F", "The uuidString representation must be uppercase.") |
| 43 | + } |
| 44 | + |
| 45 | + func test_UUIDdescription() { |
| 46 | + let uuid = UUID() |
| 47 | + XCTAssertEqual(uuid.description, uuid.uuidString, "The description must be the same as the uuidString.") |
| 48 | + } |
| 49 | + |
| 50 | + func test_UUIDNSCoding() { |
| 51 | + let uuidA = UUID() |
| 52 | + let uuidB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: uuidA)) as! UUID |
| 53 | + XCTAssertEqual(uuidA, uuidB, "Archived then unarchived uuid must be equal.") |
| 54 | + } |
| 55 | +} |
0 commit comments