输入:
有一个基于Command Line Tool
模板的项目。我有JSON.swift文件,上面有我想测试的代码。
import Foundation
// MARK: - JSON
public enum JSON<RawValue>: CustomStringConvertible {
public typealias Dictionary = [String: Any]
public typealias List = [JSON<RawValue>]
// MARK: - Cases
case String(String)
case Number(Double)
case Object(Dictionary)
case Array(List)
case Bool(Bool)
case Null
// MARK: - Identifier
private var uniqueIdentifier: Int {
switch self {
case .String:
return 1
case .Number:
return 2
case .Object:
return 3
case .Array:
return 4
case .Bool:
return 5
case .Null:
return 0
}
}
// MARK: - Initialization
init(_ rawValue: RawValue) {
switch rawValue {
case is String:
let string = rawValue as? String ?? ""
self = string.isEmpty ? .Null : .String(string)
case is Double:
let double = rawValue as? Double ?? .zero
self = double == .zero ? .Null : .Number(double)
case is Dictionary:
let dictionary = rawValue as? Dictionary ?? Dictionary()
self = dictionary.isEmpty ? .Null : .Object(dictionary)
case is List:
let list = rawValue as? List ?? []
self = list.isEmpty ? .Null : .Array(list)
case is Bool:
let bool = rawValue as? Bool
self = bool == nil ? .Null : .Bool(bool ?? false)
default:
self = .Null
}
}
init(_ rawInput: Dictionary) {
self = .Object(rawInput)
}
init(_ rawInput: List) {
self = .Array(rawInput)
}
// MARK: - (Key, Value) Description
private var key: String {
switch self {
case .String:
return "String"
case .Number:
return "Double"
case .Object:
return "Object"
case .Array:
return "Array"
case .Bool:
return "Bool"
case .Null:
return "Null"
}
}
public var value: Any? {
switch self {
case let .String(string):
if let integerValue = Int(string) {
return integerValue
} else {
return string
}
case let .Number(double):
return double
case let .Object(dictionary):
return dictionary
case let .Array(list):
return list
case let .Bool(bool):
return bool
case .Null:
return nil
}
}
public var description: String {
switch self {
case let .String(string):
return self.createDescription(by: string)
case let .Number(double):
return self.createDescription(by: double.description)
case let .Object(dictionary):
return self.createDescription(by: dictionary.description)
case let .Array(list):
return self.createDescription(by: list.description)
case let .Bool(bool):
return self.createDescription(by: bool.description)
case .Null:
return "nil"
}
}
// MARK: - Helpers
private func createDescription(by value: String) -> String {
return "\(value) as \(self.key)"
}
}
// MARK: - Equatable
extension JSON: Equatable {
public static func == (_ lhs: JSON, _ rhs: JSON) -> Bool {
return lhs.uniqueIdentifier == rhs.uniqueIdentifier
}
public static func != (_ lhs: JSON, _ rhs: JSON) -> Bool {
return !(lhs == rhs)
}
}
还有JSONTests.swift文件。
import XCTest
@testable import JSONApp
class JSONTests: XCTestCase {
// MARK: - Test Cases
func testStringInitializationForNilableByName() {
XCTAssertNotNil(JSON("Ivan"))
}
}
// MARK: - Test Life Cycle
extension JSONTests {
override func setUp() {}
override func tearDown() {}
}
我想做什么?
我尝试进行测试。
输出:
Undefined symbols for architecture x86_64:
"type metadata accessor for JSONApp.JSON", referenced from:
type metadata accessor for JSONApp.JSON<Swift.String> in JSONTests.o
"JSONApp.JSON.init(A) -> JSONApp.JSON<A>", referenced from:
implicit closure #1 () throws -> Any? in JSONTests.JSONTests.testStringInitializationForNilableByName() -> () in JSONTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)