| // 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 |
| // |
| // See http://swift.org/LICENSE.txt for license information |
| // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| // |
| |
| |
| |
| #if DEPLOYMENT_RUNTIME_OBJC || os(Linux) |
| import Foundation |
| import XCTest |
| #else |
| import SwiftFoundation |
| import SwiftXCTest |
| #endif |
| |
| |
| |
| class TestNSDictionary : XCTestCase { |
| |
| static var allTests: [(String, (TestNSDictionary) -> () throws -> Void)] { |
| return [ |
| ("test_BasicConstruction", test_BasicConstruction), |
| // ("test_ArrayConstruction", test_ArrayConstruction), |
| ("test_description", test_description), |
| ("test_enumeration", test_enumeration), |
| ("test_equality", test_equality), |
| ("test_copying", test_copying), |
| ("test_mutableCopying", test_mutableCopying), |
| ] |
| } |
| |
| func test_BasicConstruction() { |
| let dict = NSDictionary() |
| let dict2: NSDictionary = ["foo": "bar"] |
| XCTAssertEqual(dict.count, 0) |
| XCTAssertEqual(dict2.count, 1) |
| } |
| |
| |
| func test_description() { |
| let d1: NSDictionary = [ "foo": "bar", "baz": "qux"] |
| XCTAssertEqual(d1.description, "{\n baz = qux;\n foo = bar;\n}") |
| let _: NSDictionary = ["1" : ["1" : ["1" : "1"]]] |
| // Disabled since it emits AnyHashable in the description for now... |
| // XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}") |
| } |
| |
| func test_HeterogeneousConstruction() { |
| let dict2: NSDictionary = [ |
| "foo": "bar", |
| 1 : 2 |
| ] |
| XCTAssertEqual(dict2.count, 2) |
| XCTAssertEqual(dict2["foo"] as? String, "bar") |
| XCTAssertEqual(dict2[1] as? NSNumber, NSNumber(value: 2)) |
| } |
| |
| // func test_ArrayConstruction() { |
| // let objects = ["foo", "bar", "baz"] |
| // let keys = ["foo", "bar", "baz"] |
| // let dict = NSDictionary(objects: objects, forKeys: keys) |
| // XCTAssertEqual(dict.count, 3) |
| // } |
| |
| func test_enumeration() { |
| let dict : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"] |
| let e = dict.keyEnumerator() |
| var keys = Set<String>() |
| keys.insert((e.nextObject()! as! String)) |
| keys.insert((e.nextObject()! as! String)) |
| keys.insert((e.nextObject()! as! String)) |
| XCTAssertNil(e.nextObject()) |
| XCTAssertNil(e.nextObject()) |
| XCTAssertEqual(keys, ["foo", "whiz", "toil"]) |
| |
| let o = dict.objectEnumerator() |
| var objs = Set<String>() |
| objs.insert((o.nextObject()! as! String)) |
| objs.insert((o.nextObject()! as! String)) |
| objs.insert((o.nextObject()! as! String)) |
| XCTAssertNil(o.nextObject()) |
| XCTAssertNil(o.nextObject()) |
| XCTAssertEqual(objs, ["bar", "bang", "trouble"]) |
| } |
| |
| func test_sequenceType() { |
| let dict : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"] |
| var result = [String:String]() |
| for (key, value) in dict { |
| result[key as! String] = (value as! String) |
| } |
| XCTAssertEqual(result, ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"]) |
| } |
| |
| func test_equality() { |
| let dict1 = NSDictionary(dictionary: [ |
| "foo":"bar", |
| "whiz":"bang", |
| "toil":"trouble", |
| ]) |
| let dict2 = NSDictionary(dictionary: [ |
| "foo":"bar", |
| "whiz":"bang", |
| "toil":"trouble", |
| ]) |
| let dict3 = NSDictionary(dictionary: [ |
| "foo":"bar", |
| "whiz":"bang", |
| "toil":"troubl", |
| ]) |
| |
| XCTAssertTrue(dict1 == dict2) |
| XCTAssertTrue(dict1.isEqual(dict2)) |
| XCTAssertTrue(dict1.isEqual(to: [ |
| "foo":"bar", |
| "whiz":"bang", |
| "toil":"trouble", |
| ])) |
| XCTAssertEqual(dict1.hash, dict2.hash) |
| XCTAssertEqual(dict1.hashValue, dict2.hashValue) |
| |
| XCTAssertFalse(dict1 == dict3) |
| XCTAssertFalse(dict1.isEqual(dict3)) |
| XCTAssertFalse(dict1.isEqual(to:[ |
| "foo":"bar", |
| "whiz":"bang", |
| "toil":"troubl", |
| ])) |
| |
| XCTAssertFalse(dict1.isEqual(nil)) |
| XCTAssertFalse(dict1.isEqual(NSObject())) |
| } |
| |
| func test_copying() { |
| let inputDictionary : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"] |
| |
| let copy: NSDictionary = inputDictionary.copy() as! NSDictionary |
| XCTAssertTrue(inputDictionary === copy) |
| |
| let dictMutableCopy = inputDictionary.mutableCopy() as! NSMutableDictionary |
| let dictCopy2 = dictMutableCopy.copy() as! NSDictionary |
| XCTAssertTrue(type(of: dictCopy2) === NSDictionary.self) |
| XCTAssertFalse(dictMutableCopy === dictCopy2) |
| XCTAssertTrue(dictMutableCopy == dictCopy2) |
| } |
| |
| func test_mutableCopying() { |
| let inputDictionary : NSDictionary = ["foo" : "bar", "whiz" : "bang", "toil" : "trouble"] |
| |
| let dictMutableCopy1 = inputDictionary.mutableCopy() as! NSMutableDictionary |
| XCTAssertTrue(type(of: dictMutableCopy1) === NSMutableDictionary.self) |
| XCTAssertFalse(inputDictionary === dictMutableCopy1) |
| XCTAssertTrue(inputDictionary == dictMutableCopy1) |
| |
| let dictMutableCopy2 = dictMutableCopy1.mutableCopy() as! NSMutableDictionary |
| XCTAssertTrue(type(of: dictMutableCopy2) === NSMutableDictionary.self) |
| XCTAssertFalse(dictMutableCopy2 === dictMutableCopy1) |
| XCTAssertTrue(dictMutableCopy2 == dictMutableCopy1) |
| } |
| |
| } |