Class: DocRSpec

Inherits:
Object
  • Object
show all
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/debugging.rb,
lib/doc_rspec/context/example.rb,
lib/doc_rspec/rspec_example_group.rb

Overview

Code

can be found [at Codeberg](codeberg.org/lab419/doc_rspec)

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

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

This implies two thing

  • you can also write RSpec examples 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 RDoc comments 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)

Shortcuts

In addition to standard RSpec code, one can use 3 shorthand forms that are compiled as follows

==> equality

# example: ==> shorthand for eq

true => true
answer = 42
answer ==> 42

~> for match

# example: ~> shorthand for match

"alpha" ~> /\Aa.+a\z/

is! for be_

# example: is! shorthand for be 

require 'ostruct'
OpenStruct.new(ok?: true) is! ok

not! for not be_

# example: is! shorthand for be 

require 'ostruct'
OpenStruct.new(ok?: false) not! ok

Comments inside examples

# example: comments are removed

# so this works
expect(1).to eq(1)
# and this ==> too

Defined Under Namespace

Modules: Debugging, RSpecExampleGroup, Version Classes: Compiler, Context, Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#example_groupObject (readonly)

Returns the value of attribute example_group.



108
109
110
# File 'lib/doc_rspec.rb', line 108

def example_group
  @example_group
end

#linesObject (readonly)

Returns the value of attribute lines.



108
109
110
# File 'lib/doc_rspec.rb', line 108

def lines
  @lines
end

#pathObject (readonly)

Returns the value of attribute path.



108
109
110
# File 'lib/doc_rspec.rb', line 108

def path
  @path
end

Instance Method Details

#generate_specsObject



110
111
112
113
# File 'lib/doc_rspec.rb', line 110

def generate_specs
  spec_definitions = DocRSpec::Parser.new(lines).parse
  DocRSpec::Compiler.new(spec_definitions:, example_group:, path:).compile
end