class Oktest::TestGenerator

Attributes

styleoption[R]

Public Class Methods

new(styleoption=nil) click to toggle source
# File lib/oktest.rb, line 2375
def initialize(styleoption=nil)
  @styleoption = styleoption
end

Public Instance Methods

generate(io) click to toggle source
# File lib/oktest.rb, line 2450
    def generate(io)
      #; [!5hdw4] generates test code.
      tree = parse(io)
      return <<END


require 'oktest'

Oktest.scope do

#{transform(tree, 1)}

end
END
    end
parse(io) click to toggle source
# File lib/oktest.rb, line 2380
def parse(io)
  #; [!5mzd3] parses ruby code.
  tree = _parse(io, [], nil)
  return tree
end
transform(tree, depth=1) click to toggle source
# File lib/oktest.rb, line 2411
def transform(tree, depth=1)
  #; [!te7zw] converts tree into test code.
  buf = []
  tree.each do |tuple|
    _transform(tuple, depth, buf)
  end
  buf.pop() if buf[-1] == "\n"
  return buf.join()
end

Private Instance Methods

_parse(io, tree, end_indent) click to toggle source
# File lib/oktest.rb, line 2386
def _parse(io, tree, end_indent)
  while (line = io.gets())
    case line
    when /^([ \t]*)end\b/
      return tree if $1 == end_indent
    when /^([ \t]*)(module|class|def) +(\w+[.:\w]*)/
      indent, keyword, topic = $1, $2, $3
      next if line =~ /\bend$/
      if keyword == 'def'
        topic = topic =~ /^self\./ ? ".#{$'}" : "\##{topic}"
      end
      newtree = []
      _parse(io, newtree, indent)
      tree << [indent, keyword, topic, newtree]
    when /^([ \t]*)\#[:;] (.*)/
      indent, keyword, spec = $1, 'spec', $2
      tree << [indent, keyword, spec]
    end
  end
  end_indent == nil  or
    raise "parse error: end_indent=#{end_indent.inspect}"
  return tree
end
_transform(tuple, depth, buf) click to toggle source
# File lib/oktest.rb, line 2421
def _transform(tuple, depth, buf)
  #; [!q5duk] supports 'unaryop' style option.
  unaryop = @styleoption == 'unaryop'
  indent  = '  ' * (depth - 1)
  keyword = tuple[1]
  if keyword == 'spec'
    _, _, spec = tuple
    escaped = spec.gsub(/"/, '\\\"')
    buf << "\n"
    buf << "#{indent}- spec(\"#{escaped}\")\n"    if unaryop
    buf << "#{indent}  spec \"#{escaped}\"\n" unless unaryop
  else
    _, _, topic, children = tuple
    topic += '()' if keyword == 'def'
    topic_ = keyword == 'def' ? "'#{topic}'" : topic
    buf << "\n"
    buf << "#{indent}+ topic(#{topic_}) do\n"     if unaryop
    buf << "#{indent}  topic #{topic_} do\n"  unless unaryop
    buf << "\n" unless keyword == 'def'
    children.each do |child_tuple|
      _transform(child_tuple, depth+1, buf)
    end
    buf << "\n"
    buf << "#{indent}  end  # #{topic}\n"
    buf << "\n"
  end
end