-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaska.rb
148 lines (119 loc) · 3.41 KB
/
yaska.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'open3'
require 'Colorify'
class Yaska
include Colorify
attr_accessor :cwcount
attr_accessor :rwcount
attr_accessor :firsttime
@@GRAMMARDIR = "grammars"
@@GRAMMARFILE = "Test.g"
@@PACKAGE = "com.yaska"
@@TESTFILE = "TestYaska"
def initialize
@cwcount = 0
@rwcount = 0
@firsttime = true
end
# compile the grammar, spit out any warnings
# return the warning count
def compile
stdin, stdout, stderr = Open3.popen3("java org.antlr.Tool #{@@GRAMMARDIR}/#{@@GRAMMARFILE}")
eput = []
stderr.each do |line| eput << line end
ecount = 0
eput.each do |line|
puts line
if !line.match(/error|warning/).nil? then ecount += 1 end
end
IO.popen("javac #{@@GRAMMARDIR}/#{File.basename(@@GRAMMARFILE, '.g')}*.java") do |io|
io.each_line {}
end
return ecount
end
# main event loop
# check for file changes every 2 seconds
# compile, test, check for any changes
def start
ttop = nil # last unit-test modification-time
gtop = nil # last grammar modification-time
puts colorBlue("Testing #{@@GRAMMARFILE}")
loop do
tcurrent = File.mtime "#{@@TESTFILE}.java"
gcurrent = File.mtime "#{@@GRAMMARDIR}/#{@@GRAMMARFILE}"
if ttop.nil? || ttop < tcurrent || gtop < gcurrent
runtests
gtop = gcurrent
ttop = tcurrent
end
sleep 2
end
end
# compile and run the tests
def runtests
comwarn = compile
# run
stdin, stdout, stderr = Open3.popen3("javac #{@@TESTFILE}.java; java org.junit.runner.JUnitCore #{@@PACKAGE}.#{@@TESTFILE}")
# output stdout (errors)
oput = []
fails = 0
stdout.each do |line|
oput << line
if line.match("Failures") then
fails = line.split("Failures: ")[1].rstrip
end
end
sout = oput.join
oput.each do |line|
puts line
end
# out put stderr (warnings)
eput = []
stderr.each do |line| eput << line end
runwarn = eput.count
eput.each do |line|
puts line
end
# testing errors
if sout.match("FAILURES!!!") then
puts colorRed("Broke #{fails} Test(s)!")
else
puts colorGreen("Passing all Tests!")
end
# compilation warnings
puts colorBlack("Compile-Time Warnings")
puts colorBlack("-----------------")
msg = "#{comwarn} Warnings"
if @cwcount < comwarn and !@firsttime then
puts colorRed("#{msg}: Generating more!")
elsif @cwcount.eql? comwarn and !@firsttime then
puts colorYellow("#{msg}: Generating the same number!")
elsif !@firsttime
puts colorGreen("#{msg}: Generating less!")
elsif @firstttime and comwarn.eql? 0
puts colorGreen("#{msg}: No Errors!")
else
puts colorRed("#{msg}: Uh-oh!")
end
@cwcount = comwarn
# runtime warnings
puts colorBlack("Runtime Warnings")
puts colorBlack("-----------------")
msg = "#{runwarn} Warnings"
if @rwcount < runwarn and !@firsttime then
puts colorRed("#{msg}: Generating more!")
elsif @rwcount.eql? runwarn and !@firsttime then
puts colorYellow("#{msg}: Generating the same number!")
elsif !@firsttime
puts colorGreen("#{msg}: Generating less!")
elsif @firsttime and runwarn.eql? 0
puts colorGreen("#{msg}: No errors!")
else
puts colorRed("#{msg}: Uh-oh!")
end
@rwcount = runwarn
# set firsttime flag
@firsttime = false
end
end
ya = Yaska.new
ya.start