Skip to content

Commit 7e7e653

Browse files
committed
Esprima sanity test
1 parent df7577f commit 7e7e653

File tree

8 files changed

+169
-125
lines changed

8 files changed

+169
-125
lines changed

test.html

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22

33
<html>
44
<head>
5-
<meta charset="utf-8">
6-
<title>Mocha Tests</title>
7-
<link rel="stylesheet" href="lib/mocha.css" />
8-
<style>
9-
#fixture {
10-
position: absolute;
11-
top: -9999;
12-
left: -9999;
13-
};
14-
</style>
15-
<script src="lib/chai.js"></script>
16-
<script src="lib/mocha.js"></script>
17-
<script> mocha.setup('bdd');</script>
18-
<script src="test/test-browser.js"></script>
19-
<script>
20-
window.onload = function() {
21-
mocha.run()
22-
};
23-
</script>
24-
</head>
5+
<meta charset="utf-8">
6+
<title>Mocha Tests</title>
7+
<link rel="stylesheet" href="lib/mocha.css" />
8+
<style>
9+
#fixture {
10+
position: absolute;
11+
top: -9999;
12+
left: -9999;
13+
};
14+
</style>
15+
<!-- Lib and compiled JS -->
16+
<script src="lib/esprima.js"></script>
17+
<!-- Test libs -->
18+
<script src="lib/chai.js"></script>
19+
<script src="lib/mocha.js"></script>
20+
<!-- Test setup, load, run -->
21+
<script> mocha.setup('bdd');</script>
22+
<script src="test/run-browser.js"></script>
23+
<script>
24+
window.onload = function() {
25+
mocha.run();
26+
};
27+
</script>
28+
</head>
2529
<body>
26-
<div id="mocha"></div>
30+
<div id="mocha"></div>
2731
</body>
2832
</html>

test/esprima.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
describe('Dependencies Functionality', function() {
2+
describe('Esprima Core Functionality', function() {
3+
it('Sanity Check', function() {
4+
assert.ok(esprima);
5+
assert.ok(esprima.parse);
6+
});
7+
8+
describe('VariableDeclaration parsing', function() {
9+
var p = null;
10+
11+
// Ensure the remaining test cases run properly if run independently
12+
before(function() {
13+
assert.ok(p = esprima.parse("var hello='world';"));
14+
});
15+
16+
it('Variable statement parsing', function() {
17+
assert.ok(p = esprima.parse("var hello='world';"));
18+
});
19+
20+
it('Root AST type check', function() {
21+
assert.equal(p.sourceType, "script");
22+
assert.equal(p.type, "Program");
23+
24+
});
25+
26+
it('Root AST - has child elements check', function() {
27+
assert.ok(p.body);
28+
assert.ok(p.body[0]);
29+
});
30+
31+
it('VariableDeclaration - body[0] - First child element check', function() {
32+
assert.equal(p.body[0].type, "VariableDeclaration");
33+
assert.equal(p.body[0].kind, "var");
34+
35+
assert.equal(p.body[0].declarations[0].id.name, "hello");
36+
assert.equal(p.body[0].declarations[0].id.type, "Identifier");
37+
38+
assert.equal(p.body[0].declarations[0].init.raw, "'world'");
39+
assert.equal(p.body[0].declarations[0].init.type, "Literal");
40+
assert.equal(p.body[0].declarations[0].init.value, "world");
41+
});
42+
});
43+
});
44+
});

test/hello.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/run-browser.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Setup chai assert - for the browser
2+
var assert = assert || chai.assert;
3+
4+
//
5+
// I use this hackerish "require" syntax that loads the scripts Synchrounous
6+
// BECAUSE this will make the test-suite script CONSISTENT in both node and browser.
7+
//
8+
/**
9+
* Minimal node.js compatible require() for the browser.
10+
*
11+
* For documentation, see Github:
12+
* https://github.com/trausti/TKRequire.js
13+
*
14+
* Heavily based on StackOverflow answers by Lucio M. Tato and Ilya Kharlamov in thread:
15+
* http://stackoverflow.com/questions/6971583/node-style-require-for-in-browser-javascript
16+
*
17+
* MIT license.
18+
*/
19+
function require(url) {
20+
if (url.toLowerCase().substr(-3)!=='.js') {
21+
url+='.js'; // To allow loading without js suffix.
22+
}
23+
if (!require.cache) {
24+
require.cache=[]; // Init cache.
25+
}
26+
27+
if (!require.relativePath) {
28+
require.relativePath = '';
29+
//console.log("TKRequire: initializing relativePath");
30+
}
31+
var originalPath = require.relativePath;
32+
if ("http" === url.substr(0, 4)) {
33+
// If full href is given, extract relative path, if any.
34+
var baseDir = window.location.href.substring(0, window.location.href.lastIndexOf('/'));
35+
var scriptDir = url.substring(0, url.lastIndexOf('/'));
36+
if (0 == url.indexOf(baseDir)) {
37+
require.relativePath = scriptDir.substring(baseDir.length + 1) + '/';
38+
//console.log("TKRequire: extractiong relative path" + require.relativePath);
39+
}
40+
} else if ("./" === url.substr(0, 2)) {
41+
require.relativePath = require.relativePath + url.substring(2, url.lastIndexOf('/') + 1);
42+
//console.log("TKRequire: Extending Path : " + require.relativePath);
43+
}
44+
var scriptName = url.substring(url.lastIndexOf('/') + 1);
45+
//console.log("TKRequire: scriptName :" + scriptName);
46+
47+
var exports = require.cache[url]; // Get from cache.
48+
if (!exports) { // Not cached.
49+
try {
50+
exports = {};
51+
var X = new XMLHttpRequest();
52+
var fullOrRelativePath = "";
53+
if ("http" === url.substr(0, 4)) {
54+
fullOrRelativePath = url;
55+
} else {
56+
fullOrRelativePath = "./" + require.relativePath + scriptName;
57+
};
58+
//console.log("TKRequire: including: " + fullOrRelativePath);
59+
X.open("GET", fullOrRelativePath, false); // Synchrounous load.
60+
X.send();
61+
if (X.status && X.status !== 200) {
62+
throw new Error(X.statusText);
63+
}
64+
var source = X.responseText;
65+
// Fix (if saved form for Chrome Dev Tools)
66+
if (source.substr(0, 10)==="(function(") {
67+
var moduleStart = source.indexOf('{');
68+
var moduleEnd = source.lastIndexOf('})');
69+
var CDTcomment = source.indexOf('//@ ');
70+
if (CDTcomment >- 1 && CDTcomment < moduleStart + 6) {
71+
moduleStart = source.indexOf('\n', CDTcomment);
72+
}
73+
source = source.slice(moduleStart + 1, moduleEnd - 1);
74+
}
75+
// Fix, add comment to show source on Chrome Dev Tools
76+
source = "//@ sourceURL=" + window.location.origin + url + "\n" + source;
77+
//------
78+
var module = { id: url, uri: url, exports: exports }; // According to node.js modules
79+
// Create a Fn with module code, and 3 params: require, exports & module
80+
var anonFn = new Function("require", "exports", "module", source);
81+
anonFn(require, exports, module); // Call the Fn, Execute the module
82+
require.cache[url] = exports = module.exports; // Cache obj exported by module.
83+
} catch (err) {
84+
throw new Error("Error loading module " + url + ": " + err);
85+
}
86+
}
87+
// Restore the relative path.
88+
require.relativePath = originalPath;
89+
90+
return exports; // Require returns object exported by module
91+
}
92+
require.relativePath = './test/'
93+
94+
// Load all the test suites
95+
require("./run-suite.js");
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Setup chai assert - for nodejs
22
assert = require('chai').assert;
33

4+
// Setup esprima library
5+
esprima = require('esprima');
6+
47
// Setup chai assert
5-
require("./test-suite.js");
8+
require("./run-suite.js");

test/run-suite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("./esprima.js");

test/test-browser.js

Lines changed: 0 additions & 95 deletions
This file was deleted.

test/test-suite.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)