Skip to content

Parity: NSCoding: NSSet #2220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,28 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
self.init(array: [object])
}

public required convenience init?(coder aDecoder: NSCoder) {
internal class func _objects(from aDecoder: NSCoder) -> [NSObject] {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(array: objects as! [NSObject])
return objects as! [NSObject]
} else {
var objects = [AnyObject]()
var objects: [NSObject] = []
var count = 0
while let object = aDecoder.decodeObject(forKey: "NS.object.\(count)") {
objects.append(object as! NSObject)
count += 1
}
self.init(array: objects)
return objects
}
}

public required convenience init?(coder aDecoder: NSCoder) {
self.init(array: NSSet._objects(from: aDecoder))
}

open func encode(with aCoder: NSCoder) {
// The encoding of a NSSet is identical to the encoding of an NSArray of its contents
self.allObjects._nsObject.encode(with: aCoder)
Expand Down Expand Up @@ -380,7 +384,7 @@ open class NSMutableSet : NSSet {
}

public required convenience init?(coder aDecoder: NSCoder) {
NSUnimplemented()
self.init(array: NSSet._objects(from: aDecoder))
}

open func addObjects(from array: [Any]) {
Expand Down
24 changes: 24 additions & 0 deletions TestFoundation/FixtureValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ enum Fixtures {
return (try Fixtures.indexSetManyRanges.make()).mutableCopy() as! NSMutableIndexSet
}

// ===== NSSet, NSMutableSet =====

static let setOfNumbers = TypedFixture<NSSet>("NSSet-Numbers") {
let numbers = [1, 2, 3, 4, 5].map { NSNumber(value: $0) }
return NSSet(array: numbers)
}

static let setEmpty = TypedFixture<NSSet>("NSSet-Empty") {
return NSSet()
}

static let mutableSetOfNumbers = TypedFixture<NSMutableSet>("NSMutableSet-Numbers") {
let numbers = [1, 2, 3, 4, 5].map { NSNumber(value: $0) }
return NSMutableSet(array: numbers)
}

static let mutableSetEmpty = TypedFixture<NSMutableSet>("NSMutableSet-Empty") {
return NSMutableSet()
}

// ===== Fixture list =====

static let _listOfAllFixtures: [AnyFixture] = [
Expand All @@ -221,6 +241,10 @@ enum Fixtures {
AnyFixture(Fixtures.mutableIndexSetEmpty),
AnyFixture(Fixtures.mutableIndexSetOneRange),
AnyFixture(Fixtures.mutableIndexSetManyRanges),
AnyFixture(Fixtures.setOfNumbers),
AnyFixture(Fixtures.setEmpty),
AnyFixture(Fixtures.mutableSetOfNumbers),
AnyFixture(Fixtures.mutableSetEmpty),
]

// This ensures that we do not have fixtures with duplicate identifiers:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
74 changes: 52 additions & 22 deletions TestFoundation/TestNSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@
//

class TestNSSet : XCTestCase {

static var allTests: [(String, (TestNSSet) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("testInitWithSet", testInitWithSet),
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_setOperations", test_setOperations),
("test_equality", test_equality),
("test_copying", test_copying),
("test_mutableCopying", test_mutableCopying),
("test_CountedSetBasicConstruction", test_CountedSetBasicConstruction),
("test_CountedSetObjectCount", test_CountedSetObjectCount),
("test_CountedSetAddObject", test_CountedSetAddObject),
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
("test_CountedSetCopying", test_CountedSetCopying),
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
("test_Subsets", test_Subsets),
("test_description", test_description)
]
}

func test_BasicConstruction() {
let set = NSSet()
let set2 = NSSet(array: ["foo", "bar"])
Expand Down Expand Up @@ -271,4 +249,56 @@ class TestNSSet : XCTestCase {
XCTAssertTrue(description.contains(" 2222"))
XCTAssertTrue(description.contains(" 3333"))
}

let setFixtures = [
Fixtures.setOfNumbers,
Fixtures.setEmpty,
]

let mutableSetFixtures = [
Fixtures.mutableSetOfNumbers,
Fixtures.mutableSetEmpty,
]

func test_codingRoundtrip() throws {
for fixture in setFixtures {
try fixture.assertValueRoundtripsInCoder()
}
for fixture in mutableSetFixtures {
try fixture.assertValueRoundtripsInCoder()
}
}

func test_loadedValuesMatch() throws {
for fixture in setFixtures {
try fixture.assertLoadedValuesMatch()
}
for fixture in mutableSetFixtures {
try fixture.assertLoadedValuesMatch()
}
}

static var allTests: [(String, (TestNSSet) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("testInitWithSet", testInitWithSet),
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_setOperations", test_setOperations),
("test_equality", test_equality),
("test_copying", test_copying),
("test_mutableCopying", test_mutableCopying),
("test_CountedSetBasicConstruction", test_CountedSetBasicConstruction),
("test_CountedSetObjectCount", test_CountedSetObjectCount),
("test_CountedSetAddObject", test_CountedSetAddObject),
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
("test_CountedSetCopying", test_CountedSetCopying),
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
("test_Subsets", test_Subsets),
("test_description", test_description),
("test_codingRoundtrip", test_codingRoundtrip),
("test_loadedValuesMatch", test_loadedValuesMatch),
]
}

}