-
Notifications
You must be signed in to change notification settings - Fork 785
/
test.js
181 lines (147 loc) 路 4.88 KB
/
test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
QUnit.test( "expect query and multiple issue", function( assert ) {
assert.expect( 2 );
assert.ok( true );
var expected = assert.expect();
assert.equal( expected, 2 );
assert.expect( expected + 1 );
assert.ok( true );
} );
if ( typeof document !== "undefined" ) {
QUnit.module( "fixture", function( hooks ) {
var failure = false,
values = [ /* initial value (see unshift below), */ "<b>ar</b>", undefined ],
originalValue;
hooks.before( function() {
originalValue = QUnit.config.fixture;
values.unshift( originalValue );
} );
hooks.beforeEach( function( assert ) {
assert.fixtureEquals = function fixtureEquals( options ) {
var expectedTagName = options.tagName || "div";
var expectedAttributes = options.attributes || {};
var expectedContent = options.content || "";
var element = document.getElementById( "qunit-fixture" );
this.pushResult( {
result: element.tagName === expectedTagName.toUpperCase(),
actual: element.tagName.toLowerCase(),
expected: expectedTagName,
message: "tagName"
} );
var actualAttributes = {};
for ( var i = 0, l = element.attributes.length; i < l; i++ ) {
actualAttributes[ element.attributes[ i ].name ] = element.attributes[ i ].value;
}
this.deepEqual( actualAttributes, expectedAttributes, "attributes" );
this.strictEqual( element.innerHTML, expectedContent, "contents" );
};
assert.hasFailingAssertions = function() {
for ( var i = 0; i < this.test.assertions.length; i++ ) {
if ( !this.test.assertions[ i ].result ) {
return true;
}
}
return false;
};
} );
// Set QUnit.config.fixture for the next test, propagating failures to recover the sequence
hooks.afterEach( function( assert ) {
failure = failure || assert.hasFailingAssertions();
if ( failure ) {
assert.ok( false, "prior failure" );
QUnit.config.fixture = originalValue;
} else {
QUnit.config.fixture = values.shift();
}
} );
QUnit.test( "setup", function( assert ) {
assert.equal( values.length, 3, "proper sequence" );
// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
} );
QUnit.test( "automatically reset", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: originalValue
} );
assert.equal( values.length, 2, "proper sequence" );
} );
QUnit.test( "user-specified", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "<b>ar</b>"
} );
assert.equal( values.length, 1, "proper sequence" );
// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "baz";
} );
QUnit.test( "disabled", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "baz"
} );
assert.equal( values.length, 0, "proper sequence" );
} );
} );
}
QUnit.module( "custom assertions" );
QUnit.assert.mod2 = function( value, expected, message ) {
var actual = value % 2;
this.pushResult( {
result: actual === expected,
actual: actual,
expected: expected,
message: message
} );
};
QUnit.assert.testForPush = function( value, expected, message ) {
this.push( true, value, expected, message, false );
};
QUnit.test( "mod2", function( assert ) {
assert.expect( 2 );
assert.mod2( 2, 0, "2 % 2 == 0" );
assert.mod2( 3, 1, "3 % 2 == 1" );
} );
QUnit.test( "testForPush", function( assert ) {
assert.expect( 6 );
QUnit.log( function( detail ) {
if ( detail.message === "should be call pushResult" ) {
assert.equal( detail.result, true );
assert.equal( detail.actual, 1 );
assert.equal( detail.expected, 1 );
assert.equal( detail.message, "should be call pushResult" );
assert.equal( detail.negative, false );
}
} );
assert.testForPush( 1, 1, "should be call pushResult" );
} );
QUnit.module( "QUnit.skip", {
beforeEach: function( assert ) {
// Skip test hooks for skipped tests
assert.ok( false, "skipped function" );
throw "Error";
},
afterEach: function( assert ) {
assert.ok( false, "skipped function" );
throw "Error";
}
} );
QUnit.skip( "test blocks are skipped", function( assert ) {
// This test callback won't run, even with broken code
assert.expect( 1000 );
throw "Error";
} );
QUnit.skip( "no function" );
QUnit.module( "Missing Callbacks" );
QUnit.test( "QUnit.test without a callback logs a descriptive error", function( assert ) {
assert.throws( function() {
QUnit.test( "should throw an error" );
}, /You must provide a function as a test callback to QUnit.test\("should throw an error"\)/ );
} );
QUnit.test( "QUnit.todo without a callback logs a descriptive error", function( assert ) {
assert.throws( function() {
QUnit.todo( "should throw an error" );
}, /You must provide a function as a test callback to QUnit.todo\("should throw an error"\)/ );
} );