Browse Source

require() relative ID test case fixes

These testcases were testing for incorrect behavior.
v1.0-maintenance
Sami Vaarala 10 years ago
parent
commit
2cbeea5840
  1. 13
      ecmascript-testcases/test-commonjs-require-environment.js
  2. 51
      ecmascript-testcases/test-commonjs-require-resolution.js
  3. 20
      ecmascript-testcases/test-commonjs-require-tweaked-id.js

13
ecmascript-testcases/test-commonjs-require-environment.js

@ -9,8 +9,10 @@
/*=== /*===
basic bindings basic bindings
modSearch: foo/bar
modSearch: foo/quux
require: function false require: function false
module: object foo/bar false false false module: object foo/quux false false false
exports: object true exports: object true
===*/ ===*/
@ -19,10 +21,11 @@ exports: object true
var global_require = require; var global_require = require;
Duktape.modSearch = function (id) { Duktape.modSearch = function (id) {
if (id === 'foo') { print('modSearch:', id);
return 'var mod = require("./bar");\n'
}
if (id === 'foo/bar') { if (id === 'foo/bar') {
return 'var mod = require("./quux");\n'
}
if (id === 'foo/quux') {
return 'var pd;\n' + return 'var pd;\n' +
'print("require:", typeof require, require === global_require);\n' + 'print("require:", typeof require, require === global_require);\n' +
'pd = Object.getOwnPropertyDescriptor(module, "id");\n' + 'pd = Object.getOwnPropertyDescriptor(module, "id");\n' +
@ -36,7 +39,7 @@ Duktape.modSearch = function (id) {
print('basic bindings'); print('basic bindings');
function bindingTest() { function bindingTest() {
var mod = require('foo'); var mod = require('foo/bar');
// module.id must be a resolved absolute path so that it can be used // module.id must be a resolved absolute path so that it can be used
// to require the correct module from any other module // to require the correct module from any other module

51
ecmascript-testcases/test-commonjs-require-resolution.js

@ -61,8 +61,12 @@ global require: foo/mod1/. -> TypeError
global require: foo/mod1/.. -> TypeError global require: foo/mod1/.. -> TypeError
Duktape.modSearch baz Duktape.modSearch baz
Duktape.modSearch xxx Duktape.modSearch xxx
Duktape.modSearch baz/xxx Duktape.modSearch xxy
Duktape.modSearch baz/xxx/yyy Duktape.modSearch xxx/yyy
Duktape.modSearch quux/foo
Duktape.modSearch xxz
Duktape.modSearch quux/xxw
Duktape.modSearch quux/xxw/yyy
Duktape.modSearch zzz Duktape.modSearch zzz
Duktape.modSearch www Duktape.modSearch www
===*/ ===*/
@ -144,8 +148,25 @@ function basicResolutionTest() {
print('Duktape.modSearch', id); print('Duktape.modSearch', id);
if (id === 'baz') { if (id === 'baz') {
return 'require("xxx");\n' + // absolute return 'require("xxx");\n' + // absolute
'require("./xxx");\n' + // relative 'require("./xxy");\n' + // relative
'require("./xxx/yyy");\n' + // relative 'require("./xxx/yyy");\n' // relative
;
}
return ''; // return a fake empty module
};
void require('baz');
/*
* Require from inside a module with a few more path components.
*/
Duktape.modSearch = function (id) {
print('Duktape.modSearch', id);
if (id === 'quux/foo') {
return 'require("xxz");\n' + // absolute
'require("./xxw");\n' + // relative
'require("./xxw/yyy");\n' + // relative
'require("../zzz");\n' + // relative 'require("../zzz");\n' + // relative
'require("././../www");\n' 'require("././../www");\n'
; ;
@ -153,7 +174,7 @@ function basicResolutionTest() {
return ''; // return a fake empty module return ''; // return a fake empty module
}; };
void require('baz'); void require('quux/foo');
} }
print('basic resolution'); print('basic resolution');
@ -260,14 +281,14 @@ Duktape.modSearch foo/bar
268: TypeError 268: TypeError
269: TypeError 269: TypeError
270: TypeError 270: TypeError
230: foo/bar 230: bar
231: foo/bar 231: bar
232: foo/bar 232: bar
233: foo/bar 233: bar
234: foo/bar 234: bar
235: foo/bar 235: TypeError
236: foo/bar 236: TypeError
237: foo/bar 237: TypeError
238: TypeError 238: TypeError
239: TypeError 239: TypeError
240: TypeError 240: TypeError
@ -304,8 +325,8 @@ Duktape.modSearch foo/bar
===*/ ===*/
/* Test the current implementation limit for ID lengths. This also /* Test the current implementation limit for ID lengths. This also
* does some boundary value testing for ID length. * does some boundary value testing for ID length.
*/ */
function lengthTest() { function lengthTest() {
var i; var i;

20
ecmascript-testcases/test-commonjs-require-tweaked-id.js

@ -12,24 +12,24 @@
/*=== /*===
Duktape.modSearch quux Duktape.modSearch quux
quux: Error quux: Error
Duktape.modSearch foo/bar/quux
./quux: Error
Duktape.modSearch foo/quux Duktape.modSearch foo/quux
./quux: Error
Duktape.modSearch quux
../quux: Error ../quux: Error
Duktape.modSearch testModule Duktape.modSearch testModule/subModule
Duktape.modSearch test/innerRequire Duktape.modSearch test/innerRequire
testModule: Error testModule: Error
===*/ ===*/
Duktape.modSearch = function (id) { Duktape.modSearch = function (id) {
print('Duktape.modSearch', id); print('Duktape.modSearch', id);
if (id == 'testModule') { if (id == 'testModule/subModule') {
// require.id is non-writable but is configurable, so its value must // require.id is non-writable but is configurable, so its value must
// be changed with Object.defineProperty(). // be changed with Object.defineProperty().
return 'var mod;\n' + return 'var mod;\n' +
'exports.name = "testModule";\n' + 'exports.name = "testModule/subModule";\n' +
'Object.defineProperty(require, "id", { value: "./././testModule/foo/../../test" });\n' + // same as 'test' but non-canonical 'Object.defineProperty(require, "id", { value: "./././testModule/subModule/foo/../../../test/foo" });\n' + // same as 'test/foo' but non-canonical
'mod = require("./innerRequire");\n'; 'mod = require("./innerRequire");\n'; // test/foo + ./innerRequire -> test/innerRequire
} }
throw new Error('cannot find module'); throw new Error('cannot find module');
} }
@ -51,13 +51,13 @@ function tweakedIdentifierTest() {
require.id = './foo//./bar'; // same as 'foo/bar' require.id = './foo//./bar'; // same as 'foo/bar'
globalTest('quux'); globalTest('quux');
globalTest('./quux'); globalTest('./quux'); // foo/bar + ./quux -> foo/quux
globalTest('../quux'); globalTest('../quux'); // foo/bar + ../quux -> quux
delete require.id; delete require.id;
// Module 'id' not relative // Module 'id' not relative
try { try {
mod = require('testModule'); mod = require('testModule/subModule');
print('never here'); print('never here');
} catch (e) { } catch (e) {
print('testModule:', e.name); print('testModule:', e.name);

Loading…
Cancel
Save