A JSON matcher extension in Swift for Nimble
Switch branches/tags
Nothing to show
Clone or download
dusek and giginet Migrate to Swift 3 (#2)
* Xcode Convert to Swift 3

* Upgrade Nimble (v4.0.1 -> v6.1.0)

* Fix location of Nimble's utils.swift

* Fix remaining build failures

* Fix remaining test build failures

* Travis: upgrade Xcode image (7.3 -> 8.3)

* Fix Xcode automatic migration

* Travis: do not install swiftlint

swiftlint is apparently already installed on Travis CI Xcode 8.3
image.

* Add SWIFT_VERSION for tvOS and macOS projects too

* Travis: run `pod repo update` before running linting podspec

* Adjust macOS deployment target to match Nimble's

* Add .swift-version file for CocoaPods

* Remove `pod repo update` from Travis CI

No longer seems necessary.

* Review comment: adhere to Swift API guidelines

* Change `fileprivate` to `private`

* Adhere to Swift API guidelines

* Workaround for SR-4549

This did not affect Run and Test, but did affect Profile and
Archive (release builds).

Tested with Xcode 8.3.2 (Apple Swift version 3.1 (swiftlang-802.0.53
clang-802.0.42))

* Improve reliability of (fix) run_tests

Previously run_tests exit status did not fail if the tests
failed. Additional sh settings are needed to make it fail when our
unit tests fail.

Solution found using based on what fastlane's `scan` command does.

* Fix test failures due to change in stringification

In Swift 3, `String(describing: ...)` formats `["a": "b"]` as such,
however formats `"a"` as simply `a`, i.e. without quotes. This leads
to discrepancies when Nimble stringifies actual value in the form of a
`Dictionary` (i.e. leaf `String`s have quotes), and when JSONMatcher
stringifies the expected value in the form of
DictionaryElement (i.e. leaf `String`s, in this case `StringElement`
having no quotes). The solution to ensure consitency between the two
is therefore to stringify `StringElement`s with quotes around the
actual string.

* Remove swiftlint warnings [colon]

* Remove swiftlint warnings [trailing_comma]

* Fix build failure
Latest commit 07458af Jun 1, 2017

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