Commit 2344ade5 2344ade5ddc941b9edbfaba9128092964f9a7a42 by Mickaël Andrieu

Introduced Tester.assertInstanceOf method, refs #602

- add utils.betterInstanceOf
- add 12 tests
- add documentation
1 parent eb4be25d
...@@ -450,9 +450,9 @@ Asserts that the provided input is of the given type:: ...@@ -450,9 +450,9 @@ Asserts that the provided input is of the given type::
450 ``assertInstanceOf()`` 450 ``assertInstanceOf()``
451 ------------------------------------------------------------------------------- 451 -------------------------------------------------------------------------------
452 452
453 **Signature:** ``assertInstanceOf(mixed input, String className[, String message])`` 453 **Signature:** ``assertInstanceOf(mixed input, function constructor[, String message])``
454 454
455 Asserts that the provided input is of the given className:: 455 Asserts that the provided input is of the given constructor::
456 456
457 function Cow() { 457 function Cow() {
458 this.moo = function moo() { 458 this.moo = function moo() {
...@@ -461,8 +461,8 @@ Asserts that the provided input is of the given className:: ...@@ -461,8 +461,8 @@ Asserts that the provided input is of the given className::
461 } 461 }
462 casper.test.begin('assertInstanceOf() tests', 2, function suite(test) { 462 casper.test.begin('assertInstanceOf() tests', 2, function suite(test) {
463 var daisy = new Cow(); 463 var daisy = new Cow();
464 test.assertInstanceOf(daisy, "Cow", "Ok, daisy is a cow."); 464 test.assertInstanceOf(daisy, Cow, "Ok, daisy is a cow.");
465 test.assertInstanceOf(["moo", "boo"], "Array", "We can test for arrays too!"); 465 test.assertInstanceOf(["moo", "boo"], Array, "We can test for arrays too!");
466 test.done(); 466 test.done();
467 }); 467 });
468 468
......
...@@ -840,24 +840,24 @@ Tester.prototype.assertType = function assertType(subject, type, message) { ...@@ -840,24 +840,24 @@ Tester.prototype.assertType = function assertType(subject, type, message) {
840 }; 840 };
841 841
842 /** 842 /**
843 * Asserts that the provided subject is of the given class name. 843 * Asserts that the provided subject is of the given class.
844 * 844 *
845 * @param mixed subject The value to test 845 * @param mixed subject The value to test
846 * @param string className The className 846 * @param function constructor The javascript type name
847 * @param String message Test description 847 * @param String message Test description
848 * @return Object An assertion result object 848 * @return Object An assertion result object
849 */ 849 */
850 Tester.prototype.assertInstanceOf = function assertInstanceOf(subject, className, message) { 850 Tester.prototype.assertInstanceOf = function assertInstanceOf(subject, constructor, message) {
851 "use strict"; 851 "use strict";
852 if (utils.betterTypeOf(subject.constructor) !== "function") { 852 if (utils.betterTypeOf(constructor) !== "function") {
853 throw new CasperError('Subject is null or undefined.'); 853 throw new CasperError('Subject is null or undefined.');
854 } 854 }
855 return this.assert(utils.equals(subject.constructor.name, className), message, { 855 return this.assert(utils.betterInstanceOf(subject, constructor), message, {
856 type: "assertInstanceOf", 856 type: "assertInstanceOf",
857 standard: f('Subject is an instance of: "%s"', subject.constructor.name), 857 standard: f('Subject type is: "%s"', constructor),
858 values: { 858 values: {
859 subject: subject, 859 subject: subject,
860 className: className, 860 className: constructor.name,
861 } 861 }
862 }); 862 });
863 }; 863 };
......
...@@ -71,6 +71,33 @@ function betterTypeOf(input) { ...@@ -71,6 +71,33 @@ function betterTypeOf(input) {
71 exports.betterTypeOf = betterTypeOf; 71 exports.betterTypeOf = betterTypeOf;
72 72
73 /** 73 /**
74 * Provides a better instanceof operator equivalent, able to retrieve the uninstantiated string
75 * or inheritance cases.
76 *
77 * @param mixed input
78 * @param function constructor
79 * @return String
80 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model
81 */
82 function betterInstanceOf(input, constructor) {
83 "use strict";
84 while (input !== null) {
85 if (input === constructor.prototype) {
86 return true;
87 }
88 if (typeof input === 'xml') {
89 return constructor.prototype === document.prototype;
90 }
91 if (typeof input === 'undefined') {
92 return false;
93 }
94 input = input.__proto__;
95 }
96 return false;
97 }
98 exports.betterInstanceOf = betterInstanceOf;
99
100 /**
74 * Cleans a passed URL. 101 * Cleans a passed URL.
75 * 102 *
76 * @param String url An HTTP URL 103 * @param String url An HTTP URL
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 /*jshint strict:false, maxstatements:99*/ 2 /*jshint strict:false, maxstatements:99*/
3 var fs = require('fs'); 3 var fs = require('fs');
4 4
5 casper.test.begin('Common assertions tests', 50, function(test) { 5 casper.test.begin('Common assertions tests', 46, function(test) {
6 casper.start('tests/site/index.html', function() { 6 casper.start('tests/site/index.html', function() {
7 test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text'); 7 test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
8 test.assertTextExist('form', 'Tester.assertTextExist() checks that page body contains text [alias]'); 8 test.assertTextExist('form', 'Tester.assertTextExist() checks that page body contains text [alias]');
...@@ -60,13 +60,7 @@ casper.test.begin('Common assertions tests', 50, function(test) { ...@@ -60,13 +60,7 @@ casper.test.begin('Common assertions tests', 50, function(test) {
60 test.assertTitleMatch(/test index/, 'Tester.assertTitleMatch() works as expected'); 60 test.assertTitleMatch(/test index/, 'Tester.assertTitleMatch() works as expected');
61 test.assertTitleMatches(/test index/, 'Tester.assertTitleMatches() works as expected [alias]'); 61 test.assertTitleMatches(/test index/, 'Tester.assertTitleMatches() works as expected [alias]');
62 test.assertType("plop", "string", "Tester.assertType() works as expected"); 62 test.assertType("plop", "string", "Tester.assertType() works as expected");
63 // we need a class and an instance of this class 63 test.assertInstanceOf("plop", String, "Tester.assertInstanceOf() works as expected");
64 function Cow(){} var daisy = new Cow();
65 test.assertInstanceOf(12, "Number", "Tester.assertInstanceOf() works as expected");
66 test.assertInstanceOf("Boo", "String", "Tester.assertInstanceOf() works as expected");
67 test.assertInstanceOf(["moo", "bar"], "Array", "Tester.assertInstanceOf() works as expected")
68 test.assertInstanceOf(true, "Boolean", "Test.assertInstanceOf() works as expected");
69 test.assertInstanceOf(daisy, "Cow", "Tester.assertInstanceOf() works as expected");
70 test.assertUrlMatch(/index\.html$/, "Tester.assertUrlMatch() works as expected"); 64 test.assertUrlMatch(/index\.html$/, "Tester.assertUrlMatch() works as expected");
71 test.assertUrlMatches(/index\.html$/, "Tester.assertUrlMatches() works as expected [alias]"); 65 test.assertUrlMatches(/index\.html$/, "Tester.assertUrlMatches() works as expected [alias]");
72 test.assertVisible('img', 'Tester.assertVisible() works as expected'); 66 test.assertVisible('img', 'Tester.assertVisible() works as expected');
......
...@@ -24,6 +24,32 @@ casper.test.begin('utils.betterTypeOf() tests', 10, function(test) { ...@@ -24,6 +24,32 @@ casper.test.begin('utils.betterTypeOf() tests', 10, function(test) {
24 test.done(); 24 test.done();
25 }); 25 });
26 26
27 casper.test.begin('utils.betterInstanceOf() tests', 12, function(test) {
28 // need two objects to test inheritance
29 function Cow(){} var daisy = new Cow();
30 function SuperCow(){} SuperCow.prototype = new Cow(); var superDaisy = new SuperCow();
31 var date = new Date(); var regex = new RegExp();
32 var testCases = [
33 {subject: 1, fn: Number, expected: true},
34 {subject: '1', fn: String, expected: true},
35 {subject: {}, fn: Object, expected: true},
36 {subject: [], fn: Array, expected: true},
37 {subject: undefined, fn: Array, expected: false},
38 {subject: null, fn: Array, expected: false},
39 {subject: function(){}, fn: Function, expected: true},
40 {subject: date, fn: Date, expected: true},
41 {subject: regex, fn: RegExp, expected: true},
42 {subject: daisy, fn: Cow, expected: true},
43 {subject: superDaisy, fn: SuperCow, expected: true},
44 {subject: superDaisy, fn: Cow, expected: true}
45 ];
46 testCases.forEach(function(testCase) {
47 test.assertEquals(utils.betterInstanceOf(testCase.subject, testCase.fn), testCase.expected,
48 utils.format('betterInstanceOf() detects expected constructor "%s"', testCase.fn.name));
49 });
50 test.done();
51 });
52
27 casper.test.begin('utils.cleanUrl() tests', 11, function(test) { 53 casper.test.begin('utils.cleanUrl() tests', 11, function(test) {
28 var testCases = { 54 var testCases = {
29 'http://google.com/': 'http://google.com/', 55 'http://google.com/': 'http://google.com/',
......