Skip to content

Commit 76fe3ec

Browse files
authored
Merge pull request #2220 from millenomi/nsset-nscoding
2 parents e74e8ce + 067e5ce commit 76fe3ec

File tree

7 files changed

+85
-27
lines changed

7 files changed

+85
-27
lines changed

Foundation/NSSet.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,28 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
8686
self.init(array: [object])
8787
}
8888

89-
public required convenience init?(coder aDecoder: NSCoder) {
89+
internal class func _objects(from aDecoder: NSCoder) -> [NSObject] {
9090
guard aDecoder.allowsKeyedCoding else {
9191
preconditionFailure("Unkeyed coding is unsupported.")
9292
}
9393
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
9494
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
95-
self.init(array: objects as! [NSObject])
95+
return objects as! [NSObject]
9696
} else {
97-
var objects = [AnyObject]()
97+
var objects: [NSObject] = []
9898
var count = 0
9999
while let object = aDecoder.decodeObject(forKey: "NS.object.\(count)") {
100100
objects.append(object as! NSObject)
101101
count += 1
102102
}
103-
self.init(array: objects)
103+
return objects
104104
}
105105
}
106106

107+
public required convenience init?(coder aDecoder: NSCoder) {
108+
self.init(array: NSSet._objects(from: aDecoder))
109+
}
110+
107111
open func encode(with aCoder: NSCoder) {
108112
// The encoding of a NSSet is identical to the encoding of an NSArray of its contents
109113
self.allObjects._nsObject.encode(with: aCoder)
@@ -380,7 +384,7 @@ open class NSMutableSet : NSSet {
380384
}
381385

382386
public required convenience init?(coder aDecoder: NSCoder) {
383-
NSUnimplemented()
387+
self.init(array: NSSet._objects(from: aDecoder))
384388
}
385389

386390
open func addObjects(from array: [Any]) {

TestFoundation/FixtureValues.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ enum Fixtures {
200200
return (try Fixtures.indexSetManyRanges.make()).mutableCopy() as! NSMutableIndexSet
201201
}
202202

203+
// ===== NSSet, NSMutableSet =====
204+
205+
static let setOfNumbers = TypedFixture<NSSet>("NSSet-Numbers") {
206+
let numbers = [1, 2, 3, 4, 5].map { NSNumber(value: $0) }
207+
return NSSet(array: numbers)
208+
}
209+
210+
static let setEmpty = TypedFixture<NSSet>("NSSet-Empty") {
211+
return NSSet()
212+
}
213+
214+
static let mutableSetOfNumbers = TypedFixture<NSMutableSet>("NSMutableSet-Numbers") {
215+
let numbers = [1, 2, 3, 4, 5].map { NSNumber(value: $0) }
216+
return NSMutableSet(array: numbers)
217+
}
218+
219+
static let mutableSetEmpty = TypedFixture<NSMutableSet>("NSMutableSet-Empty") {
220+
return NSMutableSet()
221+
}
222+
203223
// ===== Fixture list =====
204224

205225
static let _listOfAllFixtures: [AnyFixture] = [
@@ -221,6 +241,10 @@ enum Fixtures {
221241
AnyFixture(Fixtures.mutableIndexSetEmpty),
222242
AnyFixture(Fixtures.mutableIndexSetOneRange),
223243
AnyFixture(Fixtures.mutableIndexSetManyRanges),
244+
AnyFixture(Fixtures.setOfNumbers),
245+
AnyFixture(Fixtures.setEmpty),
246+
AnyFixture(Fixtures.mutableSetOfNumbers),
247+
AnyFixture(Fixtures.mutableSetEmpty),
224248
]
225249

226250
// This ensures that we do not have fixtures with duplicate identifiers:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

TestFoundation/TestNSSet.swift

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@
88
//
99

1010
class TestNSSet : XCTestCase {
11-
12-
static var allTests: [(String, (TestNSSet) -> () throws -> Void)] {
13-
return [
14-
("test_BasicConstruction", test_BasicConstruction),
15-
("testInitWithSet", testInitWithSet),
16-
("test_enumeration", test_enumeration),
17-
("test_sequenceType", test_sequenceType),
18-
("test_setOperations", test_setOperations),
19-
("test_equality", test_equality),
20-
("test_copying", test_copying),
21-
("test_mutableCopying", test_mutableCopying),
22-
("test_CountedSetBasicConstruction", test_CountedSetBasicConstruction),
23-
("test_CountedSetObjectCount", test_CountedSetObjectCount),
24-
("test_CountedSetAddObject", test_CountedSetAddObject),
25-
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
26-
("test_CountedSetCopying", test_CountedSetCopying),
27-
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
28-
("test_Subsets", test_Subsets),
29-
("test_description", test_description)
30-
]
31-
}
32-
3311
func test_BasicConstruction() {
3412
let set = NSSet()
3513
let set2 = NSSet(array: ["foo", "bar"])
@@ -271,4 +249,56 @@ class TestNSSet : XCTestCase {
271249
XCTAssertTrue(description.contains(" 2222"))
272250
XCTAssertTrue(description.contains(" 3333"))
273251
}
252+
253+
let setFixtures = [
254+
Fixtures.setOfNumbers,
255+
Fixtures.setEmpty,
256+
]
257+
258+
let mutableSetFixtures = [
259+
Fixtures.mutableSetOfNumbers,
260+
Fixtures.mutableSetEmpty,
261+
]
262+
263+
func test_codingRoundtrip() throws {
264+
for fixture in setFixtures {
265+
try fixture.assertValueRoundtripsInCoder()
266+
}
267+
for fixture in mutableSetFixtures {
268+
try fixture.assertValueRoundtripsInCoder()
269+
}
270+
}
271+
272+
func test_loadedValuesMatch() throws {
273+
for fixture in setFixtures {
274+
try fixture.assertLoadedValuesMatch()
275+
}
276+
for fixture in mutableSetFixtures {
277+
try fixture.assertLoadedValuesMatch()
278+
}
279+
}
280+
281+
static var allTests: [(String, (TestNSSet) -> () throws -> Void)] {
282+
return [
283+
("test_BasicConstruction", test_BasicConstruction),
284+
("testInitWithSet", testInitWithSet),
285+
("test_enumeration", test_enumeration),
286+
("test_sequenceType", test_sequenceType),
287+
("test_setOperations", test_setOperations),
288+
("test_equality", test_equality),
289+
("test_copying", test_copying),
290+
("test_mutableCopying", test_mutableCopying),
291+
("test_CountedSetBasicConstruction", test_CountedSetBasicConstruction),
292+
("test_CountedSetObjectCount", test_CountedSetObjectCount),
293+
("test_CountedSetAddObject", test_CountedSetAddObject),
294+
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
295+
("test_CountedSetCopying", test_CountedSetCopying),
296+
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
297+
("test_Subsets", test_Subsets),
298+
("test_description", test_description),
299+
("test_codingRoundtrip", test_codingRoundtrip),
300+
("test_loadedValuesMatch", test_loadedValuesMatch),
301+
]
302+
}
303+
274304
}

0 commit comments

Comments
 (0)