Skip to content

Commit 62ed264

Browse files
committed
NSArray: Add more tests
1 parent bc3c200 commit 62ed264

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

Foundation/NSArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
4949
}
5050
}
5151

52-
convenience init(objects elements: AnyObject...) {
52+
public convenience init(objects elements: AnyObject...) {
5353
self.init(objects: elements, count: elements.count)
5454
}
5555

TestFoundation/TestNSArray.swift

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class TestNSArray : XCTestCase {
2424
static var allTests: [(String, (TestNSArray) -> () throws -> Void)] {
2525
return [
2626
("test_BasicConstruction", test_BasicConstruction),
27+
("test_constructors", test_constructors),
28+
("test_constructorWithCopyItems", test_constructorWithCopyItems),
2729
("test_enumeration", test_enumeration),
2830
("test_sequenceType", test_sequenceType),
2931
("test_objectAtIndex", test_objectAtIndex),
@@ -43,7 +45,8 @@ class TestNSArray : XCTestCase {
4345
("test_copying", test_copying),
4446
("test_mutableCopying", test_mutableCopying),
4547
("test_writeToFile", test_writeToFile),
46-
("test_initWithContentsOfFile", test_initWithContentsOfFile)
48+
("test_initWithContentsOfFile", test_initWithContentsOfFile),
49+
("test_readWriteURL", test_readWriteURL)
4750
]
4851
}
4952

@@ -53,6 +56,45 @@ class TestNSArray : XCTestCase {
5356
XCTAssertEqual(array.count, 0)
5457
XCTAssertEqual(array2.count, 2)
5558
}
59+
60+
func test_constructors() {
61+
let arrayNil = NSArray(objects: nil, count: 0)
62+
XCTAssertEqual(arrayNil.count, 0)
63+
64+
let array1 = NSArray(object: "foo")
65+
XCTAssertEqual(array1.count, 1)
66+
67+
let testStrings: [AnyObject] = [NSString(string:"foo"), NSString(string: "bar")]
68+
testStrings.withUnsafeBufferPointer { ptr in
69+
let array2 = NSArray(objects: ptr.baseAddress, count: 1)
70+
XCTAssertEqual(array1, array2)
71+
}
72+
73+
let array3 = NSArray(objects: "foo" as NSString, "bar" as NSString, "baz" as NSString)
74+
XCTAssertEqual(array3.count, 3)
75+
let array4 = NSArray(array: ["foo", "bar", "baz"])
76+
XCTAssertEqual(array4.count, 3)
77+
let array5 = NSArray(arrayLiteral: "foo", "bar", "baz")
78+
XCTAssertEqual(array5.count, 3)
79+
XCTAssertEqual(array3, array4)
80+
XCTAssertEqual(array3, array5)
81+
XCTAssertEqual(array4, array5)
82+
83+
}
84+
85+
func test_constructorWithCopyItems() {
86+
let foo = "foo" as NSMutableString
87+
88+
let array1 = NSArray(array: [foo], copyItems: false)
89+
let array2 = NSArray(array: [foo], copyItems: true)
90+
91+
XCTAssertEqual(array1[0] as! String, "foo")
92+
XCTAssertEqual(array2[0] as! String, "foo")
93+
94+
foo.append("1")
95+
XCTAssertEqual(array1[0] as! String, "foo1")
96+
XCTAssertEqual(array2[0] as! String, "foo")
97+
}
5698

5799
func test_enumeration() {
58100
let array : NSArray = ["foo", "bar", "baz"]
@@ -464,7 +506,7 @@ class TestNSArray : XCTestCase {
464506
}
465507
}
466508

467-
func test_initWithContentsOfFile() {
509+
func test_initWithContentsOfFile() {
468510
let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 234))
469511
if let _ = testFilePath {
470512
let a1: NSArray = ["foo", "bar"]
@@ -504,7 +546,23 @@ class TestNSArray : XCTestCase {
504546
XCTFail("Temporary file creation failed")
505547
}
506548
}
507-
549+
550+
func test_readWriteURL() {
551+
let data = NSArray(arrayLiteral: "one", "two", "three", "four", "five")
552+
do {
553+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString
554+
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
555+
let testFile = tempDir + "/readWriteURL.txt"
556+
let url = URL(fileURLWithPath: testFile)
557+
try data.write(to: url)
558+
let data2 = try NSArray(contentsOf: url, error: ())
559+
XCTAssertEqual(data, data2)
560+
removeTestFile(testFile)
561+
} catch let e {
562+
XCTFail("Failed to write to file: \(e)")
563+
}
564+
}
565+
508566
private func createTestFile(_ path: String, _contents: Data) -> String? {
509567
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
510568
do {

0 commit comments

Comments
 (0)