Skip to content

Commit dd68ea9

Browse files
committed
Finished refactoring and beautifying functional tests
1 parent 76d145a commit dd68ea9

File tree

6 files changed

+133
-124
lines changed

6 files changed

+133
-124
lines changed

test/functional/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0"?>
21
<!DOCTYPE html>
32
<html>
43
<head>
@@ -23,7 +22,7 @@ <h3><a href="https://github.com/eriwen">eriwen</a> / <strong><a href="https://gi
2322
... more code of yours ...
2423
&lt;/script&gt;</pre>
2524
</code>
26-
<p>Tested in <a href="testcase1.html" target="_blank">Plain test</a></p>
25+
<p>Tested in <a href="testcase1.html" target="_blank">No-options test</a></p>
2726
<ul>
2827
<li>You can also pass in your own Error to get a stacktrace:</li>
2928
</ul>

test/functional/testCommon.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function toList(array) {
2+
return "<ol><li>" + (array.join("</li><li>")) + "</li></ol>";
3+
}
4+
5+
function printTrace(trace) {
6+
var output = document.getElementById("output");
7+
if (!output) {
8+
output = document.createElement("div");
9+
output.id = "output";
10+
document.body.appendChild(output);
11+
}
12+
13+
var content = [];
14+
content.push(toList(trace));
15+
content.push("--------------Expected:-------------------");
16+
content.push(toList(window.expected || []));
17+
output.innerHTML = (content.join("<br/>"));
18+
}

test/functional/testcase1.html

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
<!DOCTYPE html>
12
<html>
2-
<head><title>normal test</title></head>
3-
<body>
4-
<div id="output"></div>
5-
</body>
6-
<script type="text/javascript" src="../../stacktrace.js"></script>
7-
<script type="text/javascript">function printTrace(trace){var output=document.getElementById("output");if(!output){output=document.createElement("div");output.id="output";document.body.appendChild(output);}
8-
if(!expected){expected="";}
9-
trace.push("--------------Expected:-------------------")
10-
trace.push(expected);output.innerHTML=(trace.join('<br/>'));}
11-
function foo(){bar(2);}
12-
function bar(n){if(n<2){printTrace(printStackTrace());return;}
13-
bar(n-1);}
14-
var expected=["bar(1)","bar(2)","foo()"].join("<br/>");foo();</script>
3+
<head>
4+
<title>No-options test</title>
5+
<script type="text/javascript" src="../../stacktrace.js"></script>
6+
<script type="text/javascript" src="testCommon.js"></script>
7+
</head>
8+
<body>
9+
<div id="output"></div>
10+
<script type="text/javascript">
11+
function bar(n) {
12+
if (n < 2) {
13+
printTrace(printStackTrace());
14+
return;
15+
}
16+
bar(n - 1);
17+
}
18+
19+
function foo() {
20+
bar(2);
21+
}
22+
23+
var expected=["bar(1)", "bar(2)", "foo()"];
24+
foo();
25+
</script>
26+
</body>
1527
</html>

test/functional/testcase2.html

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
<!DOCTYPE html>
12
<html>
2-
<head><title>lastError test</title></head>
3-
<body>
4-
<div id="output"></div>
5-
</body>
6-
<script type="text/javascript" src="../../stacktrace.js"></script>
7-
<script type="text/javascript">function printTrace(trace){var output=document.getElementById("output");if(!output){output=document.createElement("div");output.id="output";document.body.appendChild(output);}
8-
if(!expected){expected="";}
9-
trace.push("--------------Expected:-------------------")
10-
trace.push(expected);output.innerHTML=(trace.join('<br/>'));}
11-
function foo(){bar(2);}
12-
function bar(n){if(n<2){abc();}
13-
bar(n-1);}
14-
var expected=["bar(1)","bar(2)","foo()"].join("<br/>");var lastError;try{foo()}catch(e){lastError=e;printTrace(printStackTrace({e:lastError}));}</script>
3+
<head>
4+
<title>passing error test</title>
5+
<script type="text/javascript" src="../../stacktrace.js"></script>
6+
<script type="text/javascript" src="testCommon.js"></script>
7+
</head>
8+
<body>
9+
<div id="output"></div>
10+
<script type="text/javascript">
11+
function bar(n) {
12+
if (n < 2) {
13+
window.abc();
14+
}
15+
bar(n - 1);
16+
}
17+
18+
function foo() {
19+
bar(2);
20+
}
21+
22+
var expected=["bar(1)", "bar(2)", "foo()"];
23+
24+
var lastError;
25+
try {
26+
foo();
27+
} catch (e) {
28+
lastError = e;
29+
printTrace(printStackTrace({e:lastError}));
30+
}
31+
</script>
32+
</body>
1533
</html>

test/functional/testcase3.html

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
1+
<!DOCTYPE html>
12
<html>
2-
<head>
3-
<title>window.onerror test</title>
4-
<script type="text/javascript" src="../../stacktrace.js"></script>
5-
</head>
6-
<body>
7-
<div id="output"></div>
8-
<script type="text/javascript">
9-
function toList(array) {
10-
return "<ol><li>" + (array.join("</li><li>")) + "</li></ol>";
11-
}
3+
<head>
4+
<title>window.onerror test</title>
5+
<script type="text/javascript" src="../../stacktrace.js"></script>
6+
<script type="text/javascript" src="testCommon.js"></script>
7+
</head>
8+
<body>
9+
<div id="output"></div>
10+
<script type="text/javascript">
11+
function bar(n) {
12+
if (n < 2) {
13+
window.abc();
14+
}
15+
bar(n - 1);
16+
}
1217

13-
var expected = [];
18+
function foo() {
19+
bar(2);
20+
}
1421

15-
function printTrace(trace) {
16-
var output = document.getElementById("output");
17-
if (!output) {
18-
output = document.createElement("div");
19-
output.id = "output";
20-
document.body.appendChild(output);
21-
}
22-
23-
var content = [];
24-
content.push(toList(trace));
25-
content.push("--------------Expected:-------------------");
26-
content.push(toList(expected || []));
27-
output.innerHTML = (content.join("<br/>"));
28-
}
29-
30-
function bar(n) {
31-
if (n < 2) {
32-
window.abc();
33-
}
34-
bar(n - 1);
35-
}
36-
37-
function foo() {
38-
bar(2);
39-
}
40-
41-
window.onerror = function(msg, file, line) {
42-
printTrace(window.printStackTrace());
43-
return true;
44-
};
45-
expected = ["bar(1)", "bar(2)", "foo()"];
46-
foo();
47-
</script>
48-
</body>
22+
window.onerror = function(msg, file, line) {
23+
printTrace(window.printStackTrace());
24+
return true;
25+
};
26+
expected = ["bar(1)", "bar(2)", "foo()"];
27+
foo();
28+
</script>
29+
</body>
4930
</html>

test/functional/testcase4.html

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,34 @@
1+
<!DOCTYPE html>
12
<html>
2-
<head>
3-
<title>function instrumentation test</title>
4-
<script type="text/javascript" src="../../stacktrace.js"></script>
5-
</head>
6-
<body>
7-
<div id="output"></div>
8-
<script type="text/javascript">
9-
function toList(array) {
10-
return "<ol><li>" + (array.join("</li><li>")) + "</li></ol>";
11-
}
12-
13-
var expected = [];
14-
15-
function printTrace(trace) {
16-
var output = document.getElementById("output");
17-
if (!output) {
18-
output = document.createElement("div");
19-
output.id = "output";
20-
document.body.appendChild(output);
21-
}
22-
23-
var content = [];
24-
content.push(toList(trace));
25-
content.push("--------------Expected:-------------------");
26-
content.push(toList(expected || []));
27-
output.innerHTML = (content.join("<br/>"));
28-
}
29-
30-
function baz() {
31-
var c = 3;
32-
}
33-
34-
function bar() {
35-
var b = 2;
36-
baz();
37-
}
38-
39-
var foo = function() {
40-
var a = 1;
41-
bar();
42-
};
43-
44-
var p = new window.printStackTrace.implementation();
45-
p.instrumentFunction(this, 'baz', printTrace);
46-
47-
expected = ["bar()", "foo()"];
48-
foo();
49-
50-
p.deinstrumentFunction(this, 'bar');
51-
</script>
52-
</body>
3+
<head>
4+
<title>function instrumentation test</title>
5+
<script type="text/javascript" src="../../stacktrace.js"></script>
6+
<script type="text/javascript" src="testCommon.js"></script>
7+
</head>
8+
<body>
9+
<div id="output"></div>
10+
<script type="text/javascript">
11+
function baz() {
12+
var c = 3;
13+
}
14+
15+
function bar() {
16+
var b = 2;
17+
baz();
18+
}
19+
20+
var foo = function() {
21+
var a = 1;
22+
bar();
23+
};
24+
25+
var p = new window.printStackTrace.implementation();
26+
p.instrumentFunction(this, 'baz', printTrace);
27+
28+
expected = ["bar()", "foo()"];
29+
foo();
30+
31+
p.deinstrumentFunction(this, 'bar');
32+
</script>
33+
</body>
5334
</html>

0 commit comments

Comments
 (0)