Skip to content

Commit 2e17867

Browse files
authored
Merge pull request #28378 from JacobMao/fix/SR-11283
SR-11283: Amend OptionSet.remove method
2 parents 3ed0e19 + d5e2533 commit 2e17867

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

stdlib/public/core/OptionSet.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,13 @@ extension OptionSet where Element == Self {
286286
@inlinable // generic-performance
287287
@discardableResult
288288
public mutating func remove(_ member: Element) -> Element? {
289-
let r = isSuperset(of: member) ? Optional(member) : nil
289+
let intersectionElements = intersection(member)
290+
guard !intersectionElements.isEmpty else {
291+
return nil
292+
}
293+
290294
self.subtract(member)
291-
return r
295+
return intersectionElements
292296
}
293297

294298
/// Inserts the given element into the set.

test/stdlib/OptionSetTest.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,40 @@ tests.test("basics") {
8383
expectEqual([.Satchel, .Box], p)
8484
}
8585

86-
// FIXME: add tests for all of SetAlgebra, in particular
87-
// insert/remove/replace.
86+
tests.test("set algebra") {
87+
typealias P = PackagingOptions
88+
89+
// remove
90+
var p = P.BoxOrBag
91+
expectNil(p.remove(P.Carton))
92+
93+
p = P.BoxOrBag
94+
p.remove(P.BoxOrCartonOrBag)
95+
expectEqual(P(), p)
96+
97+
p = P.BoxOrBag
98+
expectEqual(P.Bag, p.remove(P.SatchelOrBag))
99+
expectEqual(P.Box, p)
100+
101+
// insert
102+
p = P.Box
103+
var insertionResult = p.insert(.Bag)
104+
expectTrue(insertionResult.inserted)
105+
expectEqual(P.Bag, insertionResult.memberAfterInsert)
106+
expectEqual(P.BoxOrBag, p)
107+
108+
insertionResult = p.insert(.Bag)
109+
expectFalse(insertionResult.inserted)
110+
expectEqual(P.Bag, insertionResult.memberAfterInsert)
111+
112+
// update
113+
p = P.Box
114+
expectNil(p.update(with: .Bag))
115+
expectEqual(P.BoxOrBag, p)
116+
117+
p = P.Box
118+
expectEqual(P.Box, p.update(with: .BoxOrBag))
119+
expectEqual(P.BoxOrBag, p)
120+
}
88121

89122
runAllTests()

0 commit comments

Comments
 (0)