Module: Doctest2::Minitest::DoctestHelpers::ClassMethods

Defined in:
lib/doctest2/minitest.rb

Instance Method Summary collapse

Instance Method Details

#doctest(klass) ⇒ Object

To be used inside the body of a tests-class It will automatically create test cases for all 'documentation test' snippets that exist in the comments above the methods in klass.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/doctest2/minitest.rb', line 20

def doctest(klass)
  doctests = Doctest2::Core.extract_from(klass)
  return puts "Note: Attempting to extract doctests for #{klass}, but none were found (at: #{caller_locations.first})" if doctests.empty?

  doctests.each do |doctest|
    file_location = Pathname.new(doctest.original_file).relative_path_from(Pathname.pwd)
    define_method("test_ (doctest) #{file_location}:#{doctest.line}") do
      message = "While running doctest: \n\t#{doctest.code_string}\n"
      begin
        assert_equal(doctest.result_evaluation, doctest.code_evaluation, message)
      rescue StandardError => error
        error.message << "\n\n#{message}"
        raise error
      end
    end
  end
end