This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (104 loc) · 2.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if (typeof define !== 'function') { var define = (require('amdefine'))(module); }
define([
"assert"
], function(assert){
"use strict";
var exports = function(name, obj){
var testCase = {};
testCase[name] = visit(obj);
return testCase;
};
exports.Skip = {};
exports._count = 0;
exports._failures = [];
exports.assert = function(){
return exports.assert.ok.apply(exports.assert, arguments);
};
for(var method in assert){
exports.assert[method] = (function(fn){
return function(){
exports._count++;
try{
return fn.apply(this, arguments);
}catch(error){
exports._failures.push(error);
throw error;
}
};
})(assert[method]);
}
assert.defined = function(value, message){
exports._count++;
if(typeof value === "undefined"){
assert.fail(value, "undefined", message, "==", assert.defined);
}
};
exports.assert.same = exports.assert.strictEqual;
exports.assert.exception = exports.assert["throws"];
var refute = exports.refute = function(){
exports._count++;
return exports.refute.ok.apply(exports.refute, arguments);
};
refute.ok = function(value, message){
return exports.assert.ok(!value, message);
};
refute.defined = function(value, message){
exports._count++;
if(typeof value !== "undefined"){
assert.fail(value, "undefined", message, "!=", refute.defined);
}
};
refute.equal = exports.assert.notEqual;
refute.deepEqual = exports.assert.notDeepEqual;
refute.strictEqual = refute.same = exports.assert.notStrictEqual;
refute.exception = refute["throws"] = exports.assert.doesNotThrow;
function visit(obj){
for(var key in obj){
if(typeof obj[key] === "function"){
switch(key){
case "before":
case "beforeEach":
case "after":
case "afterEach":
break;
default:
obj[key] = wrap(obj[key]);
}
}else if(obj[key] === exports.Skip){
delete obj[key];
}else if(typeof obj[key] === "object"){
visit(obj[key]);
}
}
return obj;
}
function wrap(fn){
return function(done){
exports._count = 0;
exports._failures = [];
if(fn.length){
fn.call(this, testAssertions);
}else{
var promise = fn.call(this);
if(promise && typeof promise.then === "function"){
promise.then(function(){ testAssertions(); }, testAssertions);
}else{
testAssertions();
}
}
function testAssertions(error){
if(exports._failures.length){
error = exports._failures[0];
}
if(error instanceof Error || error != null){
done(error);
}else if(exports._count === 0){
done(new Error("No assertions!"));
}else{
done();
}
}
};
}
return exports;
});