You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pm...@apache.org on 2012/02/21 18:04:46 UTC
git commit: fix failing CJS test cases
Updated Branches:
refs/heads/issue-272 [created] 0104bce78
fix failing CJS test cases
https://issues.apache.org/jira/browse/CB-272
Fixed up require to pass the CJS tests.
Also added new tests for 'module.exports = ...' and
define.remove().
I also changed the thrown error messages in require.js to
'quote' the module name when printed. This allows folks to
see if they've got white-space in prefixing or suffixing
their module ids.
Minor change the CJS test suite to log a console message
for each test suite. It was difficult to debug where
problems were occurring without it.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/0104bce7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/0104bce7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/0104bce7
Branch: refs/heads/issue-272
Commit: 0104bce788b215ad0c96ab9311da0aea9d4d9c5c
Parents: d734199
Author: Patrick Mueller <pm...@apache.org>
Authored: Tue Feb 21 12:01:38 2012 -0500
Committer: Patrick Mueller <pm...@apache.org>
Committed: Tue Feb 21 12:01:38 2012 -0500
----------------------------------------------------------------------
lib/require.js | 139 ++++++++++++---
test/commonjs/launcher-in-iframe.template.html | 1 +
test/test.require.js | 6 +-
thirdparty/commonjs-tests/remove/basic/foo.js | 20 ++
thirdparty/commonjs-tests/remove/basic/program.js | 34 ++++
thirdparty/commonjs-tests/remove/basic/test.js | 33 ++++
thirdparty/commonjs-tests/set-exports/basic/foo.js | 20 ++
.../commonjs-tests/set-exports/basic/program.js | 23 +++
.../commonjs-tests/set-exports/basic/test.js | 33 ++++
9 files changed, 283 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/lib/require.js
----------------------------------------------------------------------
diff --git a/lib/require.js b/lib/require.js
index ac6946a..2e6f0ea 100644
--- a/lib/require.js
+++ b/lib/require.js
@@ -1,43 +1,136 @@
-var require,
- define;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var require
+var define
(function () {
- var modules = {};
+ var modules = {}
+
+ //--------------------------------------------------------------------------
+ // someone else defined "define" and "require", so leave
+ //--------------------------------------------------------------------------
+ if (typeof define !== 'undefined') return
+ if (typeof require !== 'undefined') return
+
+ //--------------------------------------------------------------------------
+ function hop(object, property) {
+ return Object.prototype.hasOwnProperty.call(object, property)
+ }
+ //--------------------------------------------------------------------------
function build(module) {
- var factory = module.factory;
- module.exports = {};
- delete module.factory;
- factory(require, module.exports, module);
- return module.exports;
+ var factory = module.factory
+
+ module.exports = {}
+ delete module.factory
+
+ var modRequire = getRequire(module)
+ factory(modRequire, module.exports, module)
+
+ return module.exports
+ }
+
+ //--------------------------------------------------------------------------
+ function getRequire(parentModule) {
+ return function require(id) {
+ return requireResolved(parentModule, id)
+ }
}
- require = function (id) {
- if (!modules[id]) {
- throw "module " + id + " not found";
+ //--------------------------------------------------------------------------
+ function requireResolved(parentModule, id) {
+ normalizedId = normalize(parentModule.id, id)
+
+ if (!hop(modules, normalizedId)) {
+ throw 'module "' + normalizedId + '" not found'
}
- return modules[id].factory ? build(modules[id]) : modules[id].exports;
+
+ id = normalizedId
+ return modules[id].factory ? build(modules[id]) : modules[id].exports
}
+ //--------------------------------------------------------------------------
+ function normalize(parentModuleId, id) {
+
+ // only need to interpret path if it starts with "."
+ if (id[0] != '.') return id
+
+ // build the full combined path
+ var path = (parentModuleId == "") ? id : parentModuleId + "/../" + id
+
+ // interpret the "." and ".." bits of the path
+ var pathParts = path.split('/')
+ var resultParts = []
+
+ for (var i=0; i<pathParts.length; i++) {
+ var pathPart = pathParts[i]
+
+ if (pathPart == '.') {
+ }
+
+ else if (pathPart == '..') {
+ if (resultParts.length > 0) {
+ resultParts.pop()
+ }
+ else {
+ throw 'module "' + id + '" not found, too many .."s'
+ }
+ }
+
+ else {
+ resultParts.push(pathPart)
+ }
+ }
+
+ return resultParts.join('/')
+ }
+
+ //--------------------------------------------------------------------------
+ require = getRequire({
+ id: '',
+ exports: {}
+ })
+
+ //--------------------------------------------------------------------------
define = function (id, factory) {
- if (modules[id]) {
- throw "module " + id + " already defined";
+ if (hop(modules, id)) {
+ throw 'module "' + id + '" already defined'
}
modules[id] = {
- id: id,
+ id: id,
factory: factory
- };
+ }
}
+ //--------------------------------------------------------------------------
define.remove = function (id) {
- delete modules[id];
- };
+ delete modules[id]
+ }
-})();
+})()
-//Export for use in node
-if (typeof module === "object" && typeof require === "function") {
- module.exports.require = require;
- module.exports.define = define;
+//------------------------------------------------------------------------------
+// export for use in node
+//------------------------------------------------------------------------------
+if (typeof module === 'object' && typeof require === 'function') {
+ module.exports.require = require
+ module.exports.define = define
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/test/commonjs/launcher-in-iframe.template.html
----------------------------------------------------------------------
diff --git a/test/commonjs/launcher-in-iframe.template.html b/test/commonjs/launcher-in-iframe.template.html
index 62d5ea8..84a815e 100644
--- a/test/commonjs/launcher-in-iframe.template.html
+++ b/test/commonjs/launcher-in-iframe.template.html
@@ -39,6 +39,7 @@ function onLoad() {
window.parent.testsStarting("@title@")
}
+ console.log("running test @title@")
try {
require("program")
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/test/test.require.js
----------------------------------------------------------------------
diff --git a/test/test.require.js b/test/test.require.js
index b6e3ea8..e5e671f 100644
--- a/test/test.require.js
+++ b/test/test.require.js
@@ -17,7 +17,7 @@ describe("require", function () {
it("throws an error the module already exists", function () {
expect(function () {
define("cordova", function () {});
- }).toThrow("module cordova already defined");
+ }).toThrow("module \"cordova\" already defined");
});
it("doesn't call the factory method when defining", function () {
@@ -31,7 +31,7 @@ describe("require", function () {
it("throws an exception when module doesn't exist", function () {
expect(function () {
require("your mom");
- }).toThrow("module your mom not found");
+ }).toThrow("module \"your mom\" not found");
});
it("calls the factory method when requiring", function () {
@@ -39,7 +39,7 @@ describe("require", function () {
define("dino", factory);
require("dino");
- expect(factory).toHaveBeenCalledWith(require,
+ expect(factory).toHaveBeenCalledWith(jasmine.any(Function),
{}, {
id: "dino",
exports: {}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/remove/basic/foo.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/remove/basic/foo.js b/thirdparty/commonjs-tests/remove/basic/foo.js
new file mode 100644
index 0000000..d21ab06
--- /dev/null
+++ b/thirdparty/commonjs-tests/remove/basic/foo.js
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+module.exports = 42
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/remove/basic/program.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/remove/basic/program.js b/thirdparty/commonjs-tests/remove/basic/program.js
new file mode 100644
index 0000000..493ea5c
--- /dev/null
+++ b/thirdparty/commonjs-tests/remove/basic/program.js
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var test = require('test');
+var foo = require('foo');
+
+define.remove('foo')
+
+var threw = false
+try {
+ foo = require('foo')
+}
+catch (e) {
+ threw = true
+}
+
+test.assert(threw === true, 'define.remove() did not remove the module');
+test.print('DONE', 'info');
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/remove/basic/test.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/remove/basic/test.js b/thirdparty/commonjs-tests/remove/basic/test.js
new file mode 100644
index 0000000..f3c3f27
--- /dev/null
+++ b/thirdparty/commonjs-tests/remove/basic/test.js
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/set-exports/basic/foo.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/set-exports/basic/foo.js b/thirdparty/commonjs-tests/set-exports/basic/foo.js
new file mode 100644
index 0000000..edbd348
--- /dev/null
+++ b/thirdparty/commonjs-tests/set-exports/basic/foo.js
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+module.exports = 234
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/set-exports/basic/program.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/set-exports/basic/program.js b/thirdparty/commonjs-tests/set-exports/basic/program.js
new file mode 100644
index 0000000..c3956d9
--- /dev/null
+++ b/thirdparty/commonjs-tests/set-exports/basic/program.js
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var test = require('test');
+var foo = require('foo');
+test.assert(foo == 234, 'basic use of "module.exports = ..."');
+test.print('DONE', 'info');
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0104bce7/thirdparty/commonjs-tests/set-exports/basic/test.js
----------------------------------------------------------------------
diff --git a/thirdparty/commonjs-tests/set-exports/basic/test.js b/thirdparty/commonjs-tests/set-exports/basic/test.js
new file mode 100644
index 0000000..f3c3f27
--- /dev/null
+++ b/thirdparty/commonjs-tests/set-exports/basic/test.js
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License') you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+exports.print = typeof print !== "undefined" ? print : function () {
+ var system = require("system");
+ var stdio = system.stdio;
+ stdio.print.apply(stdio, arguments);
+};
+
+exports.assert = function (guard, message) {
+ if (guard) {
+ exports.print('PASS ' + message, 'pass');
+ } else {
+ exports.print('FAIL ' + message, 'fail');
+ }
+};
+