Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a not expectation property returning an opposite expectation #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
### Enhancements

- Unhandled errors will now be reported from the invoked cases source map.
- New `not` expectation method which returns a Not Expectation. This allows you
to run expecations such as:

```swift
try expect(...).to.not.beNil()
```

## 0.8.0

Expand Down
48 changes: 48 additions & 0 deletions Sources/Spectre/Expectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ public protocol ExpectationType {
}


public protocol NotExpectationType {
associatedtype ValueType
var expression: () throws -> ValueType? { get }
func failure(_ reason: String) -> FailureType
}


struct ExpectationFailure : FailureType {
let file: String
let line: Int
Expand Down Expand Up @@ -44,6 +51,30 @@ open class Expectation<T> : ExpectationType {
}
}

public class NotExpectation<T>: NotExpectationType {
public typealias ValueType = T
public let expression: () throws -> ValueType?

let file: String
let line: Int
let function: String

public var to: NotExpectation<T> {
return self
}

init(file: String, line: Int, function: String, expression: @escaping () throws -> ValueType?) {
self.file = file
self.line = line
self.function = function
self.expression = expression
}

public func failure(_ reason: String) -> FailureType {
return ExpectationFailure(reason: reason, file: file, line: line, function: function)
}
}

public func expect<T>( _ expression: @autoclosure @escaping () throws -> T?, file: String = #file, line: Int = #line, function: String = #function) -> Expectation<T> {
return Expectation(file: file, line: line, function: function, expression: expression)
}
Expand All @@ -52,6 +83,14 @@ public func expect<T>(_ file: String = #file, line: Int = #line, function: Strin
return Expectation(file: file, line: line, function: function, expression: expression)
}

// MARK: Not

extension Expectation {
public var not: NotExpectation<T> {
return NotExpectation(file: file, line: line, function: function, expression: expression)
}
}

// MARK: Equatability

public func == <E: ExpectationType>(lhs: E, rhs: E.ValueType) throws where E.ValueType: Equatable {
Expand Down Expand Up @@ -126,6 +165,15 @@ extension ExpectationType {
}
}

extension NotExpectationType {
public func beNil() throws {
let value = try expression()
if value == nil {
throw failure("value is nil")
}
}
}

// MARK: Boolean

extension ExpectationType where ValueType == Bool {
Expand Down
17 changes: 17 additions & 0 deletions Tests/SpectreTests/ExpectationSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public func testExpectation() {
$0.it("passes when value is nil") {
try expect(name).to.beNil()
}

$0.context("not") {
$0.it("does not error when value is not nil") {
do {
try expect("kyle").to.not.beNil()
} catch {
fatalError()
}
}

$0.it("errors when value is nil") {
do {
try expect(name).to.not.beNil()
fatalError()
} catch {}
}
}
}

$0.describe("comparison to type") {
Expand Down