Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Addding loader plugin tests.

  • Loading branch information...
commit 42ff5f5d94e4a30049dd32cb2738fc828848ab9e 1 parent 09f0f7f
@jrburke jrburke authored
View
3  impl/requirejs/config.js
@@ -10,5 +10,6 @@ var config = require,
implemented = {
basic: true,
anon: true,
- funcString: true
+ funcString: true,
+ plugins: true
};
View
3  tests/plugins/a.js
@@ -0,0 +1,3 @@
+define({
+ name: 'a'
+});
View
4 tests/plugins/a.refine
@@ -0,0 +1,4 @@
+//The refine plugin changes the word refine into define.
+refine({
+ name: 'a'
+});
View
5 tests/plugins/b.js
@@ -0,0 +1,5 @@
+define(function () {
+ return {
+ name: "b"
+ };
+});
View
3  tests/plugins/c.js
@@ -0,0 +1,3 @@
+define({
+ name: "c"
+});
View
50 tests/plugins/double.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Double Plugin Call Test</title>
+ <script src="../bootstrap.js"></script>
+ <script>
+ var master = new doh.Deferred(),
+ count = 0;
+
+ function done() {
+ count += 1;
+ if (count === 2) {
+ master.callback(true);
+ }
+ }
+
+ doh.register(
+ "doublePluginCall",
+ [
+ {
+ name: "doublePluginCall",
+ timeout: 5000,
+ runTest: function() {
+ return master;
+ }
+ }
+ ]
+ );
+ doh.run();
+
+ go(['double!foo'], function (foo) {
+ if (foo === 'x') {
+ done();
+ }
+ });
+
+ go(['double!foo'], function (foo) {
+ if (foo === 'x') {
+ done();
+ }
+ });
+
+ </script>
+</head>
+<body>
+ <h1>Double Plugin Call Test</h1>
+ <p>Tests that the same value is returned for two calls to a plugin.</p>
+ <p>Check console for messages</p>
+</body>
+</html>
View
5 tests/plugins/double.js
@@ -0,0 +1,5 @@
+define({
+ load: function (name, req, load, config) {
+ load('x');
+ }
+});
View
13 tests/plugins/earth.js
@@ -0,0 +1,13 @@
+define(function (require) {
+ return {
+ getA: function () {
+ return require("./index!0?./a:./b:./c");
+ },
+ getC: function () {
+ return require("./index!2?./a:./b:./c");
+ },
+ getB: function () {
+ return require("./index!1?./a:./b:./c");
+ }
+ };
+});
View
12 tests/plugins/fromText-tests.js
@@ -0,0 +1,12 @@
+go(['refine!a'], function (a) {
+
+ doh.register(
+ 'pluginsFromText',
+ [
+ function pluginsFromText(t){
+ t.is('a', a.name);
+ }
+ ]
+ );
+ doh.run();
+});
View
13 tests/plugins/fromText.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>fromText Plugin Test</title>
+ <script src="../bootstrap.js"></script>
+ <script src="fromText-tests.js"></script>
+</head>
+<body>
+ <h1>fromText Plugin Test</h1>
+ <p>Tests a plugin that uses load.fromText from the plugin API</p>
+ <p>Check console for messages</p>
+</body>
+</html>
View
37 tests/plugins/index.js
@@ -0,0 +1,37 @@
+(function () {
+
+ function parse(name) {
+ var parts = name.split('?'),
+ index = parseInt(parts[0], 10),
+ choices = parts[1].split(':'),
+ choice = choices[index];
+
+ return {
+ index: index,
+ choices: choices,
+ choice: choice
+ };
+ }
+
+ define({
+ pluginBuilder: './indexBuilder',
+ normalize: function (name, normalize) {
+ var parsed = parse(name),
+ choices = parsed.choices;
+
+ //Normalize each path choice.
+ for (i = 0; i < choices.length; i++) {
+ choices[i] = normalize(choices[i]);
+ }
+
+ return parsed.index + '?' + choices.join(':');
+ },
+
+ load: function (name, req, load, config) {
+ req([parse(name).choice], function (value) {
+ load(value);
+ });
+ }
+ });
+
+}());
View
44 tests/plugins/indexBuilder.js
@@ -0,0 +1,44 @@
+(function () {
+
+ function parse(name) {
+ var parts = name.split('?'),
+ index = parseInt(parts[0], 10),
+ choices = parts[1].split(':'),
+ choice = choices[index];
+
+ return {
+ index: index,
+ choices: choices,
+ choice: choice
+ };
+ }
+
+ define({
+ normalize: function (name, normalize) {
+ var parsed = parse(name),
+ choices = parsed.choices;
+
+ //Normalize each path choice.
+ for (i = 0; i < choices.length; i++) {
+ choices[i] = normalize(choices[i]);
+ }
+
+ return parsed.index + '?' + choices.join(':');
+ },
+
+ load: function (name, req, load, config) {
+ req([parse(name).choice], function (value) {
+ load(value);
+ });
+ },
+
+ //This is strictly not necessary (and *not* recommended),
+ //but just doing it as a test.
+ write: function (pluginName, moduleName, write) {
+ var parsed = parse(moduleName);
+ write("define('" + pluginName + "!" + moduleName +
+ "', ['" + parsed.choice + "'], function (value) { return value;});\n");
+ }
+ });
+
+}());
View
18 tests/plugins/normalize-tests.js
@@ -0,0 +1,18 @@
+go( ['require', 'earth', 'prime/earth'],
+function (require, earth, primeEarth) {
+
+ doh.register(
+ "pluginsNormalize",
+ [
+ function pluginsNormalize(t){
+ t.is("a", earth.getA().name);
+ t.is("c", earth.getC().name);
+ t.is("b", earth.getB().name);
+ t.is("aPrime", primeEarth.getA().name);
+ t.is("cPrime", primeEarth.getC().name);
+ t.is("bPrime", primeEarth.getB().name);
+ }
+ ]
+ );
+ doh.run();
+});
View
13 tests/plugins/normalize.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Plugin Normalize Test</title>
+ <script src="../bootstrap.js"></script>
+ <script src="normalize-tests.js"></script>
+</head>
+<body>
+ <h1>Plugin Normalize Test</h1>
+ <p>Tests a plugin that implements normalize()</p>
+ <p>Check console for messages</p>
+</body>
+</html>
View
3  tests/plugins/prime/a.js
@@ -0,0 +1,3 @@
+define({
+ name: 'aPrime'
+});
View
5 tests/plugins/prime/b.js
@@ -0,0 +1,5 @@
+define(function () {
+ return {
+ name: "bPrime"
+ };
+});
View
3  tests/plugins/prime/c.js
@@ -0,0 +1,3 @@
+define({
+ name: "cPrime"
+});
View
13 tests/plugins/prime/earth.js
@@ -0,0 +1,13 @@
+define(function (require) {
+ return {
+ getA: function () {
+ return require("../index!0?./a:./b:./c");
+ },
+ getC: function () {
+ return require("../index!2?./a:./b:./c");
+ },
+ getB: function () {
+ return require("../index!1?./a:./b:./c");
+ }
+ };
+});
View
130 tests/plugins/refine.js
@@ -0,0 +1,130 @@
+
+/*jslint strict: false, plusplus: false */
+/*global define: false, require: false, XMLHttpRequest: false, ActiveXObject: false,
+ window: false, Packages: false, java: false, process: false */
+
+(function () {
+ //Load the text plugin, so that the XHR calls can be made.
+ var buildMap = {}, fetchText, fs,
+ progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
+
+ function createXhr() {
+ //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
+ var xhr, i, progId;
+ if (typeof XMLHttpRequest !== "undefined") {
+ return new XMLHttpRequest();
+ } else {
+ for (i = 0; i < 3; i++) {
+ progId = progIds[i];
+ try {
+ xhr = new ActiveXObject(progId);
+ } catch (e) {}
+
+ if (xhr) {
+ progIds = [progId]; // so faster next time
+ break;
+ }
+ }
+ }
+
+ if (!xhr) {
+ throw new Error("require.getXhr(): XMLHttpRequest not available");
+ }
+
+ return xhr;
+ }
+
+ if (typeof window !== "undefined" && window.navigator && window.document) {
+ fetchText = function (url, callback) {
+ var xhr = createXhr();
+ xhr.open('GET', url, true);
+ xhr.onreadystatechange = function (evt) {
+ //Do not explicitly handle errors, those should be
+ //visible via console output in the browser.
+ if (xhr.readyState === 4) {
+ callback(xhr.responseText);
+ }
+ };
+ xhr.send(null);
+ };
+ } else if (typeof process !== "undefined" &&
+ process.versions &&
+ !!process.versions.node) {
+ //Using special require.nodeRequire, something added by r.js.
+ fs = require.nodeRequire('fs');
+
+ fetchText = function (url, callback) {
+ callback(fs.readFileSync(url, 'utf8'));
+ };
+ } else if (typeof Packages !== 'undefined') {
+ //Why Java, why is this so awkward?
+ fetchText = function (url, callback) {
+ var encoding = "utf-8",
+ file = new java.io.File(url),
+ lineSeparator = java.lang.System.getProperty("line.separator"),
+ input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
+ stringBuffer, line,
+ content = '';
+ try {
+ stringBuffer = new java.lang.StringBuffer();
+ line = input.readLine();
+
+ // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
+ // http://www.unicode.org/faq/utf_bom.html
+
+ // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
+ // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
+ if (line && line.length() && line.charAt(0) === 0xfeff) {
+ // Eat the BOM, since we've already found the encoding on this file,
+ // and we plan to concatenating this buffer with others; the BOM should
+ // only appear at the top of a file.
+ line = line.substring(1);
+ }
+
+ stringBuffer.append(line);
+
+ while ((line = input.readLine()) !== null) {
+ stringBuffer.append(lineSeparator);
+ stringBuffer.append(line);
+ }
+ //Make sure we return a JavaScript string and not a Java string.
+ content = String(stringBuffer.toString()); //String
+ } finally {
+ input.close();
+ }
+ callback(content);
+ };
+ }
+
+ define(function () {
+ return {
+ load: function (name, parentRequire, load, config) {
+ var url = parentRequire.toUrl(name + '.refine');
+ fetchText(url, function (text) {
+ text = text.replace(/refine/g, 'define');
+
+ if (config.isBuild) {
+ buildMap[name] = text;
+ }
+
+ //Add in helpful debug line
+ text += "\r\n//@ sourceURL=" + url;
+
+ load.fromText(name, text);
+
+ parentRequire([name], function (value) {
+ load(value);
+ });
+ });
+ },
+
+ write: function (pluginName, name, write) {
+ if (name in buildMap) {
+ var text = buildMap[name];
+ write.asModule(pluginName + "!" + name, text);
+ }
+ }
+ };
+ });
+
+}());
View
9 tests/tests.js
@@ -6,7 +6,8 @@
levelNames = [
'basic',
'anon',
- 'funcString'
+ 'funcString',
+ 'plugins'
],
i, levels, name;
@@ -30,6 +31,12 @@
funcString: function () {
reg('funcString/funcString');
+ },
+
+ plugins: function () {
+ reg('plugins/double');
+ reg('plugins/fromText');
+ reg('plugins/normalize');
}
Please sign in to comment.
Something went wrong with that request. Please try again.