module-namespace.js 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // ES6 Module syntax tests -- verifies syntax of import and export statements
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. function testModuleScript(source, message, shouldFail) {
  8. let testfunc = () => WScript.LoadModule(source);
  9. if (shouldFail) {
  10. let caught = false;
  11. // We can't use assert.throws here because the SyntaxError used to construct the thrown error
  12. // is from a different context so it won't be strictly equal to our SyntaxError.
  13. try {
  14. testfunc();
  15. } catch(e) {
  16. caught = true;
  17. // Compare toString output of SyntaxError and other context SyntaxError constructor.
  18. assert.areEqual(e.constructor.toString(), SyntaxError.toString(), message);
  19. }
  20. assert.isTrue(caught, `Expected error not thrown: ${message}`);
  21. } else {
  22. assert.doesNotThrow(testfunc, message);
  23. }
  24. }
  25. var tests = [
  26. {
  27. name: "Basic import namespace",
  28. body: function () {
  29. testModuleScript('import * as foo from "ValidExportStatements.js"; for (var i in foo) helpers.writeln(i + "=" + foo[i]);', '', false);
  30. }
  31. },
  32. {
  33. name: "import namespace with verification",
  34. body: function () {
  35. testModuleScript('import * as foo from "moduleExport1.js"; for (var i in foo) WScript.Echo(i + "=" + foo[i]);', '', false);
  36. testModuleScript('import * as foo from "moduleExport1.js"; foo.verifyNamespace(foo);', '', false);
  37. testModuleScript('import * as foo from "moduleExport1.js"; foo.changeContext(); foo.verifyNamespace(foo);', '', false);
  38. }
  39. },
  40. {
  41. name: "reexport only",
  42. body: function () {
  43. testModuleScript('import * as foo from "ValidReExportStatements.js"; for (var i in foo) helpers.writeln(i + "=" + foo[i]);', '', false);
  44. }
  45. },
  46. {
  47. name: "complex reexport",
  48. body: function () {
  49. testModuleScript('import * as fooComplex from "ModuleComplexReexports.js"; for (var i in fooComplex) WScript.Echo(i + "=" + fooComplex[i]);', '', false);
  50. }
  51. },
  52. {
  53. name: "namespace as prototype",
  54. body: function () {
  55. testModuleScript('import * as foo from "ValidExportStatements.js"; var childObj = Object.create(foo); for (var i in childObj) helpers.writeln(i + "=" + childObj[i]);', '', false);
  56. }
  57. },
  58. {
  59. name: "namespace internal operations",
  60. body: function () {
  61. let functionBody =
  62. `import * as foo from "ValidExportStatements.js";
  63. assert.areEqual(null, Object.getPrototypeOf(foo), 'namespace prototype is null');
  64. assert.areEqual(false, Object.isExtensible(foo), 'namespace is not extensible');
  65. assert.areEqual(false, Reflect.set(foo, "non_existing", 20), '[[set]] returns false ');
  66. assert.areEqual(undefined, foo.non_existing, 'namespace object is immutable');
  67. assert.areEqual(false, Reflect.set(foo, "4", 20), 'cannot set item in namespace obect');
  68. assert.areEqual(undefined, foo[4], 'cannot export item in namespace obect');
  69. assert.areEqual(false, Reflect.deleteProperty(foo, "var1"), 'cannot delete export in namespace obect');
  70. assert.areEqual(true, Reflect.deleteProperty(foo, "nonevar"), 'cannot delete export in namespace obect');
  71. assert.areEqual(undefined, foo[6], 'cannot get item in namespace obect');
  72. assert.areEqual(false, Reflect.set(foo, Symbol.species, 20), 'no species property');
  73. assert.areEqual(undefined, foo[Symbol.species], 'namespace is not contructor');
  74. assert.areEqual("Module", foo[Symbol.toStringTag], 'namespace toStringTag');
  75. assert.areEqual("[object Module]", Object.prototype.toString.call(foo), 'Object.prototype.toString uses the module namespace @@toStringTag value');
  76. helpers.writeln("in iterator"); for (var i of foo) helpers.writeln(i);
  77. helpers.writeln("done with iterator")
  78. var symbols = Object.getOwnPropertySymbols(foo);
  79. assert.areEqual(2, symbols.length, "two symbols");
  80. assert.areEqual(symbols[0], Symbol.toStringTag, "first symbol is toStringTag");
  81. assert.areEqual(symbols[1], Symbol.iterator, "second symbol is iterator");
  82. assert.throws( function() {Object.setPrototypeOf(foo, Object)}, TypeError, 'Cannot create property for a non-extensible object');
  83. assert.areEqual(true, Reflect.preventExtensions(foo), '[[PreventExtensions]] for namespace object returns true');`;
  84. testModuleScript(functionBody, "Test importing as different binding identifiers", false);
  85. }
  86. },
  87. ];
  88. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });