Skip to content

Commit 5ad7f58

Browse files
committed
Adjusted tests.
1 parent f01dc5e commit 5ad7f58

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

jquery.json.test.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
/// Tests ///
22

3-
var tests_passed = true;
3+
var tests_fail = 0;
44

5-
// 'console' comes from FireBug
6-
function testJSON(o, full, compact) {
7-
if (compact == undefined)
8-
compact = full;
5+
function _test(o, correct, compact)
6+
{
97
try {
10-
var full_result = $.toJSON(o);
8+
var result = $.toJSON(o, compact);
119
} catch (e) {
12-
if (e.name === full.name) return; // For TypeError, etc
13-
return console.error(e, o); }
14-
try {
15-
var compact_result = $.compactJSON(o);
16-
} catch (e) {
17-
if (e.name === compact.name) return; // For TypeError, etc
18-
return console.error(e, o); }
19-
if (full_result != full)
20-
{ console.error("Conversion Error: %s != %s", full_result, full);
21-
tests_passed = false; }
22-
if (compact_result != compact)
23-
{ console.error("Compact Conversion Error: %s != %s", compact_result, compact);
24-
tests_passed = false; }
10+
if (e.name === correct.name) return; // For TypeError, etc
11+
tests_fail += 1;
12+
throw e;
13+
}
14+
15+
if (result != correct)
16+
{
17+
tests_fail += 1;
18+
throw new Error("Wanted: " + correct.toString() + " -- Got: " + result.toString());
19+
}
20+
}
21+
22+
// 'console' comes from FireBug
23+
function testJSON(o, full, compact) {
24+
try { _test(o, full); }
25+
catch (e) { console.error("Conversion Error:", e.message) }
26+
27+
if (compact == undefined) return;
28+
29+
try { _test(o, compact, true); }
30+
catch (e) { console.error("Compact Conversion Error:", e.message) }
2531
}
2632

2733
console.log("Testing...");
@@ -36,8 +42,8 @@ console.log("Done.")
3642

3743
$(function()
3844
{
39-
if (tests_passed)
40-
$('h1').addClass('passed').append(' PASSED');
45+
if (tests_fail > 0)
46+
$('h1 span').addClass('fail').empty().append("FAIL (" + tests_fail +")");
4147
else
42-
$('h1').addClass('fail').append(' FAIL');
48+
$('h1 span').addClass('passed').empty().append("PASSED");
4349
})

test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</style>
1111
</head>
1212
<body>
13-
<h1>Tests: </h1>
13+
<h1>Tests: <span>Invalid</span></h1>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)