program for running unit tests
[swftools.git] / lib / as3 / runtests.py
1 #!/usr/bin/python
2 #
3 # runtests.py
4 #
5 # Run compiler unit tests
6 #
7 # Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 import sys
24 import os
25 import time
26 import subprocess
27
28 def check(s):
29     row = None
30     ok = 0
31     for line in s.split("\n"):
32         if line.startswith("[") and line.endswith("]"):
33             continue
34         if not line.strip():
35             continue
36         if not line.startswith("ok"):
37             return 0
38         if line.startswith("ok "):
39             if "/" not in line:
40                 return 0
41             i = line.index('/')
42             nr,len = int(line[3:i]),int(line[i+1:])
43             if nr<1 or nr>len:
44                 return 0
45             if not row:
46                 row = [0]*len
47             row[nr-1] = len
48         elif line == "ok":
49             ok = 1
50     if ok:
51         return not row
52     if row:
53         return 0 not in row
54     return 0
55
56 def runcmd(cmd,args,output,wait):
57     #fo = open(tempfile, "wb")
58     fo= os.tmpfile()
59     p = subprocess.Popen([cmd] + args, executable=cmd, stdout=fo, stderr=fo)
60     ret = -1
61     for i in range(wait*10):
62         ret = p.poll()
63         if ret is not None:
64             break
65         time.sleep(0.1)
66     else:
67         os.kill(p.pid, 9)
68         os.system("killall -9 "+cmd)
69   
70     fo.seek(0)
71     output = fo.read()
72     fo.close()
73
74     return ret,output
75
76 class Test:
77     def __init__(self, nr, file):
78         self.nr = nr
79         self.file = file
80         self.flash_output = None
81         self.flash_error = None
82         self.compile_output = None
83         self.compile_error = None
84         self.compile()
85         if not self.compile_error:
86             self.run()
87
88     def compile(self):
89         ret,output = runcmd("./parser",[self.file],"/tmp/abctest.txt",wait=60)
90         self.compile_error = 0
91         self.compile_output = output
92         if ret&0xff00:
93             self.compile_error = 1
94
95     def run(self):
96         ret,output = runcmd("flashplayer",["abc.swf"],"/tmp/abctest.txt",wait=1)
97         os.system("killall flashplayer")
98         self.flash_output = output
99         
100         if not check(self.flash_output):
101             self.flash_error = 1
102
103     def doprint(self):
104         def r(s,l):
105             if(len(s)>=l):
106                 return s
107             return (" "*(l-len(s))) + s
108         def l(s,l):
109             if(len(s)>=l):
110                 return s
111             return s + (" "*(l-len(s)))
112
113         print r(str(self.nr),3)," ",
114         if self.compile_error:
115             print "err"," - ",
116         else:
117             print "ok ",
118             if not self.flash_error:
119                 print "ok ",
120             else:
121                 print "err",
122         print " ",
123         print file
124
125     def doprintlong(self):
126         print self.nr, self.file
127         print "================================"
128         print test.compile_output
129         print "================================"
130         print test.flash_output
131         print "================================"
132
133 checknum=-1
134 if len(sys.argv)>1:
135     checknum = int(sys.argv[1])
136
137 for nr,file in sorted(enumerate(os.listdir("ok"))):
138     if not file.endswith(".as"):
139         continue
140     file = os.path.join("ok", file)
141     if checknum>=0 and nr!=checknum:
142         continue
143     test = Test(nr,file)
144
145     if checknum!=nr:
146         test.doprint()
147     else:
148         test.doprintlong()
149
150