Created
March 9, 2022 23:38
-
-
Save Qqwy/555d7ea83bdf633fbd109811c1a1b40f to your computer and use it in GitHub Desktop.
Example of how to integrate doctests with Ruby Minitest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of how to integrate doctests with Ruby Minitest | |
# | |
# Besides depending on `minitest`, | |
# this functionality depends on the `doctest-core` gem, c.f. https://www.rubydoc.info/gems/doctest-core/ | |
class YourAppName::TestCase < MiniTest::Test | |
# 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`. | |
# | |
def self.doctest!(klass) | |
doctests = Doctest::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(Rails.root) | |
define_method("test_ (doctest) #{file_location}:#{doctest.line}") do | |
message = "While running doctest: \n\t#{doctest.code_string}\n" | |
assert_equal(doctest.result_evaluation, doctest.code_evaluation, message) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can then use it as
And then when running minitest, this example will run.