Doctest::Core
Extract doctests from your ruby files
Installation
Add this line to your application's Gemfile:
gem 'doctest-core'
And then execute:
$ bundle
Or install it yourself as:
$ gem install doctest-core
Usage
Add doctest-comments to your ruby files:
class ClassWithDoctests
# Always returns 'a'
#
# >> ClassWithDoctests.a
# => 'a'
def self.a
'a'
end
# Always returns 'b'
#
# >> ClassWithDoctests.b
# => 'b'
def self.b
'b'
end
end
You can then run
Doctest::Core.extract_from(ClassWithDoctests)
to get a collection of doctest instances.
Syntax
This searches for doctests within the source files of the given class. It checks for the following syntax:
# >> ruby_expression
for a test command,
# => ruby_expression
for a test result. A doctest consists of one or more consecutive test commands and one result line. There must be no empty lines between, but you can add any number of spaces before and after the #
at the beginning of the comment line.
Doctest instances
A doctest instance consists of
code_string
: The source code string ('ClassWithDoctests.a'
)result_string
: The result code string ('b'
)original_file
: The source location of the file that contains the doctestline
: The line number of the doctest occurrence
You can also access code_evaluation
and result_evaluation
for the results of the both code blocks.
Those instances can be used to implement test framework-specific gems.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request