Skip to content

Implement NSArray's customMirror #2033

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 4 commits into from
Mar 26, 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
4 changes: 3 additions & 1 deletion Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,9 @@ extension NSArray : ExpressibleByArrayLiteral {
}

extension NSArray : CustomReflectable {
public var customMirror: Mirror { NSUnimplemented() }
public var customMirror: Mirror {
return Mirror(reflecting: _storage)
}
}

extension NSArray : _StructTypeBridgeable {
Expand Down
15 changes: 15 additions & 0 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TestNSArray : XCTestCase {
("test_replaceObjectsAtIndexesWithObjects", test_replaceObjectsAtIndexesWithObjects),
("test_pathsMatchingExtensions", test_pathsMatchingExtensions),
("test_arrayUsedAsCFArrayInvokesArrayMethods", test_arrayUsedAsCFArrayInvokesArrayMethods),
("test_customMirror", test_customMirror),
]
}

Expand Down Expand Up @@ -796,6 +797,20 @@ class TestNSArray : XCTestCase {
let match5 = paths.pathsMatchingExtensions(["..txt"])
XCTAssertEqual(match5, [])
}

func test_customMirror() {
let inputArray = ["this", "is", "a", "test", "of", "custom", "mirror"]
let array = NSArray(array: inputArray)
let arrayMirror = array.customMirror

XCTAssertEqual(array[0] as! String, arrayMirror.descendant(0) as! String)
XCTAssertEqual(array[1] as! String, arrayMirror.descendant(1) as! String)
XCTAssertEqual(array[2] as! String, arrayMirror.descendant(2) as! String)
XCTAssertEqual(array[3] as! String, arrayMirror.descendant(3) as! String)
XCTAssertEqual(array[4] as! String, arrayMirror.descendant(4) as! String)
XCTAssertEqual(array[5] as! String, arrayMirror.descendant(5) as! String)
XCTAssertEqual(array[6] as! String, arrayMirror.descendant(6) as! String)
}

func test_arrayUsedAsCFArrayInvokesArrayMethods() {
let number = 789 as NSNumber
Expand Down