Skip to content

Commit

Permalink
Merge pull request #3 from quartzmo/multiple-examples
Browse files Browse the repository at this point in the history
Add support for multiple examples per code object
  • Loading branch information
p0deje authored Sep 20, 2016
2 parents a075461 + 80af343 commit b3614c3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
34 changes: 32 additions & 2 deletions features/yard-doctest.feature
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ Feature: yard doctest
When I run `bundle exec yard doctest`
Then the output should contain:
"""
Expected: 2
Actual: "2"
--- expected
+++ actual
@@ -1 +1,2 @@
-2
+# encoding: US-ASCII
+"2"
"""

Scenario: asserts exceptions
Expand Down Expand Up @@ -494,6 +498,32 @@ Feature: yard doctest
When I run `bundle exec yard doctest`
Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"

Scenario: supports test-name hooks for multiple examples on the same code object
Given a file named "doctest_helper.rb" with:
"""
require 'app/app'
YARD::Doctest.before('#flag') do
@flag = true
end
YARD::Doctest.before('#flag@Second example for flag') do
@flag = false
end
"""
And a file named "app/app.rb" with:
"""
# @example
# flag #=> true
# @example Second example for flag
# flag #=> false
def flag
@flag
end
"""
When I run `bundle exec yard doctest`
Then the output should contain "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips"

Scenario: can skip tests
Given a file named "doctest_helper.rb" with:
"""
Expand Down
16 changes: 13 additions & 3 deletions lib/yard/doctest/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ def generate

return if YARD::Doctest.skips.any? { |skip| this.definition.include?(skip) }
describe this.definition do
register_hooks(this.definition, YARD::Doctest.hooks)
# Append this.name to this.definition if YARD's @example tag is followed by
# descriptive text, to support hooks for multiple examples per code object.
example_name = if this.name.empty?
this.definition
else
"#{this.definition}@#{this.name}"
end

register_hooks(example_name, YARD::Doctest.hooks)

it this.name do
this.asserts.each do |assert|
Expand Down Expand Up @@ -65,12 +73,14 @@ def add_filepath_to_backtrace(exception, filepath)
exception.set_backtrace backtrace
end

def self.register_hooks(definition, all_hooks)


def self.register_hooks(example_name, all_hooks)
all_hooks.each do |type, hooks|
hooks.each do |hook|
if hook[:test]
# test-name hooks
send(type, &hook[:block]) if definition.include?(hook[:test])
send(type, &hook[:block]) if example_name.include?(hook[:test])
else
# global hooks
send(type, &hook[:block])
Expand Down

0 comments on commit b3614c3

Please sign in to comment.