Mercurial > projects > dang
comparison tests/run.d @ 134:570a4917413a
Changed the test-program.
Most important difference is that a single count is printed at the end now
(easier to remember/compare).
Also noticed that our tests give different results in debug and release mode, I
will have to look into that.
author | Anders Halager <halager@gmail.com> |
---|---|
date | Wed, 09 Jul 2008 12:59:57 +0200 |
parents | a49bb982a7b0 |
children | 1e48315c36fc |
comparison
equal
deleted
inserted
replaced
133:9c48871eb816 | 134:570a4917413a |
---|---|
1 // skip | |
1 module run.d; | 2 module run.d; |
2 | 3 |
3 import tango.io.Stdout, | 4 import tango.core.Array, |
4 tango.core.Array, | 5 tango.io.FileConduit, |
5 tango.io.FilePath, | 6 tango.io.FileScan, |
6 tango.io.GrowBuffer, | 7 tango.io.Stdout, |
7 tango.io.UnicodeFile, | 8 tango.sys.Process, |
8 tango.io.stream.BufferStream, | 9 tango.text.Ascii, |
9 tango.text.Util, | 10 tango.text.Regex, |
10 tango.io.protocol.Reader, | 11 tango.text.Util; |
11 tango.io.protocol.Writer, | |
12 tango.text.Unicode, | |
13 tango.sys.Process; | |
14 | 12 |
13 // -- Settings -- | |
14 char[] compiler = "./Dang"; | |
15 char[] test_folder = "tests"; | |
16 char[] valid_filenames = r"^[^.].*"; | |
17 bool print_expected = false; | |
15 | 18 |
16 enum | 19 // the tests can be sorted by one of the following functions |
20 bool nameSort (FilePath a, FilePath b) { return a.name < b.name; } | |
21 bool pathSort (FilePath a, FilePath b) { return a.toString < b.toString; } | |
22 bool modifiedSort(FilePath a, FilePath b) { return a.modified < b.modified; } | |
23 bool createdSort (FilePath a, FilePath b) { return a.created < b.created; } | |
24 const sortBy = &pathSort; | |
25 | |
26 // -- end of settings | |
27 | |
28 enum TestResult | |
17 { | 29 { |
18 SuccessSuccess, | 30 Skipped, |
19 SuccessFailure, | 31 Expected, |
20 FailureSuccess, | 32 Unexpected |
21 FailureFailure, | |
22 } | 33 } |
23 | |
24 char[] prog = "./Dang"; | |
25 | 34 |
26 void main(char[][] args) | 35 void main(char[][] args) |
27 { | 36 { |
28 auto cPath = FilePath("tests"); | 37 scope scan = new FileScan; |
38 scope regex = new Regex(valid_filenames); | |
39 // Return true for files/folders to include | |
40 bool filter(FilePath p, bool isDir) | |
41 { | |
42 if (isDir) | |
43 return p.name[0] != '.'; | |
44 else | |
45 return p.ext == "d" && regex.test(p.name); | |
46 } | |
47 scan.sweep(test_folder, &filter, true); | |
48 FilePath[] files = scan.files; | |
49 int total_tests = files.length; | |
29 | 50 |
30 ubyte success_success, success_failure, failure_failure, failure_success; | 51 // Sort the result by the chosen function - default is the full path |
52 sort(files, sortBy); | |
31 | 53 |
32 foreach( path ; cPath.toList((FilePath path, bool isFolder){return isFolder;})) | 54 int[TestResult.max + 1] results = 0; |
55 foreach (i, ref test; files) | |
33 { | 56 { |
34 Stdout(path.name)(":").newline; | 57 begin_test(i + 1, total_tests, test.name); |
35 auto paths = path.toList( | 58 TestResult res = run_test(test); |
36 (FilePath path, bool isFolder) | 59 results[res] += 1; |
37 { | 60 end_test(); |
38 if(path.ext == "d" && path.name[0] != '.') | 61 } |
39 return true; | 62 Stdout.format("\r{,80}\r", " "); |
40 return false; | 63 Stdout.newline; |
41 }); | 64 int good = TestResult.Expected; |
42 sort(paths, (FilePath a, FilePath b) { return a.name < b.name; }); | 65 int bad = TestResult.Unexpected; |
43 foreach (p ; paths) | 66 int tests_run = results[good] + results[bad]; |
44 { | 67 Stdout.formatln("{}/{} tests failed", results[bad], tests_run); |
45 auto test = new Test(p); | 68 } |
46 ubyte result = test.run(); | |
47 | 69 |
48 switch(result) | 70 void begin_test(int number, int total_tests, char[] name) |
71 { | |
72 char[60] progressbar = ' '; | |
73 int progress = number*progressbar.length/total_tests; | |
74 progressbar[0 .. progress] = '='; | |
75 progressbar[progress-1] = '>'; | |
76 Stdout.format("\r{}% - [{}]", 1e2 * number / total_tests, progressbar); | |
77 Stdout.flush(); | |
78 //Thread.sleep(0.05); | |
79 } | |
80 | |
81 void end_test() { } | |
82 | |
83 enum { | |
84 NoFail, | |
85 CompiletimeFail, | |
86 RuntimeFail | |
87 } | |
88 | |
89 private int min(int a, int b) { return a < b? a : b; } | |
90 TestResult run_test(ref FilePath p) | |
91 { | |
92 auto file = new FileConduit(p.toString(), FileConduit.ReadExisting); | |
93 char[256] content; | |
94 int len = file.read(content); | |
95 file.close(); | |
96 char[] line = content[0 .. min(len, content.find('\n'))]; | |
97 | |
98 bool compile = true; | |
99 int fail = NoFail; | |
100 if (line.length >= 2 && line[0 .. 2] == "//") | |
101 { | |
102 foreach (command; line[2 .. $].delimiters(",;")) | |
103 switch (toLower(substitute(command, " ", ""))) | |
49 { | 104 { |
50 case SuccessSuccess: | 105 case "skip", "dontcompile": |
51 success_success++; | 106 compile = false; |
52 break; | 107 break; |
53 case SuccessFailure: | 108 case "fail", "compilefail", |
54 success_failure++; | 109 "compiletimefail", "failatcompiletime": |
110 fail = CompiletimeFail; | |
55 break; | 111 break; |
56 case FailureFailure: | 112 case "runtime", "runtimefail", "failatruntime": |
57 failure_failure++; | 113 fail = RuntimeFail; |
58 break; | 114 Stderr("== Compiled tests will not be run! ==").newline; |
59 case FailureSuccess: | 115 return TestResult.Skipped; |
60 failure_success++; | 116 default: |
61 break; | 117 break; |
62 } | 118 } |
63 | |
64 } | |
65 } | 119 } |
66 | 120 |
67 Stdout().newline.newline() | 121 if (compile) |
68 ("Result:").newline() | 122 { |
69 (" - Success/Success: ")(success_success).newline() | 123 auto process = new Process(compiler, "--gen-llvm", p.toString()); |
70 (" - Success/Failure: ")(success_failure).newline() | 124 process.execute(); |
71 (" - Failure/Failure: ")(failure_failure).newline() | 125 auto result = process.wait(); |
72 (" - Failure/Success: ")(failure_success).newline; | 126 return resultOf(p, result.status, fail); |
127 } | |
128 | |
129 return TestResult.Skipped; | |
73 } | 130 } |
74 | 131 |
75 class Test | 132 private TestResult resultOf(FilePath p, int result, int expected) |
76 { | 133 { |
77 enum TestValue | 134 char[] good(char[] s) |
78 { | 135 { |
79 Success = 0, | 136 version (Posix) |
80 Lexer = 2, | 137 return "\033[1;32m" ~ s ~ "\033[m"; |
81 Parser = 3, | 138 else |
82 Gen = 4, | 139 return s; |
83 | |
84 Fail = 100 | |
85 } | 140 } |
86 | 141 |
87 FilePath target; | 142 char[] bad(char[] s) |
88 | |
89 TestValue[int] testValues; | |
90 | |
91 public this(FilePath target) | |
92 { | 143 { |
93 this.target = target; | 144 s = s ~ " - Unexpected"; |
145 version (Posix) | |
146 return "\033[1;31m" ~ s ~ "\033[m"; | |
147 else | |
148 return s; | |
94 } | 149 } |
95 | 150 |
96 public ubyte run() | 151 bool unexpected = (result != expected); |
152 auto f = unexpected? &bad : &good; | |
153 char[] s = (result == 0)? "SUCCESS" : "FAILURE"; | |
154 // always print if unexpeted, otherwise check the settings | |
155 if (unexpected || print_expected) | |
97 { | 156 { |
98 auto process = new Process(prog,"--gen-llvm",target.path~target.file); | 157 Stdout.format("\r{,80}\r", " "); |
99 | 158 Stdout.format(" {,-45}", p); |
100 auto file = new UnicodeFile!(char)(target.path~target.file, Encoding.UTF_8); | 159 Stdout(f(s)).newline; |
101 | |
102 TestValue mode; | |
103 | |
104 char[] data = file.read; | |
105 char[][] commands = split(splitLines(data)[0], " "); | |
106 if(commands[0] == "//fail") | |
107 { | |
108 mode = TestValue.Fail; | |
109 if(commands.length > 1) | |
110 { | |
111 try | |
112 { | |
113 int i = Integer.toInt(commands[1]); | |
114 if(i in testValues) | |
115 mode = testValues[i]; | |
116 } | |
117 catch{} | |
118 } | |
119 } | |
120 /* if(data.length > 6 && data[0..6] == "//fail") | |
121 { | |
122 char[] str = data.splitLines()[0][6..$]; | |
123 | |
124 switch(toLower(trim(str))) | |
125 { | |
126 case "fail": | |
127 case "failure": | |
128 mode = 1; | |
129 break; | |
130 default: | |
131 mode = 0; | |
132 } | |
133 } | |
134 */ | |
135 Stdout.format(" {,-25}", target.file); | |
136 | |
137 process.execute; | |
138 auto result = process.wait; | |
139 | |
140 /* | |
141 if(result.status == 0) | |
142 { | |
143 auto llvm_process = new Process("llvm-as"); | |
144 llvm_process.execute; | |
145 llvm_process.stdin.copy(process.stdout); | |
146 llvm_process.stdin.close(); | |
147 result = llvm_process.wait; | |
148 } | |
149 */ | |
150 | |
151 return resultOf(result.status, mode); | |
152 } | 160 } |
153 | 161 return unexpected? TestResult.Unexpected : TestResult.Expected; |
154 private int resultOf(int status, TestValue mode) | |
155 { | |
156 char[] good(char[] s) | |
157 { | |
158 version (Posix) | |
159 return "\033[1;32m" ~ s ~ "\033[m"; | |
160 else | |
161 return s; | |
162 } | |
163 | |
164 char[] bad(char[] s) | |
165 { | |
166 version (Posix) | |
167 return "\033[1;31m" ~ s ~ "\033[m"; | |
168 else | |
169 return s; | |
170 } | |
171 | |
172 if(status == 0) | |
173 { | |
174 if(mode == TestValue.Success) | |
175 { | |
176 Stdout(good("SUCCESS")).newline; | |
177 return SuccessSuccess; | |
178 } | |
179 if(mode == TestValue.Fail) | |
180 { | |
181 Stdout(bad("SUCCESS - Unexpected")).newline; | |
182 return FailureSuccess; | |
183 } | |
184 } | |
185 else | |
186 { | |
187 if(mode == TestValue.Fail) | |
188 { | |
189 Stdout(good("FAILURE")).newline; | |
190 return FailureFailure; | |
191 } | |
192 if(mode == TestValue.Success) | |
193 { | |
194 Stdout(bad("FAILURE - Unexpected")).newline; | |
195 return SuccessFailure; | |
196 } | |
197 } | |
198 } | |
199 } | 162 } |
200 | 163 |