added as3 compiler objects
[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             if row[nr-1]:
48                 return 0
49             row[nr-1] = 1
50         elif line == "ok":
51             ok = 1
52     if ok:
53         return not row
54     if row:
55         return 0 not in row
56     return 0
57
58 def runcmd(cmd,args,wait):
59     #fo = open(tempfile, "wb")
60     fo= os.tmpfile()
61     p = subprocess.Popen([cmd] + args, executable=cmd, stdout=fo, stderr=fo)
62     ret = -1
63     for i in range(wait*10):
64         ret = p.poll()
65         if ret is not None:
66             break
67         time.sleep(0.1)
68     else:
69         os.kill(p.pid, 9)
70         os.system("killall -9 "+cmd)
71   
72     fo.seek(0)
73     output = fo.read()
74     fo.close()
75     return ret,output
76
77 class TestBase:
78     def __init__(self, nr, file, run):
79         self.nr = nr
80         self.dorun = run
81         self.file = file
82         self.flash_output = None
83         self.flash_error = None
84         self.compile_output = None
85         self.compile_error = None
86
87     def compile(self):
88         try: os.unlink("abc.swf");
89         except: pass
90         ret,output = runcmd("./parser",[self.file],wait=60)
91         self.compile_error = 0
92         self.compile_output = output
93         if ret:
94             self.compile_error = 1
95         if not os.path.isfile("abc.swf"):
96             self.compile_error = 1
97
98     def run(self):
99         ret,output = runcmd("flashplayer",["abc.swf"],wait=1)
100         os.system("killall flashplayer")
101         self.flash_output = output
102         
103         if not check(self.flash_output):
104             self.flash_error = 1
105
106     def doprint(self):
107         print self.r(str(self.nr),3)," ",
108         if self.compile_error:
109             if self.dorun:
110                 print "err"," - ",
111             else:
112                 print "err","   ",
113         else:
114             print "ok ",
115             if self.dorun:
116                 if not self.flash_error:
117                     print "ok ",
118                 else:
119                     print "err",
120             else:
121                 print "   ",
122         print " ",
123         print self.file
124
125     def doprintlong(self):
126         print self.nr, self.file
127         print "================================"
128         print "compile:", (test.compile_error and "error" or "ok")
129         print test.compile_output
130         if not self.dorun:
131             return
132         print "================================"
133         print "run:", (test.flash_error and "error" or "ok")
134         print test.flash_output
135         print "================================"
136
137     def r(self,s,l):
138         if(len(s)>=l):
139             return s
140         return (" "*(l-len(s))) + s
141     def l(self,s,l):
142         if(len(s)>=l):
143             return s
144         return s + (" "*(l-len(s)))
145
146 class Test(TestBase):
147     def __init__(self, nr, file):
148         TestBase.__init__(self, nr, file, run=1)
149         self.compile()
150         if not self.compile_error:
151             self.run()
152
153 class ErrTest(TestBase):
154     def __init__(self, nr, file):
155         TestBase.__init__(self, nr, file, run=0)
156         self.compile()
157         self.compile_error = not self.compile_error
158
159 class Suite:
160     def __init__(self, dir):
161         self.dir = dir
162         self.errtest = "err" in dir
163     def run(self, nr):
164         print "-"*40,"tests \""+self.dir+"\"","-"*40
165         for file in sorted(os.listdir(self.dir)):
166             if not file.endswith(".as"):
167                 continue
168             nr = nr + 1
169             file = os.path.join(self.dir, file)
170             if checknum>=0 and nr!=checknum:
171                 continue
172             if self.errtest:
173                 test = ErrTest(nr,file)
174             else:
175                 test = Test(nr,file)
176             if checknum!=nr:
177                 test.doprint()
178             else:
179                 test.doprintlong()
180         return nr
181
182 checknum=-1
183 if len(sys.argv)>1:
184     checknum = int(sys.argv[1])
185
186 nr = 0
187 nr = Suite("err").run(nr)
188 nr = Suite("ok").run(nr)
189