A JSON matcher extension in Swift for Nimble
Swift Ruby Objective-C
Latest commit c501bbe May 21, 2016 @giginet Add CocoaPods badges
Permalink
Failed to load latest commit information.
Carthage/Checkouts
JSONMatcher.xcodeproj
JSONMatcher.xcworkspace
Sources/JSONMatcher
Tests/JSONMatcher
.gitignore
.gitmodules
.swiftlint.yml
.travis.yml
Cartfile
Cartfile.resolved
JSONMatcher.podspec
LICENSE.md
Package.swift
README.md
codecov.yml
run_tests

README.md

JSONMatcher

Build Status codecov CocoaPods Compatible Carthage compatible Swift Package Manager Platform

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

Carthage

Declare following in your Cartfile.private.

github "giginet/JSONMatcher"

CocoaPods

Declare following in your Podfile.

use_frameworks!

target "YourApplicationTests" do
  pod 'JSONMatcher'
end

Author

giginet <giginet.net@gmail.com>

License

MIT License