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

back_step[R]

Previous step. @return [Step] previous step

demo[R]

Ths demo to which the step belongs. @return [Demo] demo

example_lines[R]

The steps example text, broken down into an array of ‘[line number, line text]` entries.

@return [Array<Array<Integer,String>]] example_lines

explain_lines[R]

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[R]

Next step. @return [Step] next step

Public Class Methods

new(demo, explain_lines, example_lines, last) click to toggle source

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

arguments() click to toggle source

Returns any extra arguments the step passes along to rules.

# File lib/qed/step.rb, line 171
def arguments
  []
end
assertive?() click to toggle source
# File lib/qed/step.rb, line 214
def assertive?
  @assertive ||= !text.strip.end_with?('^')
end
clean_example() click to toggle source

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
code()
Alias for: example
code?() click to toggle source

Is the example text code to be evaluated?

# File lib/qed/step.rb, line 126
def code?
  !data? && example?
end
data()
Alias for: example
data?() click to toggle source

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
description()

Alternate term for explain.

Alias for: explain
example() click to toggle source
# File lib/qed/step.rb, line 158
def example
  @example ||= (
    if data?
      @example_lines.map{ |lineno, line| line }.join("")
    else
      tweak_code
    end
  )
end
Also aliased as: code, data
example?() click to toggle source

Does the block have an example?

# File lib/qed/step.rb, line 152
def example?
  ! example.strip.empty?
end
Also aliased as: has_example?
example_lineno() click to toggle source
# File lib/qed/step.rb, line 147
def example_lineno
  @example_lines.first ? @example_lines.first.first : 1
end
explain() click to toggle source

Description text.

# File lib/qed/step.rb, line 85
def explain
  @explain ||= @explain_lines.map{ |lineno, line| line }.join("")
end
Also aliased as: description, text
explain_lineno() click to toggle source
# File lib/qed/step.rb, line 143
def explain_lineno
  @explain_lines.first ? @explain_lines.first.first : 1
end
file() click to toggle source

Ths file to which the step belongs.

@return [String] file path

# File lib/qed/step.rb, line 75
def file
  demo.file
end
has_example?()
Alias for: example?
heading?() click to toggle source

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
inspect() click to toggle source

TODO: object_hexid

# File lib/qed/step.rb, line 208
def inspect
  str = text[0,24].gsub("\n"," ")
  %[\#<Step:#{object_id} "#{str} ...">]
end
lineno() click to toggle source

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
sample_text() click to toggle source

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
text()

@deprecated

Alias for: explain
to_s() click to toggle source

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
type() click to toggle source
# File lib/qed/step.rb, line 110
def type
  assertive? ? :test : :proc
end

Protected Instance Methods

next_step=(n) click to toggle source
# File lib/qed/step.rb, line 221
def next_step=(n)
  @next_step = n
end

Private Instance Methods

tweak_code() click to toggle source
# 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