From 0856854bfcd061d940a5be43394b5cfe0eed13a7 Mon Sep 17 00:00:00 2001 From: Yuki Yoshioka Date: Sat, 23 Mar 2019 14:58:55 +0900 Subject: [PATCH 1/4] [Foundation] Implement `NSSet#description` Because `NSSet#description` was not implemented. --- Docs/Status.md | 2 +- Foundation/NSSet.swift | 43 +++++++++++++++++++++++++++++++--- TestFoundation/TestNSSet.swift | 23 +++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Docs/Status.md b/Docs/Status.md index 5a18734cff..09520852e0 100644 --- a/Docs/Status.md +++ b/Docs/Status.md @@ -166,7 +166,7 @@ There is no _Complete_ status for test coverage because there are always additio | `NSDictionary` | Mostly Complete | Incomplete | `NSCoding` with non-keyed-coding archivers, `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented | | `NSMutableDictionary` | Mostly Complete | Incomplete | `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented | | `NSCFDictionary` | N/A | N/A | For internal use only | - | `NSSet` | Mostly Complete | Incomplete | `description(withLocale:)` and `customMirror` remain unimplemented | + | `NSSet` | Mostly Complete | Incomplete | `customMirror` remain unimplemented | | `NSMutableSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented | | `NSCountedSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented | | `NSCFSet` | N/A | N/A | For internal use only | diff --git a/Foundation/NSSet.swift b/Foundation/NSSet.swift index a64f62d703..0c51e1799b 100644 --- a/Foundation/NSSet.swift +++ b/Foundation/NSSet.swift @@ -107,9 +107,46 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi return true } - open func description(withLocale locale: Locale?) -> String { - // NSUnimplemented() - return description + override open var description: String { + return description(withLocale: nil) + } + + open func description(withLocale locale: Locale?) -> String { + return description(withLocale: locale, indent: 0) + } + + private func description(withLocale locale: Locale?, indent level: Int) -> String { + var descriptions = [String]() + + for obj in self._storage { + if let string = obj as? String { + descriptions.append(string) + } else if let array = obj as? [Any] { + descriptions.append(NSArray(array: array).description(withLocale: locale, indent: level + 1)) + } else if let dict = obj as? [AnyHashable : Any] { + descriptions.append(dict._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1)) + } else if let set = obj as? Set { + descriptions.append(set._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1)) + } else { + descriptions.append("\(obj)") + } + } + var indent = "" + for _ in 0..)) } + + func test_description() { + let array = NSArray(array: ["array_element1", "array_element2"]) + let dictionary = NSDictionary(dictionary: ["key1": "value1", "key2": "value2"]) + let innerSet = NSSet(array: [4444, 5555]) + let set: NSSet = NSSet(array: [array, dictionary, innerSet, 1111, 2222, 3333]) + + let description = set.description + + XCTAssertTrue(String(description.prefix(2)) == "{(") + XCTAssertTrue(String(description.suffix(2)) == ")}") + XCTAssertTrue(description.contains(" (\n array_element1,\n array_element2\n )")) + XCTAssertTrue(description.contains(" key1 = value1")) + XCTAssertTrue(description.contains(" key2 = value2")) + XCTAssertTrue(description.contains(" 4444")) + XCTAssertTrue(description.contains(" 5555")) + XCTAssertTrue(description.contains(" 1111")) + XCTAssertTrue(description.contains(" 2222")) + XCTAssertTrue(description.contains(" 3333")) + } } From 231c8be1d55e688ee6b893df2b2c339ee7aaa7c9 Mon Sep 17 00:00:00 2001 From: Yuki Yoshioka Date: Sat, 23 Mar 2019 19:33:30 +0900 Subject: [PATCH 2/4] Remove variable named `cnt` For following review https://github.com/apple/swift-corelibs-foundation/pull/2037#discussion_r268389699 --- Foundation/NSSet.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Foundation/NSSet.swift b/Foundation/NSSet.swift index 0c51e1799b..86368c2a63 100644 --- a/Foundation/NSSet.swift +++ b/Foundation/NSSet.swift @@ -136,10 +136,9 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi indent += " " } var result = indent + "{(\n" - let cnt = count - for idx in 0.. Date: Sun, 24 Mar 2019 02:11:38 +0900 Subject: [PATCH 3/4] Fix build error of NSSet#description's test on Linux --- TestFoundation/TestNSSet.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TestFoundation/TestNSSet.swift b/TestFoundation/TestNSSet.swift index 112d61f5e9..eb04ded579 100644 --- a/TestFoundation/TestNSSet.swift +++ b/TestFoundation/TestNSSet.swift @@ -245,10 +245,10 @@ class TestNSSet : XCTestCase { let innerSet = NSSet(array: [4444, 5555]) let set: NSSet = NSSet(array: [array, dictionary, innerSet, 1111, 2222, 3333]) - let description = set.description - - XCTAssertTrue(String(description.prefix(2)) == "{(") - XCTAssertTrue(String(description.suffix(2)) == ")}") + let description = NSString(string: set.description) + + XCTAssertTrue(String(description.substring(to: 2)) == "{(") + XCTAssertTrue(String(description.substring(from: description.length - 2)) == ")}") XCTAssertTrue(description.contains(" (\n array_element1,\n array_element2\n )")) XCTAssertTrue(description.contains(" key1 = value1")) XCTAssertTrue(description.contains(" key2 = value2")) From d4414b71d9766d1c9dfb2353dd7631e4fd34c5b3 Mon Sep 17 00:00:00 2001 From: Yuki Yoshioka Date: Sun, 24 Mar 2019 09:34:38 +0900 Subject: [PATCH 4/4] Fix build error of NSSet#description's test on Linux Remove `String` using --- TestFoundation/TestNSSet.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestFoundation/TestNSSet.swift b/TestFoundation/TestNSSet.swift index eb04ded579..47e64d3980 100644 --- a/TestFoundation/TestNSSet.swift +++ b/TestFoundation/TestNSSet.swift @@ -247,8 +247,8 @@ class TestNSSet : XCTestCase { let description = NSString(string: set.description) - XCTAssertTrue(String(description.substring(to: 2)) == "{(") - XCTAssertTrue(String(description.substring(from: description.length - 2)) == ")}") + XCTAssertTrue(description.substring(to: 2).isEqual(to: "{(")) + XCTAssertTrue(description.substring(from: description.length - 2).isEqual(to: ")}")) XCTAssertTrue(description.contains(" (\n array_element1,\n array_element2\n )")) XCTAssertTrue(description.contains(" key1 = value1")) XCTAssertTrue(description.contains(" key2 = value2"))