1 | <!doctype html>
|
2 | <html>
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>Clone Test on Browser</title>
|
6 | <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
|
7 | <script>
|
8 | window.TESTING = true;
|
9 | var tests = exports = {}, module = {};
|
10 | function require(moduleName) {
|
11 | if (moduleName == './') return clone;
|
12 | if (moduleName == 'underscore') return _;
|
13 | }
|
14 | function log(str) {
|
15 | logList.innerHTML += '<li>'+str+'</li>'
|
16 | }
|
17 | </script>
|
18 | <script src="clone.js"></script>
|
19 | <script src="test.js"></script>
|
20 | </head>
|
21 | <body>
|
22 | <h1 id="nodeunit-header">Clone Test on Browser</h1>
|
23 | Tests started: <span id="testsStarted"></span>;
|
24 | Tests finished: <span id="testsFinished"></span>.
|
25 | <ul id="logList"></ul>
|
26 | <script>
|
27 | /* Methods copyed from
|
28 | ** https:
|
29 | */
|
30 | function isUndefinedOrNull (value) {
|
31 | return value === null || value === undefined;
|
32 | }
|
33 | function isArguments (object) {
|
34 | return Object.prototype.toString.call(object) == '[object Arguments]';
|
35 | }
|
36 | var _keys = function(obj){
|
37 | if(Object.keys) return Object.keys(obj);
|
38 | if (typeof obj != 'object' && typeof obj != 'function') {
|
39 | throw new TypeError('-');
|
40 | }
|
41 | var keys = [];
|
42 | for(var k in obj) if(obj.hasOwnProperty(k)) keys.push(k);
|
43 | return keys;
|
44 | };
|
45 | function objEquiv (a, b) {
|
46 | if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) return false;
|
47 | if (a.prototype !== b.prototype) return false;
|
48 | if (isArguments(a)) {
|
49 | if (!isArguments(b)) return false;
|
50 | a = pSlice.call(a);
|
51 | b = pSlice.call(b);
|
52 | return _deepEqual(a, b);
|
53 | }
|
54 | try{
|
55 | var ka = _keys(a), kb = _keys(b), key, i;
|
56 | } catch (e) {
|
57 | return false
|
58 | }
|
59 | if (ka.length != kb.length) return false;
|
60 | ka.sort();
|
61 | kb.sort();
|
62 | for (i = ka.length - 1; i >= 0; i--) {
|
63 | if (ka[i] != kb[i]) return false;
|
64 | }
|
65 | for (i = ka.length - 1; i >= 0; i--) {
|
66 | key = ka[i];
|
67 | if (!_deepEqual(a[key], b[key] )) return false;
|
68 | }
|
69 | return true;
|
70 | }
|
71 | function _deepEqual(actual, expected) {
|
72 | if (actual === expected) {
|
73 | return true;
|
74 | } else if (actual instanceof Date && expected instanceof Date) {
|
75 | return actual.getTime() === expected.getTime();
|
76 | } else if (actual instanceof RegExp && expected instanceof RegExp) {
|
77 | return actual.source === expected.source &&
|
78 | actual.global === expected.global &&
|
79 | actual.ignoreCase === expected.ignoreCase &&
|
80 | actual.multiline === expected.multiline;
|
81 | } else if (typeof actual != 'object' && typeof expected != 'object') {
|
82 | return actual == expected;
|
83 | } else {
|
84 | return objEquiv(actual, expected);
|
85 | }
|
86 | }
|
87 |
|
88 | for ( var testName in tests ) {
|
89 | setTimeout((function(testName){
|
90 | try {
|
91 | testsStarted.innerHTML = (parseInt(testsStarted.innerHTML) || 0) + 1;
|
92 | function incFinished() {
|
93 | testsFinished.innerHTML = (parseInt(testsFinished.innerHTML) || 0) + 1;
|
94 | }
|
95 | tests[testName]({
|
96 | expect: function(num) { this._expect = num },
|
97 | ok: function(val) { if(!val) throw new Error(val + ' is not ok.') },
|
98 | equal: function(a,b) {
|
99 | if (a!=b) throw new Error(a + ' is not equal to ' + b)
|
100 | },
|
101 | notEqual: function(a,b) {
|
102 | if (a==b) throw new Error(a + ' is equal to ' + b)
|
103 | },
|
104 | strictEqual: function(a,b) {
|
105 | if (a!==b) throw new Error(a + ' is not strict equal to ' + b)
|
106 | },
|
107 | deepEqual: function(a,b) {
|
108 | if (!_deepEqual(a,b))
|
109 | throw new Error(JSON.stringify(a) + ' is not deep equal to ' +
|
110 | JSON.stringify(b))
|
111 | },
|
112 | done: function() {
|
113 | log(testName + ' <span style="color:blue">is ok</span>.');
|
114 | incFinished();
|
115 | }
|
116 | })
|
117 | } catch(e) {
|
118 | log(testName + ' <span style="color:red">FAIL.</span> <small>'+ e +'</small>');
|
119 | incFinished();
|
120 | console.log(e);
|
121 | }
|
122 | })(testName), 1);
|
123 | }
|
124 | </script>
|
125 | </body>
|
126 | </html>
|