new tester can handle coffeescript test files as well
Showing
6 changed files
with
97 additions
and
85 deletions
... | @@ -265,10 +265,14 @@ | ... | @@ -265,10 +265,14 @@ |
265 | * @param String file Absolute path to some js/coffee file | 265 | * @param String file Absolute path to some js/coffee file |
266 | */ | 266 | */ |
267 | this.exec = function(file) { | 267 | this.exec = function(file) { |
268 | if (!fs.isFile(file) || !this.isJsFile(file)) { | 268 | if (!fs.isFile(file) || !isJsFile(file)) { |
269 | throw "Can only exec() files with .js or .coffee extensions"; | 269 | throw "Can only exec() files with .js or .coffee extensions"; |
270 | } | 270 | } |
271 | eval(fs.read(file)); | 271 | if (fileExt(file) === "coffee") { |
272 | phantom.injectJs(file); | ||
273 | } else { | ||
274 | eval(fs.read(file)); | ||
275 | } | ||
272 | }; | 276 | }; |
273 | 277 | ||
274 | /** | 278 | /** |
... | @@ -301,7 +305,7 @@ | ... | @@ -301,7 +305,7 @@ |
301 | } | 305 | } |
302 | }); | 306 | }); |
303 | return entries.filter(function(entry) { | 307 | return entries.filter(function(entry) { |
304 | return self.isJsFile(fs.absolute(pathJoin(dir, entry))); | 308 | return isJsFile(fs.absolute(pathJoin(dir, entry))); |
305 | }); | 309 | }); |
306 | }; | 310 | }; |
307 | 311 | ||
... | @@ -329,20 +333,6 @@ | ... | @@ -329,20 +333,6 @@ |
329 | }; | 333 | }; |
330 | 334 | ||
331 | /** | 335 | /** |
332 | * Checks if a file is apparently javascript compatible (.js or .coffee). | ||
333 | * | ||
334 | * @param String file Path to the file to test | ||
335 | * @return Boolean | ||
336 | */ | ||
337 | this.isJsFile = function(file) { | ||
338 | var ext; | ||
339 | try { | ||
340 | ext = file.split('.').pop().toLowerCase(); | ||
341 | } catch(e) {} | ||
342 | return isType(ext, "string") && ['js', 'coffee'].indexOf(ext) !== -1; | ||
343 | }; | ||
344 | |||
345 | /** | ||
346 | * Adds a successful test entry to the stack. | 336 | * Adds a successful test entry to the stack. |
347 | * | 337 | * |
348 | * @param String message | 338 | * @param String message | ... | ... |
... | @@ -151,6 +151,20 @@ function dump(value) { | ... | @@ -151,6 +151,20 @@ function dump(value) { |
151 | } | 151 | } |
152 | 152 | ||
153 | /** | 153 | /** |
154 | * Returns the file extension in lower case. | ||
155 | * | ||
156 | * @param String file File path | ||
157 | * @return string | ||
158 | */ | ||
159 | function fileExt(file) { | ||
160 | try { | ||
161 | return file.split('.').pop().toLowerCase(); | ||
162 | } catch(e) { | ||
163 | return ''; | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /** | ||
154 | * Takes a string and append blank until the pad value is reached. | 168 | * Takes a string and append blank until the pad value is reached. |
155 | * | 169 | * |
156 | * @param String text | 170 | * @param String text |
... | @@ -166,6 +180,17 @@ function fillBlanks(text, pad) { | ... | @@ -166,6 +180,17 @@ function fillBlanks(text, pad) { |
166 | } | 180 | } |
167 | 181 | ||
168 | /** | 182 | /** |
183 | * Checks if a file is apparently javascript compatible (.js or .coffee). | ||
184 | * | ||
185 | * @param String file Path to the file to test | ||
186 | * @return Boolean | ||
187 | */ | ||
188 | function isJsFile(file) { | ||
189 | var ext = fileExt(file); | ||
190 | return isType(ext, "string") && ['js', 'coffee'].indexOf(ext) !== -1; | ||
191 | } | ||
192 | |||
193 | /** | ||
169 | * Shorthands for checking if a value is of the given type. Can check for | 194 | * Shorthands for checking if a value is of the given type. Can check for |
170 | * arrays. | 195 | * arrays. |
171 | * | 196 | * | ... | ... |
1 | # A small subset of the run.js written in coffeescript | 1 | # A small subset of the run.js written in coffeescript |
2 | 2 | ||
3 | phantom.injectJs "casper.js" | 3 | do (casper) -> |
4 | steps = 0 | ||
4 | 5 | ||
5 | casper = new phantom.Casper | 6 | casper.options.onStepComplete = -> steps++ |
6 | faultTolerant: false | ||
7 | verbose: true | ||
8 | onStepComplete: -> @test.comment "step completed" | ||
9 | 7 | ||
10 | casper.start "tests/site/index.html", -> | 8 | casper.start "tests/site/index.html", -> |
11 | @test.assertTitle "CasperJS test index", "Casper.start() casper can start itself an open an url" | 9 | @test.assertTitle "CasperJS test index", "Casper.start() casper can start itself an open an url" |
12 | @test.comment "fetching" | 10 | @test.assertEquals @fetchText("ul li"), "onetwothree", "Casper.fetchText() can retrieves text contents" |
13 | @test.assertEquals @fetchText("ul li"), "onetwothree", "Casper.fetchText() can retrieves text contents" | 11 | @click "a[href=\"test.html\"]" |
14 | @test.comment "clicking" | ||
15 | @click "a[href=\"test.html\"]" | ||
16 | 12 | ||
17 | casper.test.comment "then" | 13 | casper.then -> |
18 | casper.then -> | 14 | @test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link" |
19 | @test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link" | 15 | @click "a[href=\"form.html\"]" |
20 | @click "a[href=\"form.html\"]" | ||
21 | 16 | ||
22 | casper.run() | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
17 | casper.run -> | ||
18 | @test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete" | ||
19 | @options.onStepComplete = null | ||
20 | @test.done() | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | // phantom.Casper.FunctionArgsInjector | 1 | (function(t) { |
2 | var t = casper.test; | 2 | function createInjector(fn, values) { |
3 | return new phantom.Casper.FunctionArgsInjector(fn, values); | ||
4 | } | ||
3 | 5 | ||
4 | function createInjector(fn, values) { | 6 | var testFn = function(a, b) { return a + b; }; |
5 | return new phantom.Casper.FunctionArgsInjector(fn, values); | 7 | var injector = createInjector(testFn); |
6 | } | 8 | var extract = injector.extract(testFn); |
7 | 9 | ||
8 | var testFn = function(a, b) { return a + b; }; | 10 | t.comment('FunctionArgsInjector.extract()'); |
9 | var injector = createInjector(testFn); | 11 | t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object'); |
10 | var extract = injector.extract(testFn); | 12 | t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected'); |
13 | t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected'); | ||
14 | t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected'); | ||
11 | 15 | ||
12 | t.comment('FunctionArgsInjector.extract()'); | 16 | var processed; |
13 | t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object'); | 17 | eval('processed = ' + injector.process({ a: 1, b: 2 })); |
14 | t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected'); | ||
15 | t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected'); | ||
16 | t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected'); | ||
17 | 18 | ||
18 | var processed; | 19 | t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() proccessed the function correctly'); |
19 | eval('processed = ' + injector.process({ a: 1, b: 2 })); | ||
20 | 20 | ||
21 | t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() proccessed the function correctly'); | ||
22 | |||
23 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
21 | t.done(); | ||
22 | })(casper.test); | ... | ... |
1 | var t = casper.test; | 1 | (function(t) { |
2 | t.comment('Tester.testEquals()'); | ||
3 | t.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality'); | ||
4 | t.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality'); | ||
5 | t.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality'); | ||
6 | t.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality'); | ||
7 | t.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality'); | ||
8 | t.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality'); | ||
9 | t.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality'); | ||
10 | t.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality'); | ||
11 | t.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality'); | ||
12 | t.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality'); | ||
13 | t.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality'); | ||
14 | t.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality'); | ||
15 | t.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality'); | ||
16 | t.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality'); | ||
17 | t.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality'); | ||
18 | t.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality'); | ||
19 | t.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality'); | ||
20 | t.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality'); | ||
21 | t.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality'); | ||
22 | t.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality'); | ||
23 | t.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality'); | ||
2 | 24 | ||
3 | t.comment('Tester.testEquals()'); | ||
4 | t.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality'); | ||
5 | t.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality'); | ||
6 | t.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality'); | ||
7 | t.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality'); | ||
8 | t.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality'); | ||
9 | t.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() cast number equality'); | ||
10 | t.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality'); | ||
11 | t.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality'); | ||
12 | t.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality'); | ||
13 | t.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality'); | ||
14 | t.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality'); | ||
15 | t.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality'); | ||
16 | t.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality'); | ||
17 | t.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality'); | ||
18 | t.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality'); | ||
19 | t.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality'); | ||
20 | t.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality'); | ||
21 | t.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality'); | ||
22 | t.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality'); | ||
23 | t.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality'); | ||
24 | t.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality'); | ||
25 | |||
26 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
25 | t.done(); | ||
26 | })(casper.test); | ... | ... |
1 | var t = casper.test; | 1 | (function(t) { |
2 | t.comment('phantom.Casper.XUnitExporter'); | ||
2 | 3 | ||
3 | t.comment('phantom.Casper.XUnitExporter'); | 4 | xunit = new phantom.Casper.XUnitExporter(); |
5 | xunit.addSuccess('foo', 'bar'); | ||
6 | t.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | ||
7 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); | ||
8 | t.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | ||
4 | 9 | ||
5 | xunit = new phantom.Casper.XUnitExporter(); | ||
6 | xunit.addSuccess('foo', 'bar'); | ||
7 | t.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase'); | ||
8 | xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); | ||
9 | t.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); | ||
10 | |||
11 | t.done(); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
10 | t.done(); | ||
11 | })(casper.test); | ... | ... |
-
Please register or sign in to post a comment