Skip to content

CharacterSet : Codable implementation #1127

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 7 commits into from
Jul 27, 2017
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
25 changes: 24 additions & 1 deletion Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ internal final class _SwiftNSCharacterSet : NSCharacterSet, _SwiftNativeFoundati
override func isSuperset(of other: CharacterSet) -> Bool {
return _mapUnmanaged { $0.isSuperset(of: other) }
}

override var _cfObject: CFType {
// We cannot inherit super's unsafeBitCast(self, to: CFType.self) here, because layout of _SwiftNSCharacterSet
// is not compatible with CFCharacterSet. We need to bitcast the underlying NSCharacterSet instead.
return _mapUnmanaged { unsafeBitCast($0, to: CFType.self) }
}
}

/**
Expand Down Expand Up @@ -469,7 +475,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb

/// Returns true if the two `CharacterSet`s are equal.
public static func ==(lhs : CharacterSet, rhs: CharacterSet) -> Bool {
return lhs._wrapped.isEqual(rhs._bridgeToObjectiveC()) // TODO: mlehew - as NSCharacterSet
return lhs._mapUnmanaged { $0.isEqual(rhs) }
}
}

Expand Down Expand Up @@ -503,3 +509,20 @@ extension CharacterSet : _ObjectTypeBridgeable {
}

}

extension CharacterSet : Codable {
private enum CodingKeys : Int, CodingKey {
case bitmap
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let bitmap = try container.decode(Data.self, forKey: .bitmap)
self.init(bitmapRepresentation: bitmap)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.bitmapRepresentation, forKey: .bitmap)
}
}
27 changes: 27 additions & 0 deletions TestFoundation/TestCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,32 @@ class TestCodable : XCTestCase {
expectRoundTripEqualityThroughJSON(for: rect)
}
}

// MARK: - CharacterSet
lazy var characterSetValues: [CharacterSet] = [
CharacterSet.controlCharacters,
CharacterSet.whitespaces,
CharacterSet.whitespacesAndNewlines,
CharacterSet.decimalDigits,
CharacterSet.letters,
CharacterSet.lowercaseLetters,
CharacterSet.uppercaseLetters,
CharacterSet.nonBaseCharacters,
CharacterSet.alphanumerics,
CharacterSet.decomposables,
CharacterSet.illegalCharacters,
CharacterSet.punctuationCharacters,
CharacterSet.capitalizedLetters,
CharacterSet.symbols,
CharacterSet.newlines,
CharacterSet(charactersIn: "abcd")
]

func test_CharacterSet_JSON() {
for characterSet in characterSetValues {
expectRoundTripEqualityThroughJSON(for: characterSet)
}
}

}

Expand All @@ -286,6 +312,7 @@ extension TestCodable {
("test_CGPoint_JSON", test_CGPoint_JSON),
("test_CGSize_JSON", test_CGSize_JSON),
("test_CGRect_JSON", test_CGRect_JSON),
("test_CharacterSet_JSON", test_CharacterSet_JSON),
]
}
}
82 changes: 64 additions & 18 deletions TestFoundation/TestNSCharacterSet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This source file is part of the Swift.org open source project
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
Expand All @@ -7,8 +7,6 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//



#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
Expand All @@ -17,7 +15,47 @@ import SwiftFoundation
import SwiftXCTest
#endif

private struct Box: Equatable {
private let ns: NSCharacterSet
private let swift: CharacterSet

private init(ns: NSCharacterSet, swift: CharacterSet) {
self.ns = ns
self.swift = swift
}

init(charactersIn string: String) {
self.ns = NSCharacterSet(charactersIn: string)
self.swift = CharacterSet(charactersIn: string)
}

static var alphanumerics: Box {
return Box(ns: NSCharacterSet.alphanumerics._bridgeToObjectiveC(),
swift: CharacterSet.alphanumerics)
}

static var decimalDigits: Box {
return Box(ns: NSCharacterSet.decimalDigits._bridgeToObjectiveC(),
swift: CharacterSet.decimalDigits)
}

// MARK: Equatable

static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.ns == rhs.ns
&& lhs.swift == rhs.swift
&& lhs.ns._bridgeToSwift() == rhs.ns._bridgeToSwift()
&& lhs.swift._bridgeToObjectiveC() == rhs.swift._bridgeToObjectiveC()
&& lhs.ns.isEqual(rhs.ns)
&& lhs.ns.isEqual(rhs.swift)
&& lhs.ns.isEqual(rhs.ns._bridgeToSwift())
&& lhs.ns.isEqual(rhs.swift._bridgeToObjectiveC())
&& lhs.swift._bridgeToObjectiveC().isEqual(rhs.ns)
&& lhs.swift._bridgeToObjectiveC().isEqual(rhs.swift)
&& lhs.swift._bridgeToObjectiveC().isEqual(rhs.ns._bridgeToSwift())
&& lhs.swift._bridgeToObjectiveC().isEqual(rhs.swift._bridgeToObjectiveC())
}
}

class TestNSCharacterSet : XCTestCase {

Expand Down Expand Up @@ -283,28 +321,36 @@ class TestNSCharacterSet : XCTestCase {
let expected = CharacterSet(charactersIn: "abc")
XCTAssertEqual(expected, symmetricDifference)
}

func test_Equatable() {
XCTAssertEqual(NSCharacterSet(charactersIn: ""), NSCharacterSet(charactersIn: ""))
XCTAssertEqual(NSCharacterSet(charactersIn: "a"), NSCharacterSet(charactersIn: "a"))
XCTAssertEqual(NSCharacterSet(charactersIn: "ab"), NSCharacterSet(charactersIn: "ab"))

XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: "123"))
XCTAssertNotEqual(NSCharacterSet(charactersIn: "123"), NSCharacterSet(charactersIn: "abc"))

XCTAssertNotEqual(NSCharacterSet(charactersIn: ""), nil)

let equalPairs = [
("", ""),
("a", "a"),
("abcde", "abcde"),
("12345", "12345")
]

/*
Tests disabled due to CoreFoundation bug?
These NSCharacterSet pairs are (wrongly?) evaluated to be equal. Same behaviour can be observed on macOS 10.12.
Interestingly, on iOS 11 Simulator, they are evaluted to be _not_ equal,
while on iOS 10.3.1 Simulator, they are evaluted to be equal.
*/
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "ab"), NSCharacterSet(charactersIn: "abc"))
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: "ab"))
// XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: ""))
// XCTAssertNotEqual(NSCharacterSet(charactersIn: ""), NSCharacterSet(charactersIn: "abc"))
let notEqualPairs = [
("abc", "123"),
// ("ab", "abc"),
// ("abc", "")
]

for pair in equalPairs {
XCTAssertEqual(Box(charactersIn: pair.0), Box(charactersIn: pair.1))
}
XCTAssertEqual(Box.alphanumerics, Box.alphanumerics)

for pair in notEqualPairs {
XCTAssertNotEqual(Box(charactersIn: pair.0), Box(charactersIn: pair.1))
}
XCTAssertNotEqual(Box.alphanumerics, Box.decimalDigits)
}

}