Class: DocRSpec
- Inherits:
-
Object
- Object
- DocRSpec
- Defined in:
- lib/doc_rspec.rb,
lib/doc_rspec/parser.rb,
lib/doc_rspec/context.rb,
lib/doc_rspec/version.rb,
lib/doc_rspec/compiler.rb,
lib/doc_rspec/context/example.rb,
lib/doc_rspec/rspec_example_group.rb
Overview
Usage
Install the gem gem install doc_rspec or put doc_rspec in your Gemfile or gemspec file.
Then require doc_rspec in the file you want to use the docspec macro or put it into your spec_helper.rb or use any advanced require strategy you are used to.
Inside your RSpec file, at the example group level then call
# Usage example
docspec '<path_to_file>'
Where path_to_file is relative to the lib directory
Abstract
Ruby Codeblocks that start with a comment line as follows
# example: example_name
are considered an RSpec example of the RSpec example group doctest has been called.
RSpec Examples
Just plain old Ruby Code
# example: just an RSpec example
expect(2*21).to eq(42)
Implementation Notice
# example: We have access to the wrapping context
this_is_available(22) => 44
Although this gem is named doc_rspec and its goal is certainly to document behavior of Ruby code inside the RDoc documentation of that code, and most importantly backing the claims made in this documentation up with actual RSpec examples, in this MVP the parser will simply generate code for all lines matching /\A \s* \# \s{3,}/x after a triggering line macthing /\A \s* \# \s{3,} \# \s example: \s (.+)/x and potentially ended with the aforementioned /\A \s* \# \s{3,} \# \s end \s of \s example/x
This implies two thing
-
you can also write
RSpecexamples inside your code (inside comments) which are not part of rdoc -
you can use (although that would be missleading) an example like syntax in your
RDoccomments by spacing the ruby code block with only two spaces.
The context is taken from the last headline
First Examples
# example: equality
expect(41 + 1).to eq(42)
Defined Under Namespace
Modules: RSpecExampleGroup, Version Classes: Compiler, Context, Parser
Instance Attribute Summary collapse
-
#example_group ⇒ Object
readonly
Returns the value of attribute example_group.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
Instance Attribute Details
#example_group ⇒ Object (readonly)
Returns the value of attribute example_group.
68 69 70 |
# File 'lib/doc_rspec.rb', line 68 def example_group @example_group end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
68 69 70 |
# File 'lib/doc_rspec.rb', line 68 def lines @lines end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
68 69 70 |
# File 'lib/doc_rspec.rb', line 68 def path @path end |
Instance Method Details
#generate_specs ⇒ Object
70 71 72 73 |
# File 'lib/doc_rspec.rb', line 70 def generate_specs spec_definitions = DocRSpec::Parser.new(lines).parse DocRSpec::Compiler.new(spec_definitions:, example_group:, path:).compile end |