Skip to content

Commit f033eaa

Browse files
authored
Merge pull request #1792 from pvieito/patch-1
UUID.uuidString should return an upper String like on Darwin
2 parents 60651e2 + 8350aa8 commit f033eaa

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ if(ENABLE_TESTING)
418418
TestFoundation/TestURL.swift
419419
TestFoundation/TestUserDefaults.swift
420420
TestFoundation/TestUtils.swift
421+
TestFoundation/TestUUID.swift
421422
TestFoundation/TestXMLDocument.swift
422423
TestFoundation/TestXMLParser.swift
423424
CFLAGS

Foundation/UUID.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
6666
valPtr.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<uuid_t>.size) { val in
6767
withUnsafeMutablePointer(to: &bytes) { strPtr in
6868
strPtr.withMemoryRebound(to: CChar.self, capacity: MemoryLayout<uuid_string_t>.size) { str in
69-
_cf_uuid_unparse(val, str)
69+
_cf_uuid_unparse_upper(val, str)
7070
return String(cString: str, encoding: .utf8)!
7171
}
7272
}

TestFoundation/TestUUID.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ XCTMain([
8787
testCase(TestHTTPURLResponse.allTests),
8888
testCase(TestURLSession.allTests),
8989
testCase(TestNSUUID.allTests),
90+
testCase(TestUUID.allTests),
9091
testCase(TestNSValue.allTests),
9192
testCase(TestUserDefaults.allTests),
9293
testCase(TestXMLParser.allTests),

0 commit comments

Comments
 (0)