comparison testing/utp_1.1/buildxml.rb @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents
children
comparison
equal deleted inserted replaced
43:bc767aaa99a8 44:409a22968d5e
1 #!/usr/bin/ruby
2
3 require 'ostruct'
4 require 'unittest'
5 require 'rexml/document'
6 include REXML
7
8 options = OpenStruct.new
9 options.outfile = ""
10 options.infile = ""
11 options.version = "unknown"
12
13 if ARGV[1]==nil then
14 puts "Usage: buildxml.rb <inputfile> <outputfile>"
15 exit
16 else
17 options.infile = ARGV[0]
18 options.outfile = ARGV[1]
19 end
20
21 class UnitTestProcessor
22 attr_accessor :ut
23
24 def initialize
25 @ut = UnitTest.new
26 @results = []
27 end
28
29 def result=(newResult)
30 testvalues = newResult.split(",")
31 @ut.path = getPath(testvalues)
32 @ut.title = getTitle(testvalues)
33 @ut.code = getCode(testvalues)
34 result = Result.new
35 result.syntax = testvalues[1]
36 result.algorithm = testvalues[2]
37 result.date = Date.new(testvalues[4].to_i)
38 result.number = testvalues[3].to_i
39 @ut.results << result
40 end
41
42 # generates the path out of the test name
43 def getPath(testvalues)
44 parts = testvalues[0].split("_")
45 folder = parts[1]
46 parts.pop
47 filename = parts.join("_") + ".m"
48 "utps/" + folder + "/" + filename
49 end
50
51 # generates the title out of the test name
52 def getTitle(testvalues)
53 parts = testvalues[0].split("_")
54 parts.pop
55 parts.shift
56 #parts[0].upcase!
57 parts.join(" ")
58 end
59
60 # generates the title out of the test name
61 def getCode(testvalues)
62 parts = testvalues[0].split("_")
63 parts.pop
64 parts.join("_")
65 end
66
67 def parseFile
68 begin
69 if !File.exists? @ut.path then
70 return
71 end
72 inFile = File.new(@ut.path)
73 currentTest = SubTest.new
74 while line = inFile.readline.chomp
75 # TODO: strip HTML-Tags
76 # remove trailing spaces
77 line.rstrip!
78
79 if line.match /[%]+[ ]+UTP_[0-9]+/ then
80 currentTest = SubTest.new
81 parts = line.split("_")
82 currentTest.number = parts[1].to_i
83 end
84
85 if line.match(/[%]+[ ]+END[ ]+UTP_[0-9]+/) and currentTest.number > 0 then
86 @ut.tests << currentTest
87 end
88
89 if line.match "</MethodDescription>" then writeTo = "none" end
90 if line.match "</SyntaxDescription>" then writeTo = "none" end
91 if line.match "</TestDescription>" then writeTo = "none" end
92 if line.match "</AlgoDescription>" then writeTo = "none" end
93 if line.match "</SyntaxCode>" then writeTo = "none" end
94 if line.match "</AlgoCode>" then writeTo = "none" end
95
96
97
98 case writeTo
99 when "none" then
100 a = 1
101 when "algoCode" then
102 currentTest.algoCode += line + "\n"
103 when "algoDescription" then
104 currentTest.algoDescription += removeComment line
105 when "syntaxCode" then
106 currentTest.syntaxCode += line + "\n"
107 when "syntaxDescription" then
108 currentTest.syntaxDescription += removeComment line
109 when "methodDescription" then
110 @ut.methodDescription += removeComment line
111 when "testDescription" then
112 currentTest.testDescription += removeComment line
113 end
114
115 if line.match "<MethodDescription>" then writeTo = "methodDescription" end
116 if line.match "<SyntaxDescription>" then writeTo = "syntaxDescription" end
117 if line.match "<TestDescription>" then writeTo = "testDescription" end
118 if line.match "<AlgoDescription>" then writeTo = "algoDescription" end
119 if line.match "<SyntaxCode>" then writeTo = "syntaxCode" end
120 if line.match "<AlgoCode>" then writeTo = "algoCode" end
121
122 end
123 rescue EOFError
124 inFile.close
125 end
126 end
127
128 # The analysis of a single line of the .m file
129 def removeComment(line)
130 line.strip!
131 while line.match /^%/
132 # not elegant, but works in every realistic case (cut off at 100000 chars)
133 line = line[1,100000]
134 line.strip!
135 end
136 if line.length>0 then
137 line = line + "<br />"
138 end
139 line
140 end
141
142 # Merge Results and Test for easier access
143 def mergeTestsAndResults
144 @ut.tests.each do |t|
145 @ut.results.each do |r|
146 if t.number == r.number then
147 t.date = r.date
148 t.syntaxResult = r.syntax.to_i
149 t.algoResult = r.algorithm.to_i
150 if t.syntaxResult==0 or t.algoResult==0 then
151 @ut.tainted = true
152 end
153 end
154 end
155 end
156 end
157 end
158
159 infile = File.open(options.infile, 'r');
160 outfile = File.open(options.outfile, 'w');
161
162 tests = []
163
164 begin
165 while line = infile.readline.chomp
166 testvalues = line.split(",")
167 parts = testvalues[0].split("_")
168 parts.pop
169 tests << parts.join("_")
170 end
171 rescue EOFError
172 infile.close
173 end
174
175 tests = tests.uniq
176
177 # For testing: tests fet only for the first test
178 #tests = []
179 #tests << "utp_ao_join_fsdata"
180 utps = []
181 puts "Collecting data ..."
182 tests.each do |t|
183 inFile = File.open(options.infile, 'r')
184 utp = UnitTestProcessor.new
185 begin
186 while line = inFile.readline.chomp
187 if line.match t then
188 utp.result = line
189 end
190 end
191 rescue EOFError
192 inFile.close
193 end
194 utps << utp
195 end
196
197 unittests = []
198
199 i=1
200
201 utps.each do |u|
202 puts "Parsing file " + i.to_s + " of " + utps.length.to_s + " (" + u.ut.path + ") ..."
203 i += 1
204 u.parseFile
205 u.mergeTestsAndResults
206 unittests << u.ut
207 end
208
209 #if(options.outfile HERE MATCHES DAT) then
210 Marshal.dump(unittests, outfile)
211 #elseif(optione.outfile. HERE MATCHES XML then
212 # doc = Document.new()
213 # root = Element.new("Report")
214 # root.add_attribute 'date', Time.now.to_s
215 # root.add_attribute 'version', options.version
216 # unittests.each do |u|
217 # testEl = Element.new("
218 # end
219 #end
220 outfile.close