# Nimble
[](https://travis-ci.org/Quick/Nimble)
[](https://cocoapods.org/pods/Nimble)
[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org/pods/Nimble)
[](https://houndci.com)
Use Nimble to express the expected outcomes of Swift
or Objective-C expressions. Inspired by
[Cedar](https://github.com/pivotal/cedar).
```swift
// Swift
expect(1 + 1).to(equal(2))
expect(1.2).to(beCloseTo(1.1, within: 0.1))
expect(3) > 2
expect("seahorse").to(contain("sea"))
expect(["Atlantic", "Pacific"]).toNot(contain("Mississippi"))
expect(ocean.isClean).toEventually(beTruthy())
```
# How to Use Nimble
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
- [Some Background: Expressing Outcomes Using Assertions in XCTest](#some-background-expressing-outcomes-using-assertions-in-xctest)
- [Nimble: Expectations Using `expect(...).to`](#nimble-expectations-using-expectto)
- [Custom Failure Messages](#custom-failure-messages)
- [Type Safety](#type-safety)
- [Operator Overloads](#operator-overloads)
- [Lazily Computed Values](#lazily-computed-values)
- [C Primitives](#c-primitives)
- [Asynchronous Expectations](#asynchronous-expectations)
- [Objective-C Support](#objective-c-support)
- [Disabling Objective-C Shorthand](#disabling-objective-c-shorthand)
- [Built-in Matcher Functions](#built-in-matcher-functions)
- [Type Checking](#type-checking)
- [Equivalence](#equivalence)
- [Identity](#identity)
- [Comparisons](#comparisons)
- [Types/Classes](#typesclasses)
- [Truthiness](#truthiness)
- [Swift Assertions](#swift-assertions)
- [Swift Error Handling](#swift-error-handling)
- [Exceptions](#exceptions)
- [Collection Membership](#collection-membership)
- [Strings](#strings)
- [Collection Elements](#collection-elements)
- [Collection Count](#collection-count)
- [Notifications](#notifications)
- [Matching a value to any of a group of matchers](#matching-a-value-to-any-of-a-group-of-matchers)
- [Custom Validation](#custom-validation)
- [Writing Your Own Matchers](#writing-your-own-matchers)
- [PredicateResult](#predicateresult)
- [Lazy Evaluation](#lazy-evaluation)
- [Type Checking via Swift Generics](#type-checking-via-swift-generics)
- [Customizing Failure Messages](#customizing-failure-messages)
- [Basic Customization](#basic-customization)
- [Full Customization](#full-customization)
- [Supporting Objective-C](#supporting-objective-c)
- [Properly Handling `nil` in Objective-C Matchers](#properly-handling-nil-in-objective-c-matchers)
- [Migrating from the Old Matcher API](#migrating-from-the-old-matcher-api)
- [Minimal Step - Use `.predicate`](#minimal-step---use-predicate)
- [Convert to use `Predicate` Type with Old Matcher Constructor](#convert-to-use-predicate-type-with-old-matcher-constructor)
- [Convert to `Predicate` Type with Preferred Constructor](#convert-to-predicate-type-with-preferred-constructor)
- [Deprecation Roadmap](#deprecation-roadmap)
- [Installing Nimble](#installing-nimble)
- [Installing Nimble as a Submodule](#installing-nimble-as-a-submodule)
- [Installing Nimble via CocoaPods](#installing-nimble-via-cocoapods)
- [Using Nimble without XCTest](#using-nimble-without-xctest)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
# Some Background: Expressing Outcomes Using Assertions in XCTest
Apple's Xcode includes the XCTest framework, which provides
assertion macros to test whether code behaves properly.
For example, to assert that `1 + 1 = 2`, XCTest has you write:
```swift
// Swift
XCTAssertEqual(1 + 1, 2, "expected one plus one to equal two")
```
Or, in Objective-C:
```objc
// Objective-C
XCTAssertEqual(1 + 1, 2, @"expected one plus one to equal two");
```
XCTest assertions have a couple of drawbacks:
1. **Not enough macros.** There's no easy way to assert that a string
contains a particular substring, or that a number is less than or
equal to another.
2. **It's hard to write asynchronous tests.** XCTest forces you to write
a lot of boilerplate code.
Nimble addresses these concerns.
# Nimble: Expectations Using `expect(...).to`
Nimble allows you to express expectations using a natural,
easily understood language:
```swift
// Swift
import Nimble
expect(seagull.squawk).to(equal("Squee!"))
```
```objc
// Objective-C
@import Nimble;
expect(seagull.squawk).to(equal(@"Squee!"));
```
> The `expect` function autocompletes to include `file:` and `line:`,
but these parameters are optional. Use the default values to have
Xcode highlight the correct line when an expectation is not met.
To perform the opposite expectation--to assert something is *not*
equal--use `toNot` or `notTo`:
```swift
// Swift
import Nimble
expect(seagull.squawk).toNot(equal("Oh, hello there!"))
expect(seagull.squawk).notTo(equal("Oh, hello there!"))
```
```objc
// Objective-C
@import Nimble;
expect(seagull.squawk).toNot(equal(@"Oh, hello there!"));
expect(seagull.squawk).notTo(equal(@"Oh, hello there!"));
```
## Custom Failure Messages
Would you like to add more information to the test's failure messages? Use the `description` optional argument to add your own text:
```swift
// Swift
expect(1 + 1).to(equal(3))
// failed - expected to equal <3>, got <2>
expect(1 + 1).to(equal(3), description: "Make sure libKindergartenMath is loaded")
// failed - Make sure libKindergartenMath is loaded
// expected to equal <3>, got <2>
```
Or the *WithDescription version in Objective-C:
```objc
// Objective-C
@import Nimble;
expect(@(1+1)).to(equal(@3));
// failed - expected to equal <3.0000>, got <2.0000>
expect(@(1+1)).toWithDescription(equal(@3), @"Make sure libKindergartenMath is loaded");
// failed - Make sure libKindergartenMath is loaded
// expected to equal <3.0000>, got <2.0000>
```
## Type Safety
Nimble makes sure you don't compare two types that don't match:
```swift
// Swift
// Does not compile:
expect(1 + 1).to(equal("Squee!"))
```
> Nimble uses generics--only available in Swift--to ensure
type correctness. That means type checking is
not available when using Nimble in Objective-C. :sob:
## Operator Overloads
Tired of so much typing? With Nimble, you can use overloaded operators
like `==` for equivalence, or `>` for comparisons:
```swift
// Swift
// Passes if squawk does not equal "Hi!":
expect(seagull.squawk) != "Hi!"
// Passes if 10 is greater than 2:
expect(10) > 2
```
> Operator overloads are only available in Swift, so you won't be able
to use this syntax in Objective-C. :broken_heart:
## Lazily Computed Values
The `expect` function doesn't evaluate the value it's given until it's
time to match. So Nimble can test whether an expression raises an
exception once evaluated:
```swift
// Swift
// Note: Swift currently doesn't have exceptions.
// Only Objective-C code can raise exceptions
// that Nimble will catch.
// (see https://github.com/Quick/Nimble/issues/220#issuecomment-172667064)
let exception = NSException(
name: NSInternalInconsistencyException,
reason: "Not enough fish in the sea.",
userInfo: ["something": "is fishy"])
expect { exception.raise() }.to(raiseException())
// Also, you can customize raiseException to be more specific
expect { exception.raise() }.to(raiseException(named: NSInternalInconsistencyException))
expect { exception.raise() }.to(raiseException(
named: NSInternalInconsistencyException,
reason: "Not