Skip to content

Commit 13a0ce7

Browse files
committed
Refactored test suite and included RequireJS optimizer tests.
1 parent 525c403 commit 13a0ce7

File tree

9 files changed

+91
-27
lines changed

9 files changed

+91
-27
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/test/reports/
2+
/test/build_tools/_output/
23
/bower_components/
34
/node_modules/

Gruntfile.coffee

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module.exports = ->
22
@initConfig
33

4+
clean: ["test/build_tools/_output"]
5+
46
jshint: ["loader.js"]
57

68
connect:
79
test:
810
options:
9-
port: 8000
11+
port: 7357
1012

1113
watch:
1214
files: ["loader.js", "test/**/*", "Gruntfile.coffee"]
@@ -16,15 +18,41 @@ module.exports = ->
1618
test:
1719
options:
1820
urls: [
19-
"http://localhost:8000/test/requirejs.html"
20-
"http://localhost:8000/test/dojo.html"
21-
"http://localhost:8000/test/curl.html"
21+
"http://localhost:7357/test/requirejs.html"
22+
"http://localhost:7357/test/dojo.html"
23+
"http://localhost:7357/test/curl.html"
2224
]
2325

26+
requirejs:
27+
test:
28+
options:
29+
baseUrl: "test/build_tools"
30+
include: "r"
31+
out: "test/build_tools/_output/r.js"
32+
optimize: "none"
33+
exclude: ["lodash", "ldsh"]
34+
35+
paths:
36+
lodash: "../../bower_components/lodash/dist/lodash"
37+
ldsh: "../../loader"
38+
2439
@loadNpmTasks "grunt-contrib-jshint"
2540
@loadNpmTasks "grunt-contrib-watch"
2641
@loadNpmTasks "grunt-contrib-connect"
2742
@loadNpmTasks "grunt-contrib-qunit"
43+
@loadNpmTasks "grunt-contrib-clean"
2844
@loadNpmTasks "grunt-clear"
2945

30-
@registerTask "default", ["jshint", "connect", "qunit"]
46+
@loadNpmTasks "grunt-contrib-requirejs"
47+
48+
@registerTask "test", [
49+
"clean"
50+
"requirejs"
51+
"qunit"
52+
]
53+
54+
@registerTask "default", [
55+
"jshint"
56+
"connect"
57+
"test"
58+
]

loader.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,28 @@ define(function(require, exports) {
3232

3333
var settings = configure(config);
3434

35-
// Builds must happen with Node.
35+
// Builds with r.js require Node.js to be installed.
3636
if (config.isBuild) {
3737
var path = settings.root + name + settings.ext;
38+
var contents = "";
39+
40+
try {
41+
// First try reading the filepath as-is.
42+
contents = String(fs.readFileSync(path));
43+
} catch(ex) {
44+
// If it failed, it's most likely because of a leading `/` and not an
45+
// absolute path. Remove the leading slash and try again.
46+
if (path[0] === "/") {
47+
path = path.slice(1);
48+
}
49+
50+
// Try reading again with the leading `/`.
51+
contents = String(fs.readFileSync(path));
52+
}
3853

3954
// Read in the file synchronously, as RequireJS expects, and return the
4055
// contents. Process as a Lo-Dash template.
41-
buildMap[name] = _.template(String(fs.readFileSync(path)));
56+
buildMap[name] = _.template(contents);
4257

4358
return load();
4459
}
@@ -58,7 +73,7 @@ define(function(require, exports) {
5873
};
5974

6075
// Initiate the fetch.
61-
xhr.open("GET", "/" + settings.root + name + settings.ext, true);
76+
xhr.open("GET", settings.root + name + settings.ext, true);
6277
xhr.send(null);
6378
};
6479

@@ -106,6 +121,11 @@ define(function(require, exports) {
106121
templateSettings: {}
107122
}, config.lodashLoader);
108123

124+
// Ensure the root has been properly configured with a trailing slash.
125+
if (settings.root[settings.root.length-1] !== "/") {
126+
settings.root += "/";
127+
}
128+
109129
// Set the custom passed in template settings.
110130
_.extend(_.templateSettings, settings.templateSettings);
111131

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"grunt-contrib-watch": "~0.5.3",
1313
"grunt-clear": "~0.2.1",
1414
"grunt-contrib-connect": "~0.5.0",
15-
"grunt-contrib-qunit": "~0.3.0"
15+
"grunt-contrib-qunit": "~0.3.0",
16+
"grunt-dojo": "~0.2.4",
17+
"grunt-contrib-requirejs": "~0.4.1"
1618
},
1719
"scripts": {
1820
"test": "grunt",

test/build_tools/fixtures/basic.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
It works!

test/build_tools/r.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require(["ldsh!fixtures/basic"]);

test/spec/curl.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
QUnit.module("curl");
88

99
curl.config({
10-
baseUrl: "../",
10+
baseUrl: "/test",
1111

1212
paths: {
13-
"lodash": "bower_components/lodash/dist/lodash",
14-
"ldsh": "loader",
15-
"fixtures": "test/fixtures"
13+
"lodash": "../bower_components/lodash/dist/lodash",
14+
"ldsh": "../loader"
1615
}
1716
});
1817

test/spec/dojo.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
*/
66
QUnit.module("dojo");
77

8-
asyncTest("AMD support", 1, function() {
9-
require({
10-
baseUrl: "../",
8+
require({
9+
baseUrl: "/test",
1110

12-
paths: {
13-
lodash: "bower_components/lodash/dist/lodash",
14-
ldsh: "loader",
15-
fixtures: "test/fixtures"
16-
}
17-
}, ["ldsh!fixtures/template"], function(template) {
11+
paths: {
12+
lodash: "../bower_components/lodash/dist/lodash",
13+
ldsh: "../loader"
14+
}
15+
});
16+
17+
asyncTest("AMD support", 1, function() {
18+
require(["ldsh!fixtures/template"], function(template) {
1819
ok(template(), "It works!");
1920

2021
start();
@@ -48,7 +49,7 @@ asyncTest("templateSettings", function() {
4849
});
4950

5051
asyncTest("relative paths", 1, function() {
51-
require(["fixtures/nested/module"], function(exports) {
52+
require(["./fixtures/nested/module.js"], function(exports) {
5253
ok(exports.template(), "It works!");
5354

5455
start();

test/spec/requirejs.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ QUnit.module("requirejs");
77

88
asyncTest("AMD support", 1, function() {
99
require.config({
10-
baseUrl: "../",
10+
baseUrl: "/test",
1111

1212
paths: {
13-
lodash: "bower_components/lodash/dist/lodash",
14-
ldsh: "loader",
15-
fixtures: "test/fixtures"
13+
lodash: "../bower_components/lodash/dist/lodash",
14+
ldsh: "../loader"
1615
}
1716
});
1817

@@ -56,3 +55,15 @@ asyncTest("relative paths", 1, function() {
5655
start();
5756
});
5857
});
58+
59+
asyncTest("plugin works with r.js optimizer", 1, function() {
60+
// Load the module containing the build.
61+
require(["build_tools/_output/r"], function() {
62+
// Request the template.
63+
require(["ldsh!fixtures/basic"], function(template) {
64+
ok(template(), "It works!");
65+
66+
start();
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)