ES6SubclassableAsync.js 3.8 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 Subclassable async tests -- verifies subclass async behaviors
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var tests = [
  8. {
  9. name: "Subclass of Promise - basic functionality",
  10. body: function (testNum, testName) {
  11. var s = "";
  12. class P extends Promise {}
  13. var p1 = new P(function(resolve, reject) { resolve("foo"); });
  14. var p2 = new P(function(resolve, reject) { reject("quux"); });
  15. s += "a";
  16. assert.isTrue(p1 instanceof P);
  17. function thenFn(result) { assert.isTrue(result === "foo"); s += 'b'; }
  18. function catchFn(result) { assert.isTrue(result === "quux"); s += 'c'; }
  19. function shouldNotRun(result) { assert.isTrue(false); }
  20. p1.then(thenFn, shouldNotRun);
  21. p2.then(shouldNotRun, catchFn);
  22. p1.catch(shouldNotRun);
  23. p2.catch(catchFn);
  24. p1.then(function() {
  25. // P.prototype.then() should return a new P
  26. assert.isTrue(p1.then() instanceof P && p1.then() !== p1);
  27. s += 'd';
  28. check();
  29. });
  30. function check() {
  31. print("Result of test #" + testNum + " " + testName);
  32. print(s);
  33. }
  34. }
  35. },
  36. {
  37. name: "Subclass of Promise - all",
  38. body: function (testNum, testName) {
  39. var s = "";
  40. class P extends Promise {}
  41. var fulfills = P.all([
  42. new Promise(function(resolve) { resolve("foo"); }),
  43. new Promise(function(resolve) { resolve("foo"); }),
  44. ]);
  45. s += "a";
  46. var rejects = P.all([
  47. new Promise(function(_, reject) { reject("bar"); }),
  48. new Promise(function(_, reject) { reject("bar"); }),
  49. ]);
  50. assert.isTrue(fulfills instanceof P);
  51. fulfills.then(function(result) { assert.isTrue(result + "" === "foo,foo"); s += 'b'; });
  52. rejects.catch(function(result) { assert.isTrue(result === "bar"); s += 'c'; check(); });
  53. function check() {
  54. print("Result of test #" + testNum + " " + testName);
  55. print(s);
  56. }
  57. }
  58. },
  59. {
  60. name: "Subclass of Promise - race",
  61. body: function (testNum, testName) {
  62. var s = "";
  63. class P extends Promise {}
  64. var fulfills = P.race([
  65. new Promise(function(resolve) { resolve("foo"); }),
  66. new Promise(function(_, reject) { reject("bar"); }),
  67. ]);
  68. s += "a";
  69. var rejects = P.race([
  70. new Promise(function(_, reject) { reject("baz"); }),
  71. new Promise(function(resolve) { resolve("qux"); }),
  72. ]);
  73. assert.isTrue(fulfills instanceof P);
  74. fulfills.then(function(result) { assert.isTrue(result === "foo"); s += 'b' });
  75. rejects.catch(function(result) { assert.isTrue(result === "baz"); s += 'c'; check(); });
  76. function check() {
  77. print("Result of test #" + testNum + " " + testName);
  78. print(s);
  79. }
  80. }
  81. },
  82. ];
  83. for(var i=0; i<tests.length; i++ )
  84. {
  85. var test=tests[i];
  86. print("Executing test #" + (i+1) + " " + test.name);
  87. test.body(i+1, test.name);
  88. }