diff --git a/features/yard_example_test.feature b/features/yard_example_test.feature index 40e7f7f..2bfd868 100644 --- a/features/yard_example_test.feature +++ b/features/yard_example_test.feature @@ -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: """ diff --git a/lib/yard/cli/test_examples.rb b/lib/yard/cli/test_examples.rb index 621f63b..00b4acb 100644 --- a/lib/yard/cli/test_examples.rb +++ b/lib/yard/cli/test_examples.rb @@ -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] 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?)