JSONMatcher 0.1.0

JSONMatcher 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM

Maintained by giginet.



JSONMatcher is a JSON matcher library for Swift testing. It works as an extension for Nimble.

This library is inspired by rspec-json_matcher.

Usage

Example

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()],
            ]
        ]))
    }
}

Matcher

  • beJSON
  • beJSONIncluding
  • beJSONAs
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]))

Requirements

Supported Swift Version

  • Swift 2.2

Dependencies

  • Nimble >= 4.0.0

Supported Platforms

  • iOS 8.0 or above
  • OSX 10.9 or above
  • tvOS 9.0 or above

Installation

Author

giginet <[email protected]>

License

MIT License