Skip to content

Commit

Permalink
Print accurate error message when invalid DSL func used in 'beforeEac…
Browse files Browse the repository at this point in the history
…h' or 'afterEach'
  • Loading branch information
jwfriese committed Jan 23, 2016
1 parent 72f3058 commit 02a8a23
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Sources/Quick/DSL/World+DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension World {

internal func describe(description: String, flags: FilterFlags, closure: () -> ()) {
guard currentExampleMetadata == nil else {
raiseError("'describe' cannot be used inside 'it', 'describe' may only be used inside 'context' or 'describe'. ")
raiseError("'describe' cannot be used inside '\(currentPhase)', 'describe' may only be used inside 'context' or 'describe'. ")
}
guard currentExampleGroup != nil else {
raiseError("Error: example group was not created by its parent QuickSpec spec. Check that describe() or context() was used in QuickSpec.spec() and not a more general context (i.e. an XCTestCase test)")
Expand All @@ -34,7 +34,7 @@ extension World {

internal func context(description: String, flags: FilterFlags, closure: () -> ()) {
guard currentExampleMetadata == nil else {
raiseError("'context' cannot be used inside 'it', 'context' may only be used inside 'context' or 'describe'. ")
raiseError("'context' cannot be used inside '\(currentPhase)', 'context' may only be used inside 'context' or 'describe'. ")
}
self.describe(description, flags: flags, closure: closure)
}
Expand All @@ -53,7 +53,7 @@ extension World {

internal func beforeEach(closure: BeforeExampleClosure) {
guard currentExampleMetadata == nil else {
raiseError("'beforeEach' cannot be used inside 'it', 'beforeEach' may only be used inside 'context' or 'describe'. ")
raiseError("'beforeEach' cannot be used inside '\(currentPhase)', 'beforeEach' may only be used inside 'context' or 'describe'. ")
}
currentExampleGroup.hooks.appendBefore(closure)
}
Expand All @@ -71,7 +71,7 @@ extension World {

internal func afterEach(closure: AfterExampleClosure) {
guard currentExampleMetadata == nil else {
raiseError("'afterEach' cannot be used inside 'it', 'afterEach' may only be used inside 'context' or 'describe'. ")
raiseError("'afterEach' cannot be used inside '\(currentPhase)', 'afterEach' may only be used inside 'context' or 'describe'. ")
}
currentExampleGroup.hooks.appendAfter(closure)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ extension World {

internal func itBehavesLike(name: String, sharedExampleContext: SharedExampleContext, flags: FilterFlags, file: String, line: UInt) {
guard currentExampleMetadata == nil else {
raiseError("'itBehavesLike' cannot be used inside 'it', 'itBehavesLike' may only be used inside 'context' or 'describe'. ")
raiseError("'itBehavesLike' cannot be used inside '\(currentPhase)', 'itBehavesLike' may only be used inside 'context' or 'describe'. ")
}
let callsite = Callsite(file: file, line: line)
let closure = World.sharedWorld.sharedExample(name)
Expand Down Expand Up @@ -158,4 +158,18 @@ extension World {
internal func pending(description: String, closure: () -> ()) {
print("Pending: \(description)")
}

private var currentPhase: String {
if currentExampleMetadata != nil {
if beforesCurrentlyExecuting {
return "beforeEach"
} else if aftersCurrentlyExecuting {
return "afterEach"
}

return "it"
}

return ""
}
}

0 comments on commit 02a8a23

Please sign in to comment.