Skip to content
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
20 changes: 20 additions & 0 deletions features/yard_example_test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ Feature: yard test-examples
When I run `bundle exec yard test-examples`
Then the output should contain "1 runs, 0 assertions, 0 failures, 0 errors, 0 skips"

Scenario: runs titled @example tags without a body
Given a file named "example_test_helper.rb" with:
"""
require 'app/app'
"""
And a file named "app/app.rb" with:
"""
# @example sums two numbers
def sum(one, two)
one + two
end
"""
When I run `bundle exec yard test-examples`
Then the output should contain "1 runs, 0 assertions, 0 failures, 0 errors, 0 skips"

Scenario: normalizes nil example text as no lines
When I run `bundle exec ruby -e "require 'yard_example_test'; command = YARD::CLI::TestExamples.new; p command.send(:normalize_example_lines, nil)"`
Then the output should contain "[]"
And the exit status should be 0

Scenario: handles `# =>` return comment
Given a file named "example_test_helper.rb" with:
"""
Expand Down
6 changes: 5 additions & 1 deletion lib/yard/cli/test_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,19 @@ def extract_expectations(example)
# 3. Splits on newlines, strips leading and trailing whitespace from
# each line, and discards any blank lines.
#
# If +text+ is +nil+ or blank, returns an empty array.
#
# The resulting array is consumed by {#extract_expectations} to identify
# code lines and their corresponding +#=>+ assertion lines.
#
# @param text [String] the raw body text of a +@example+ tag
# @param text [String, nil] the raw body text of a +@example+ tag
#
# @return [Array<String>] the normalized, non-empty lines of the example;
# +#=>+ lines are always separate entries, never inline with code
#
def normalize_example_lines(text)
return [] if text.nil? || text.strip.empty?

text = text.gsub('# =>', '#=>')
text = text.gsub('#=>', "\n#=>")
text.split("\n").map(&:strip).reject(&:empty?)
Expand Down