Commit de27476e de27476ebe6e591d7293a35ad0dc65c1f935a852 by Nicolas Perriault

refs #397 - setUp(), test() and tearDown() uses same context

1 parent 87de6509
...@@ -5,10 +5,10 @@ default: all ...@@ -5,10 +5,10 @@ default: all
5 all: selftest clitest jshint 5 all: selftest clitest jshint
6 6
7 selftest: 7 selftest:
8 bin/casperjs selftest 8 -bin/casperjs selftest
9 9
10 clitest: 10 clitest:
11 python tests/clitests/runtests.py 11 -python tests/clitests/runtests.py
12 12
13 jshint: 13 jshint:
14 jshint --config=.jshintconfig . 14 -jshint --config=.jshintconfig .
......
...@@ -162,11 +162,7 @@ var Tester = function Tester(casper, options) { ...@@ -162,11 +162,7 @@ var Tester = function Tester(casper, options) {
162 162
163 function errorHandler(error, backtrace) { 163 function errorHandler(error, backtrace) {
164 if (error instanceof Error) { 164 if (error instanceof Error) {
165 if (error instanceof AssertionError) { 165 self.processError(error);
166 self.processAssertionError(error);
167 } else if (error.message !== self.SKIP_MESSAGE) {
168 self.uncaughtError(error, self.currentTestFile, error.line);
169 }
170 return self.done(); 166 return self.done();
171 } 167 }
172 if (error.indexOf('AssertionError') === 0) { 168 if (error.indexOf('AssertionError') === 0) {
...@@ -841,60 +837,61 @@ Tester.prototype.bar = function bar(text, style) { ...@@ -841,60 +837,61 @@ Tester.prototype.bar = function bar(text, style) {
841 */ 837 */
842 Tester.prototype.begin = function begin() { 838 Tester.prototype.begin = function begin() {
843 "use strict"; 839 "use strict";
844 /*jshint maxstatements:40 maxcomplexity:20*/
845 if (this.started && this.running) { 840 if (this.started && this.running) {
846 return this.queue.push(arguments); 841 return this.queue.push(arguments);
847 } 842 }
848 var description = arguments[0] || f("Untitled suite in %s", this.currentTestFile), 843 function getConfig(args) {
849 planned, 844 var config = {
850 suiteFn, 845 setUp: function(){},
851 setUp, 846 tearDown: function(){}
852 tearDown; 847 };
853 if (utils.isFunction(arguments[1])) { 848 if (utils.isFunction(args[1])) {
854 suiteFn = arguments[1]; 849 config.test = args[1];
855 } else if (utils.isObject(arguments[1])) { 850 } else if (utils.isObject(args[1])) {
856 suiteFn = arguments[1].test; 851 config = utils.mergeObjects(config, args[1]);
857 setUp = arguments[1].setUp; 852 } else if (utils.isNumber(args[1]) && utils.isFunction(args[2])) {
858 tearDown = arguments[1].tearDown; 853 config.planned = ~~args[1] || undefined;
859 } else if (utils.isNumber(arguments[1]) && utils.isFunction(arguments[2])) { 854 config.test = args[2];
860 planned = arguments[1]; 855 } else if (utils.isNumber(args[1]) && utils.isObject(args[2])) {
861 suiteFn = arguments[2]; 856 config.config = utils.mergeObjects(config, args[2]);
862 } else if (utils.isNumber(arguments[1]) && utils.isObject(arguments[2])) { 857 config.planned = ~~args[1] || undefined;
863 planned = arguments[1]; 858 } else {
864 suiteFn = arguments[2].test; 859 throw new CasperError('Invalid call');
865 setUp = arguments[2].setUp; 860 }
866 tearDown = arguments[2].tearDown; 861 if (!utils.isFunction(config.test)) {
867 } else { 862 throw new CasperError('begin() is missing a mandatory test function');
868 throw new CasperError('Invalid call'); 863 }
864 return config;
869 } 865 }
866 var description = arguments[0] || f("Untitled suite in %s", this.currentTestFile),
867 config = getConfig([].slice.call(arguments));
870 if (!this.options.concise) { 868 if (!this.options.concise) {
871 this.comment(description); 869 this.comment(description);
872 } 870 }
873 this.currentSuite = new TestCaseResult({ 871 this.currentSuite = new TestCaseResult({
874 name: description, 872 name: description,
875 file: this.currentTestFile, 873 file: this.currentTestFile,
876 planned: Math.abs(~~planned) || undefined 874 config: config,
875 planned: config.planned || undefined
877 }); 876 });
878 this.executed = 0; 877 this.executed = 0;
879 this.running = this.started = true; 878 this.running = this.started = true;
880 try { 879 try {
881 if (utils.isFunction(setUp)) { 880 if (config.setUp) {
882 setUp.call(this, this, this.casper); 881 config.setUp(this, this.casper);
883 } 882 }
884 suiteFn.call(this, this, this.casper); 883 config.test(this, this.casper);
885 } catch (err) { 884 } catch (err) {
886 if (err instanceof AssertionError) { 885 this.processError(err);
887 this.processAssertionError(err); 886 this.done();
888 } else {
889 this.uncaughtError(err, this.currentTestFile, err.line);
890 }
891 this.done(tearDown);
892 } 887 }
893 if (this.options.concise) { 888 if (this.options.concise) {
894 this.casper.echo([ 889 this.casper.echo([
895 this.colorize('PASS', 'INFO'), 890 this.colorize('PASS', 'INFO'),
896 this.formatMessage(description), 891 this.formatMessage(description),
897 this.colorize(f('(%d test%s)', planned, planned > 1 ? 's' : ''), 'INFO') 892 this.colorize(f('(%d test%s)',
893 config.planned,
894 config.planned > 1 ? 's' : ''), 'INFO')
898 ].join(' ')); 895 ].join(' '));
899 } 896 }
900 }; 897 };
...@@ -921,25 +918,27 @@ Tester.prototype.comment = function comment(message) { ...@@ -921,25 +918,27 @@ Tester.prototype.comment = function comment(message) {
921 /** 918 /**
922 * Declares the current test suite done. 919 * Declares the current test suite done.
923 * 920 *
924 * @param Function tearDown Function to call when done
925 */ 921 */
926 Tester.prototype.done = function done() { 922 Tester.prototype.done = function done() {
927 "use strict"; 923 "use strict";
928 /*jshint maxstatements:20 maxcomplexity:20*/ 924 /*jshint maxstatements:20 maxcomplexity:20*/
929 var planned, tearDown; 925 var planned, config = this.currentSuite.config;
930 if (utils.isNumber(arguments[0])) { 926 if (utils.isNumber(arguments[0])) {
931 this.casper.warn('done() `planned` arg is deprecated as of 1.1'); 927 this.casper.warn('done() `planned` arg is deprecated as of 1.1');
932 planned = arguments[0]; 928 planned = arguments[0];
933 } else if (utils.isFunction(arguments[0])) { 929 }
934 tearDown = arguments[0]; 930 if (config && config.tearDown && utils.isFunction(config.tearDown)) {
931 try {
932 config.tearDown(this, this.casper);
933 } catch (error) {
934 this.processError(error);
935 }
935 } 936 }
936 if (this.currentSuite && this.currentSuite.planned && this.currentSuite.planned !== this.executed) { 937 if (this.currentSuite && this.currentSuite.planned && this.currentSuite.planned !== this.executed) {
937 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name); 938 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name);
938 } else if (planned && planned !== this.executed) { 939 } else if (planned && planned !== this.executed) {
939 // BC 940 // BC
940 this.dubious(planned, this.executed); 941 this.dubious(planned, this.executed);
941 } else if (tearDown) {
942 tearDown.call(this, this, this.casper);
943 } 942 }
944 if (this.currentSuite) { 943 if (this.currentSuite) {
945 this.suiteResults.push(this.currentSuite); 944 this.suiteResults.push(this.currentSuite);
...@@ -1160,6 +1159,19 @@ Tester.prototype.processAssertionResult = function processAssertionResult(result ...@@ -1160,6 +1159,19 @@ Tester.prototype.processAssertionResult = function processAssertionResult(result
1160 }; 1159 };
1161 1160
1162 /** 1161 /**
1162 * Processes an error.
1163 *
1164 * @param {Error} error
1165 */
1166 Tester.prototype.processError = function processError(error) {
1167 "use strict";
1168 if (error instanceof AssertionError) {
1169 return this.processAssertionError(error);
1170 }
1171 return this.uncaughtError(error, this.currentTestFile, error.line);
1172 };
1173
1174 /**
1163 * Renders a detailed report for each failed test. 1175 * Renders a detailed report for each failed test.
1164 * 1176 *
1165 */ 1177 */
...@@ -1497,6 +1509,7 @@ function TestCaseResult(options) { ...@@ -1497,6 +1509,7 @@ function TestCaseResult(options) {
1497 this.failures = []; 1509 this.failures = [];
1498 this.passes = []; 1510 this.passes = [];
1499 this.warnings = []; 1511 this.warnings = [];
1512 this.config = options && options.config;
1500 this.__defineGetter__("assertions", function() { 1513 this.__defineGetter__("assertions", function() {
1501 return this.passed + this.failed; 1514 return this.passed + this.failed;
1502 }); 1515 });
......
1 /*jshint strict:false eqeqeq:false*/
2 /*global CasperError casper console phantom require*/
3 var steps = [];
4
5 casper.test.begin('Tester.begin() configuration', 10, {
6 fixtures: [1, 2, 3],
7
8 _this: function() {
9 return this;
10 },
11
12 setUp: function(test) {
13 steps.push('setUp');
14 test.pass('config.setUp() has been called');
15 test.assert(this == this._this(), 'config.setUp() is using the expected context');
16 test.assertEquals(this.fixtures, [1, 2, 3], 'config.setUp() accesses fixtures');
17 },
18
19 tearDown: function(test) {
20 steps.push('tearDown');
21 test.pass('config.tearDown() has been called');
22 test.assert(this == this._this(), 'config.test() is using the expected context');
23 test.assertEquals(this.fixtures, [1, 2, 3], 'config.tearDown() accesses fixtures');
24 test.assertEquals(steps, ['setUp', 'test', 'tearDown'],
25 'Tester.begin() has processed the configuration in the expected order');
26 },
27
28 test: function(test) {
29 steps.push('test');
30 test.pass('config.test() has been called');
31 test.assert(this == this._this(), 'config.tearDown() is using the expected context');
32 test.assertEquals(this.fixtures, [1, 2, 3], 'config.test() accesses fixtures');
33 test.done();
34 }
35 });