class TestML::Lite

Public Instance Methods

Catch(any=nil) click to toggle source
# File lib/testml/lite.rb, line 74
def Catch any=nil
  fail "Catch called, but no error occurred" unless $error
  error = $error
  $error = nil
  return error.message
end
_assert_testml() click to toggle source
# File lib/testml/lite.rb, line 86
def _assert_testml
  return if defined? @testml
  raise "No testml data provided" unless $testml_data[@test_name]
  data $testml_data[@test_name]
end
_evaluate(expr, block) click to toggle source
# File lib/testml/lite.rb, line 92
def _evaluate expr, block
  expr = ['', expr] if expr.kind_of? String
  func = expr.first
  args = expr[1..expr.length-1].collect do |ex|
    if ex.kind_of? Array
      _evaluate ex, block
    elsif ex =~ /\A\*(\w+)\z/
      block[:points][$1]
    else
      ex
    end
  end
  # TODO @error
  return if $error and func != 'Catch'
  return args.first if func.empty?
  args << block if func =~ /^assert_/
  begin
    return method(func).call(*args)
  rescue => e
    $error = e
  end
end
_get_blocks(expr, blocks=@testml) click to toggle source
# File lib/testml/lite.rb, line 115
def _get_blocks expr, blocks=@testml
  want = expr.flatten.grep(/^\*/).collect{|ex| ex.gsub /^\*/, ''}
  only = blocks.select{|block| block['ONLY']}
  blocks = only unless only.empty?
  final = []
  blocks.each do |block|
    next if block['SKIP']
    ok = true
    want.each do |w|
      unless block[:points][w]
        ok = false
        break
      end
    end
    if ok
      final << block
      break if block['LAST']
    end
  end
  return final
end
_get_token(expr) click to toggle source
# File lib/testml/lite.rb, line 158
def _get_token expr
  if expr.sub! /^(\w+)\(([^\)]+)\)\.?/, ''
    token, args = [$1], $2
    token.concat(
      args.split(/,\s*/).map {|t| t.sub /^(['"])(.*)\1$/, '\2'}
    )
  elsif expr.sub! /^\s*(==|~~)\s*/, ''
    token = $1
  elsif expr.sub! /^([\*\w]+)\.?/, ''
    token = $1
  end
  return token
end
_parse_expr(expr) click to toggle source
# File lib/testml/lite.rb, line 137
def _parse_expr expr
  left, op, right = [], nil, nil
  side = left
  while expr.length != 0
    t = _get_token expr
    if t =~ /^(==|~~)$/
      op = t == '==' ? 'assert_equal' : 'assert_match'
      left = side
      side = right = []
    else
      side = [side] if side.size >= 2
      side.unshift t
    end
  end
  right = side if right
  return left unless right
  left = left.first if left.size == 1
  right = right.first if right.size == 1
  return [op, left, right]
end
_parse_tml(string) click to toggle source
# File lib/testml/lite.rb, line 172
def _parse_tml string
  string.gsub! /^#.*\n/, ''
  string.gsub! /^\\/, ''
  string.gsub! /^\s*\n/, ''
  blocks = string.split /(^===.*?(?=^===|\z))/m
  blocks.reject!{|b| b.empty?}
  blocks.each do |block|
    block.gsub! /\n+\z/, "\n"
  end

  array = []
  blocks.each do |string_block|
    block = {}
    string_block.gsub! /^===\ +(.*?)\ *\n/, '' \
      or fail "No block title! #{string_block}"
    block[:title] = $1
    while !string_block.empty? do
      if string_block.gsub! /\A---\ +(\w+):\ +(.*)\n/, '' or
         string_block.gsub! /\A---\ +(\w+)\n(.*?)(?=^---|\z)/m, ''
        key, value = $1, $2
      else
        raise "Failed to parse TestML string:\n#{string_block}"
      end
      block[:points] ||= {}
      block[:points][key] = value

      if key =~ /^(ONLY|SKIP|LAST)$/
        block[key] = true
      end
    end
    array << block
  end
  return array
end
_run_runner(name) click to toggle source
# File lib/testml/lite.rb, line 81
def _run_runner name
  @test_name = name
  $testml_runners[name].call(self)
end
assert_equal(got, want, block) click to toggle source
Calls superclass method
# File lib/testml/lite.rb, line 58
def assert_equal got, want, block
  label = block.kind_of?(String) ? block : block[:title]
  if got != want
    on_fail if respond_to? 'on_fail'
    File.open('/tmp/got', 'w') {|f| f.write got}
    File.open('/tmp/want', 'w') {|f| f.write want}
    puts `diff -u /tmp/want /tmp/got`
  end
  super want, got, label
end
assert_match(got, want, block) click to toggle source
Calls superclass method
# File lib/testml/lite.rb, line 69
def assert_match got, want, block
  label = block.kind_of?(String) ? block : block[:title]
  super want, got, label
end
data(input) click to toggle source
# File lib/testml/lite.rb, line 30
def data input
  unless input.match /\n/
    File.open(input, 'r') {|f| input = f.read}
  end
  @testml = _parse_tml input
end
eval(expr, callback=nil) click to toggle source
# File lib/testml/lite.rb, line 41
def eval expr, callback=nil
  _assert_testml
  expr = _parse_expr expr if expr.kind_of? String
  callback ||= method 'run_test'
  _get_blocks(expr).each do |block|
    $error = nil
    callback.call(block, expr)
    raise $error if $error
  end
end
label(text) click to toggle source
# File lib/testml/lite.rb, line 37
def label text
  @label = text
end
run_test(block, expr) click to toggle source
# File lib/testml/lite.rb, line 52
def run_test block, expr
  expr = _parse_expr expr if expr.kind_of? String
  block = _get_blocks(expr, [block]).first or return
  _evaluate expr, block
end