class QED::Step
A Step
consists of a flush region of text and the indented text the follows it. QED
breaks all demos down into step for evaluation.
Steps form a doubly linkes list, each having access to the step before and the step after them. Potentially this could be used by very advnaced matchers, to vary executation by earlier or later content of a demo.
Attributes
Previous step. @return [Step] previous step
Ths demo to which the step belongs. @return [Demo] demo
The steps example text, broken down into an array of ‘[line number, line text]` entries.
@return [Array<Array<Integer,String>]] example_lines
The step’s explaination text, broken down into an array of ‘[line number, line text]` entries.
@return [Array<Array<Integer,String>]] explain_lines
Next step. @return [Step] next step
Public Class Methods
Step
up a new step.
@param [Demo] demo
The demo to which the step belongs.
@param [Array<Array<Integer,String>]] explain_lines
The step's explaination text, broken down into an array of `[line number, line text]` entries.
@param [Array<Array<Integer,String>]] example_lines
The steps example text, broken down into an array of `[line number, line text]` entries.
@param [Step] last
The previous step in the demo.
# File lib/qed/step.rb, line 45 def initialize(demo, explain_lines, example_lines, last) #QED.all_steps << self @demo = demo @file = demo.file #@lines = [] @explain_lines = explain_lines @example_lines = example_lines @back_step = last @back_step.next_step = self if @back_step end
Public Instance Methods
Returns any extra arguments the step passes along to rules.
# File lib/qed/step.rb, line 171 def arguments [] end
# File lib/qed/step.rb, line 214 def assertive? @assertive ||= !text.strip.end_with?('^') end
Clean up the example text, removing unccesseary white lines and triple quote brackets, but keep indention intact.
# File lib/qed/step.rb, line 178 def clean_example str = example.chomp.sub(/\A\n/,'') if md = /\A["]{3,}(.*?)["]{3,}\Z/.match(str) str = md[1] end str.rstrip end
Is the example text code to be evaluated?
# File lib/qed/step.rb, line 126 def code? !data? && example? end
Any commentary ending in ‘:` will mark the example text as a plain text sample and not code to be evaluated.
# File lib/qed/step.rb, line 121 def data? @is_data ||= explain.strip.end_with?(':') end
# File lib/qed/step.rb, line 158 def example @example ||= ( if data? @example_lines.map{ |lineno, line| line }.join("") else tweak_code end ) end
Does the block have an example?
# File lib/qed/step.rb, line 152 def example? ! example.strip.empty? end
# File lib/qed/step.rb, line 147 def example_lineno @example_lines.first ? @example_lines.first.first : 1 end
Description text.
# File lib/qed/step.rb, line 85 def explain @explain ||= @explain_lines.map{ |lineno, line| line }.join("") end
# File lib/qed/step.rb, line 143 def explain_lineno @explain_lines.first ? @explain_lines.first.first : 1 end
Ths file to which the step belongs.
@return [String] file path
# File lib/qed/step.rb, line 75 def file demo.file end
A step is a heading if it’s description starts with a ‘=’ or ‘#’.
# File lib/qed/step.rb, line 115 def heading? @is_heading ||= (/\A[=#]/ =~ explain) end
TODO: object_hexid
# File lib/qed/step.rb, line 208 def inspect str = text[0,24].gsub("\n"," ") %[\#<Step:#{object_id} "#{str} ...">] end
First line of example text.
# File lib/qed/step.rb, line 131 def lineno @lineno ||= ( if @example_lines.first @example_lines.first.first elsif @explain_lines.first @explain_lines.first.first else 1 end ) end
When the text is sample text and passed to an adivce block, this provides the prepared form of the example text, removing white lines, triple quote brackets and indention.
# File lib/qed/step.rb, line 199 def sample_text str = example.tabto(0).chomp.sub(/\A\n/,'') if md = /\A["]{3,}(.*?)["]{3,}\Z/.match(str) str = md[1] end str.rstrip end
Full text of block including both explination and example text.
# File lib/qed/step.rb, line 80 def to_s (@explain_lines + @example_lines).map{ |lineno, line| line }.join("") end
# File lib/qed/step.rb, line 110 def type assertive? ? :test : :proc end
Protected Instance Methods
# File lib/qed/step.rb, line 221 def next_step=(n) @next_step = n end
Private Instance Methods
# File lib/qed/step.rb, line 228 def tweak_code code = @example_lines.map{ |lineno, line| line }.join("") #code.gsub!(/\n\s*\#\ ?\=\>(.*?)$/, ' == \1 ? assert(true) : assert(false, %{not returned -- \1})') # TODO: what kind of error ? #code.gsub!(/\s*\#\ ?\=\>(.*?)$/, ' == \1 ? assert(true) : assert(false, %{not returned -- \1})') code.gsub!(/\n\s*\#\ ?\=\>\s*(.*?)$/, '.must_return(\1)') code.gsub!(/\s*\#\ ?\=\>\s*(.*?)$/, '.must_return(\1)') code end