forked from jop-devel/jop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_inline_test.rb
51 lines (48 loc) · 1.57 KB
/
run_inline_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# run an inline test specified in e.g., a java file
#
# syntax 1: $test$> command
# run the command, saving stdout and stderr
# syntax 2: $stdout$> expect
# compare the string expect to the next line of output in the previously run command
# syntax 3: $grep$> pre-context ^ expect $ post-context
# compare the string expect to the next line of standard output matching context
def match(ctx, expect, actual)
actual = actual.strip
if(expect != actual)
$stderr.puts("! #{ctx} : expected > '#{expect}'")
$stderr.puts("! #{ctx} : actual > '#{actual}'")
else
$stderr.puts("| #{ctx} : match > '#{expect}'")
end
end
file = ARGV[0]
stdin, stdout, stderr = nil,nil,nil
File.readlines(ARGV[0]).each do |l|
actual,expect="",""
if l =~ /\$test\$>(.*)$/
cmd = $1.strip
$stderr.puts "| executing > '#{cmd}'"
stdout.close if stdout
stderr.close if stderr
if ! system("#{cmd} 2>.err.log >.log")
$stderr.puts "! system failed > '#{cmd}'"
end
stdout = File.open(".log")
stderr = File.open(".err.log")
elsif l =~ /\$stdout\$>(.*)$/
expect = $1.strip
begin
actual = stdout.gets
end while actual && actual.strip == ""
match("stdout",expect,actual||"")
elsif l =~ /\$grep\$>(.*?)\^(.*?)(\$.*)?$/
prectx = $1.strip
expect = $2.strip
postctx = ($3||"$")[1..-1].strip
context = Regexp.new(Regexp.escape(prectx)+"(.*)"+Regexp.escape(postctx))
while line = stdout.gets
(actual = $1 ; break) if context =~ line.strip
end
match("grep #{prectx} ^$ #{postctx}",expect,actual)
end
end