Skip to content

Commit

Permalink
Fix crystal-lang#7192 correctly
Browse files Browse the repository at this point in the history
Currently nesting `it`/`pending` doesn't break `crystal spec -v` output.
  • Loading branch information
makenowjust committed Jan 11, 2019
1 parent 6d5347b commit 82124b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ module Spec

@@spec_nesting = false

def self.check_nesting_spec(file, line)
check_nesting_spec(file, line) { }
end

def self.check_nesting_spec(file, line, &block)
raise NestingSpecError.new("can't nest `it` or `pending`", file, line) if @@spec_nesting

Expand Down
27 changes: 10 additions & 17 deletions src/spec/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,25 @@ module Spec::Methods
#
# It is usually used inside a `#describe` or `#context` section.
def it(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block)
return unless Spec.matches?(description, file, line, end_line)
Spec::RootContext.check_nesting_spec(file, line) do
return unless Spec.matches?(description, file, line, end_line)

Spec.formatters.each(&.before_example(description))
Spec.formatters.each(&.before_example(description))

start = Time.monotonic
begin
Spec::RootContext.check_nesting_spec(file, line) do
start = Time.monotonic
begin
Spec.run_before_each_hooks
block.call
Spec::RootContext.report(:success, description, file, line, Time.monotonic - start)
rescue ex : Spec::AssertionFailed
Spec::RootContext.report(:fail, description, file, line, Time.monotonic - start, ex)
Spec.abort! if Spec.fail_fast?
rescue ex : Spec::NestingSpecError
new_ex = Spec::NestingSpecError.new("cannot nest `it` and `pending`: it has a nesting spec.", ex.file, ex.line)
Spec::RootContext.report(:error, description, file, line, Time.monotonic - start, new_ex)
Spec.abort! if Spec.fail_fast?
rescue ex
Spec::RootContext.report(:error, description, file, line, Time.monotonic - start, ex)
Spec.abort! if Spec.fail_fast?
ensure
Spec.run_after_each_hooks
end
rescue ex : Spec::NestingSpecError
Spec::RootContext.report(:error, description, file, line, Time.monotonic - start, ex)
Spec.abort! if Spec.fail_fast?
raise ex
end
end

Expand All @@ -78,12 +70,13 @@ module Spec::Methods
#
# It is usually used inside a `#describe` or `#context` section.
def pending(description = "assert", file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block)
return unless Spec.matches?(description, file, line, end_line)
Spec::RootContext.check_nesting_spec(file, line) do
return unless Spec.matches?(description, file, line, end_line)

Spec.formatters.each(&.before_example(description))
Spec::RootContext.check_nesting_spec(file, line)
Spec.formatters.each(&.before_example(description))

Spec::RootContext.report(:pending, description, file, line)
Spec::RootContext.report(:pending, description, file, line)
end
end

# Defines a yet-to-be-implemented pending test case
Expand Down

0 comments on commit 82124b3

Please sign in to comment.