Mercurial > projects > dang
annotate 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 |
rev | line source |
---|---|
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
1 // skip |
1 | 2 module run.d; |
3 | |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
4 import tango.core.Array, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
5 tango.io.FileConduit, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
6 tango.io.FileScan, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
7 tango.io.Stdout, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
8 tango.sys.Process, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
9 tango.text.Ascii, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
10 tango.text.Regex, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
11 tango.text.Util; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
12 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
13 // -- Settings -- |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
14 char[] compiler = "./Dang"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
15 char[] test_folder = "tests"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
16 char[] valid_filenames = r"^[^.].*"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
17 bool print_expected = false; |
1 | 18 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
19 // the tests can be sorted by one of the following functions |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
20 bool nameSort (FilePath a, FilePath b) { return a.name < b.name; } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
21 bool pathSort (FilePath a, FilePath b) { return a.toString < b.toString; } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
22 bool modifiedSort(FilePath a, FilePath b) { return a.modified < b.modified; } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
23 bool createdSort (FilePath a, FilePath b) { return a.created < b.created; } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
24 const sortBy = &pathSort; |
1 | 25 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
26 // -- end of settings |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
27 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
28 enum TestResult |
89
a49bb982a7b0
Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
29 { |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
30 Skipped, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
31 Expected, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
32 Unexpected |
89
a49bb982a7b0
Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
33 } |
a49bb982a7b0
Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents:
39
diff
changeset
|
34 |
1 | 35 void main(char[][] args) |
36 { | |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
37 scope scan = new FileScan; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
38 scope regex = new Regex(valid_filenames); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
39 // Return true for files/folders to include |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
40 bool filter(FilePath p, bool isDir) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
41 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
42 if (isDir) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
43 return p.name[0] != '.'; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
44 else |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
45 return p.ext == "d" && regex.test(p.name); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
46 } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
47 scan.sweep(test_folder, &filter, true); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
48 FilePath[] files = scan.files; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
49 int total_tests = files.length; |
1 | 50 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
51 // Sort the result by the chosen function - default is the full path |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
52 sort(files, sortBy); |
1 | 53 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
54 int[TestResult.max + 1] results = 0; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
55 foreach (i, ref test; files) |
1 | 56 { |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
57 begin_test(i + 1, total_tests, test.name); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
58 TestResult res = run_test(test); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
59 results[res] += 1; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
60 end_test(); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
61 } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
62 Stdout.format("\r{,80}\r", " "); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
63 Stdout.newline; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
64 int good = TestResult.Expected; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
65 int bad = TestResult.Unexpected; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
66 int tests_run = results[good] + results[bad]; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
67 Stdout.formatln("{}/{} tests failed", results[bad], tests_run); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
68 } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
69 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
70 void begin_test(int number, int total_tests, char[] name) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
71 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
72 char[60] progressbar = ' '; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
73 int progress = number*progressbar.length/total_tests; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
74 progressbar[0 .. progress] = '='; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
75 progressbar[progress-1] = '>'; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
76 Stdout.format("\r{}% - [{}]", 1e2 * number / total_tests, progressbar); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
77 Stdout.flush(); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
78 //Thread.sleep(0.05); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
79 } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
80 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
81 void end_test() { } |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
82 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
83 enum { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
84 NoFail, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
85 CompiletimeFail, |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
86 RuntimeFail |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
87 } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
88 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
89 private int min(int a, int b) { return a < b? a : b; } |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
90 TestResult run_test(ref FilePath p) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
91 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
92 auto file = new FileConduit(p.toString(), FileConduit.ReadExisting); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
93 char[256] content; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
94 int len = file.read(content); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
95 file.close(); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
96 char[] line = content[0 .. min(len, content.find('\n'))]; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
97 |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
98 bool compile = true; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
99 int fail = NoFail; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
100 if (line.length >= 2 && line[0 .. 2] == "//") |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
101 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
102 foreach (command; line[2 .. $].delimiters(",;")) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
103 switch (toLower(substitute(command, " ", ""))) |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
104 { |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
105 case "skip", "dontcompile": |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
106 compile = false; |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
107 break; |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
108 case "fail", "compilefail", |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
109 "compiletimefail", "failatcompiletime": |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
110 fail = CompiletimeFail; |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
111 break; |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
112 case "runtime", "runtimefail", "failatruntime": |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
113 fail = RuntimeFail; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
114 Stderr("== Compiled tests will not be run! ==").newline; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
115 return TestResult.Skipped; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
116 default: |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
117 break; |
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
118 } |
1 | 119 } |
120 | |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
121 if (compile) |
1 | 122 { |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
123 auto process = new Process(compiler, "--gen-llvm", p.toString()); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
124 process.execute(); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
125 auto result = process.wait(); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
126 return resultOf(p, result.status, fail); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
127 } |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
128 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
129 return TestResult.Skipped; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
130 } |
1 | 131 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
132 private TestResult resultOf(FilePath p, int result, int expected) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
133 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
134 char[] good(char[] s) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
135 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
136 version (Posix) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
137 return "\033[1;32m" ~ s ~ "\033[m"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
138 else |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
139 return s; |
20
95e3940d91d4
Small changes to the text program.
Anders Halager <halager@gmail.com>
parents:
17
diff
changeset
|
140 } |
95e3940d91d4
Small changes to the text program.
Anders Halager <halager@gmail.com>
parents:
17
diff
changeset
|
141 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
142 char[] bad(char[] s) |
20
95e3940d91d4
Small changes to the text program.
Anders Halager <halager@gmail.com>
parents:
17
diff
changeset
|
143 { |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
144 s = s ~ " - Unexpected"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
145 version (Posix) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
146 return "\033[1;31m" ~ s ~ "\033[m"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
147 else |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
148 return s; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
149 } |
20
95e3940d91d4
Small changes to the text program.
Anders Halager <halager@gmail.com>
parents:
17
diff
changeset
|
150 |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
151 bool unexpected = (result != expected); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
152 auto f = unexpected? &bad : &good; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
153 char[] s = (result == 0)? "SUCCESS" : "FAILURE"; |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
154 // always print if unexpeted, otherwise check the settings |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
155 if (unexpected || print_expected) |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
156 { |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
157 Stdout.format("\r{,80}\r", " "); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
158 Stdout.format(" {,-45}", p); |
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
159 Stdout(f(s)).newline; |
20
95e3940d91d4
Small changes to the text program.
Anders Halager <halager@gmail.com>
parents:
17
diff
changeset
|
160 } |
134
570a4917413a
Changed the test-program.
Anders Halager <halager@gmail.com>
parents:
89
diff
changeset
|
161 return unexpected? TestResult.Unexpected : TestResult.Expected; |
1 | 162 } |
15
59bfbaf8847f
Updates to run.d - still errors
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
163 |