ShouldSyntax.md
August 21, 2014 ยท View on GitHub
should and shouldNot syntax
Should syntax provides another way of writing expectations.
let actual = 3
let expected = 3
actual.should.equal(expected)
should and shouldNot work by being added to every object through extensions.
They can be used to define expectations on any object:
"Sleipnir".shouldNot.beEmpty()
[1, 2, 3].should.contain(3)
This syntax has some known limitations.
It can not be used with custom pure swift classes, because AnyObject is a protocol and protocols can not be extended in Swift.
Both should and expect are supported and available. Using one of them or both is a matter of preference.
Using Built-in matchers
Equal
Values must be Equatable, Comparable or derive from NSObject.
let actual = [1, 2, 3]
actual.should.equal([1, 2, 3])
"some string".shouldNot.equal("another string")
BeNil
let actual : String? = nil
actual.should.beNil()
"some string".shouldNot.beNil()
BeTrue/BeFalse
let actual = true
actual.should.beTrue()
actual.shouldNot.beFalse()
BeGreaterThan/BeLessThan
Values must be Comparable.
3.should.beGreaterThan(1)
1.should.beLessThan(3)
BeGreaterThanOrEqualTo/BeLessThanOrEqualTo
Values must be Comparable.
3.should.beGreaterThanOrEqualTo(1)
1.should.beLessThanOrEqualTo(3)
Collections/String matchers
Contain
Supports Swift collections with Equatable elements, NSArrays, NSSets and Strings.
let actual = [1, 2, 3]
actual.should.contain(1)
"some string".shouldNot.contain("another")
BeginWith/EndWith
Supports Swift collections with Equatable elements, NSArrays and Strings.
let actual = [1, 2, 3]
actual.should.beginWith(1)
"some string".should.endWith("string")
BeEmpty
"".should.beEmpty()
[1,2,3].shouldNot.beEmpty()