forked from crystal-lang/crystal
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spec: refactor to make it easier to add more filter kinds
Before this commit an `it` block ran immediately. This makes it hard to implement other filtering mechanisms. In particular to implement something like RSpec's `focus` we need to first know whether there's any example marked with `focus: true`. Without collecting all examples first we can't do this. So, this commit first collects all describes, contexts and examples and then filters then. The code is now much cleaner is easier to extend.
- Loading branch information
Showing
7 changed files
with
267 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "./item" | ||
|
||
module Spec | ||
class Example | ||
include Item | ||
|
||
getter parent : Context | ||
getter block : -> | ||
getter? pending : Bool | ||
|
||
def initialize(@parent : Context, @description : String, | ||
@file : String, @line : Int32, @end_line : Int32, | ||
@block : ->, @pending : Bool) | ||
end | ||
|
||
def run | ||
Spec.root_context.check_nesting_spec(file, line) do | ||
Spec.formatters.each(&.before_example(description)) | ||
|
||
if pending? | ||
Spec.root_context.report(:pending, description, file, line) | ||
return | ||
end | ||
|
||
start = Time.monotonic | ||
begin | ||
Spec.run_before_each_hooks | ||
block.call | ||
Spec.root_context.report(:success, description, file, line, Time.monotonic - start) | ||
rescue ex : Spec::AssertionFailed | ||
Spec.root_context.report(:fail, description, file, line, Time.monotonic - start, ex) | ||
Spec.abort! if Spec.fail_fast? | ||
rescue ex | ||
Spec.root_context.report(:error, description, file, line, Time.monotonic - start, ex) | ||
Spec.abort! if Spec.fail_fast? | ||
ensure | ||
Spec.run_after_each_hooks | ||
|
||
# We do this to give a chance for signals (like CTRL+C) to be handled, | ||
# which currently are only handled when there's a fiber switch | ||
# (IO stuff, sleep, etc.). Without it the user might wait more than needed | ||
# after pressing CTRL+C to quit the tests. | ||
Fiber.yield | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.