JSONMatcher 是一个 Swift 测试的 JSON 匹配器库。它作为 Nimble 的扩展。
这个库受到了 rspec-json_matcher 的启发。
import XCTest
import Nimble
import JSONMatcher
class ExampleTestCase: XCTestCase {
func testComplexExample() {
expect([
"name" : "Snorlax",
"no" : 143,
"species" : "Sleeping",
"type" : ["normal"],
"stats" : [
"hp" : 160,
"attack" : 110,
"defense" : 65,
"special_attack" : 65,
"special_defense" : 65,
"speed" : 30
],
"moves" : [
["name" : "Tackle", "type" : "normal", "level" : 1],
["name" : "Hyper Beam", "type" : "normal", "level" : NSNull()],
]
]).to(beJSONAs([
"name" : "Snorlax",
"no" : Type.Number, // value type matching
"species" : try! NSRegularExpression(pattern: "[A-Z][a-z]+", options: []), // regular expression matching
"type" : ["[a-z]+".regex], // shorthands for NSRegularExpression
"stats" : [
"hp" : 160,
"attack" : 110,
"defense" : 65,
"special_attack" : 65,
"special_defense" : 65,
"speed" : 30
],
"moves" : [
["name" : "Tackle", "type" : "[a-z]+".regex, "level" : Type.Number], // nested collection
["name" : "Hyper Beam", "type" : "normal", "level" : NSNull()],
]
]))
}
}
expect("{\"name\": \"Pikachu\"}").to(beJSON())
expect(["name" : "Pikachu", "no" : 25]).to(beJSONIncluding(["name" : "Pikachu"]))
expect(["name" : "Pikachu", "no" : 25]).to(beJSONAs(["name": "Pikachu", "no" : 25]))
giginet <[email protected]>
MIT 许可证