Class: TestML::Lite
- Inherits:
-
Test::Unit::TestCase
- Object
- Test::Unit::TestCase
- TestML::Lite
- Defined in:
- lib/testml/lite.rb
Defined Under Namespace
Modules: TestCases
Instance Method Summary collapse
- #_assert_testml ⇒ Object
- #_evaluate(expr, block) ⇒ Object
- #_get_blocks(expr, blocks = @testml) ⇒ Object
- #_get_token(expr) ⇒ Object
- #_parse_expr(expr) ⇒ Object
- #_parse_tml(string) ⇒ Object
- #_run_runner(name) ⇒ Object
- #assert_equal(got, want, block) ⇒ Object
- #assert_match(got, want, block) ⇒ Object
- #Catch(any = nil) ⇒ Object
- #data(input) ⇒ Object
- #eval(expr, callback = nil) ⇒ Object
- #label(text) ⇒ Object
- #run_test(block, expr) ⇒ Object
Instance Method Details
#_assert_testml ⇒ Object
86 87 88 89 90 |
# 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# 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) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# 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) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# 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) ⇒ Object
81 82 83 84 |
# 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) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# 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) ⇒ Object
69 70 71 72 |
# 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 |
#Catch(any = nil) ⇒ Object
74 75 76 77 78 79 |
# 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. end |
#data(input) ⇒ Object
30 31 32 33 34 35 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# 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) ⇒ Object
37 38 39 |
# File 'lib/testml/lite.rb', line 37 def label text @label = text end |
#run_test(block, expr) ⇒ Object
52 53 54 55 56 |
# 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 |