-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcmaUnit.js
168 lines (137 loc) · 3.62 KB
/
EcmaUnit.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Version 1.2.3
var ecmaUnit = ecmaUnit || {};
ecmaUnit.Runner = function (){
this.run = function(testFixture, options){
var isFunction = function(func){
return (typeof func === 'function');
};
var shouldRunTest = function(options, testName){
if (options && options.runSingleTest){
if (options.runSingleTest === testName){
return true;
}else{
return false;
}
}
return true;
}
var runTest = function(test, fixtureResult, testName, options){
var testResult = {
testName: testName
};
fixtureResult.testResults.push(testResult);
if (shouldRunTest(options, testName)){
try{
test();
testResult.result = 'pass';
fixtureResult.passCount++;
}catch(exception){
testResult.result = 'fail';
testResult.exception = exception;
fixtureResult.failCount++;
}
}else{
testResult.result = 'skipped'
fixtureResult.skippedCount++;
}
};
var runFixtureInternal = function(testFixture, options){
var fixtureResult = new ecmaUnit.FixtureResult();
for(property in testFixture){
var test = testFixture[property];
if (isFunction(test)){
runTest(test, fixtureResult, property,options);
}
}
fixtureResult.setPassedFlag();
return fixtureResult;
};
return runFixtureInternal(testFixture, options);
}
};
ecmaUnit.FixtureResult = function(){
var that = this;
this.testResults = [];
this.passCount = 0;
this.failCount = 0;
this.skippedCount = 0;
this.stringify = function(){
var lines = [];
lines.push('Test Results');
lines.push('============');
lines.push('Ran');
that.testResults.forEach(function(testResult){
var line = testResult.testName + '\t-\t' + testResult.result;
lines.push(line);
if (testResult.result === 'fail'){
lines.push('\tException: \'' + testResult.exception + '\'');
if (testResult.exception.stack){
lines.push('\tAt: ' + testResult.exception.stack);
}
}
});
return lines.join('\n');
};
this.setPassedFlag = function(fixtureResult){
if (that.failCount === 0){
that.passed = true;
} else {
that.passed = false;
}
};
};
var assert = assert || {};
assert.areEqual = function(expected, actual, message){
var isDate = function(value){
if (typeof(value) === "object"){
if (value.getTime){
return true;
}
}
return false;
};
var isEqual = function(v1, v2){
if (v1 === null && v2 === null){
return true;
}
if (isDate(v1) && isDate(v2)){
return (v1.getTime() === v2.getTime());
}
return expected === actual;
}
if (isEqual(expected, actual)){
return;
}else{
var errorMessage = "Expected '" + expected + "' but was '" + actual + "'";
if (message){
errorMessage += '\nMessage: ' + message;
}
throw new Error(errorMessage);
}
};
assert.fail = function(message){
throw new Error('Fail: ' + message);
};
assert.stringContains = function(string, match){
if (string.indexOf(match) === -1){
throw new Error('String "' + string + '" did not contain "' + match + '"');
}
};
assert.isTrue = function(value){
if(!value){
throw new Error('Expected truthy, but was: ' + value);
}
};
assert.isFalse = function(value){
if(value){
throw new Error('Expected falsy, but was: ' + value);
}
};
// Export the module for nodejs
var module = module || null;
if (module){
module.exports= {
ecmaUnit: ecmaUnit,
assert: assert
}
}