update-funcexpr.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  6. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. }
  8. var tests = [
  9. {
  10. name: "Functions can overwrite themselves",
  11. body: function () {
  12. function foo1() {
  13. foo1 = 42;
  14. assert.isTrue(typeof foo1 == "number", "foo1 should overwrite itself to a number");
  15. assert.areEqual(42, foo1, "value of foo1 after assignment");
  16. }
  17. foo1();
  18. function foo2() {
  19. foo2 &= 0;
  20. assert.isTrue(typeof foo2 == "number", "foo2 should overwrite itself to a number");
  21. assert.areEqual(0, foo2, "value of foo2 after assignment");
  22. }
  23. foo2();
  24. function foo3() {
  25. foo3 <<= 0;
  26. assert.isTrue(typeof foo3 == "number", "foo3 should overwrite itself to a number");
  27. assert.areEqual(0, foo3, "value of foo3 after assignment");
  28. }
  29. foo3();
  30. function foo4() {
  31. let x = foo4++;
  32. assert.isTrue(isNaN(x), "post-increment should return NaN");
  33. assert.isTrue(isNaN(foo4), "foo4 should overwrite itself");
  34. }
  35. foo4();
  36. function foo5() {
  37. ++foo5;
  38. assert.isTrue(isNaN(foo5), "foo5 should overwrite itself");
  39. }
  40. foo5();
  41. }
  42. },
  43. {
  44. name: "Function expressions cannot overwrite themselves",
  45. body: function () {
  46. (function foo1() {
  47. foo1 = 42;
  48. assert.isTrue(typeof foo1 == "function", "foo1 should not overwrite itself");
  49. })();
  50. (function foo2() {
  51. foo2 &= 0;
  52. assert.isTrue(typeof foo2 == "function", "foo2 should not overwrite itself");
  53. })();
  54. (function foo3() {
  55. foo3 <<= 0;
  56. assert.isTrue(typeof foo3 == "function", "foo3 should not overwrite itself");
  57. })();
  58. (function foo4() {
  59. let x = foo4++;
  60. assert.isTrue(isNaN(x), "post-increment should return NaN");
  61. assert.isTrue(typeof foo4 == "function", "foo4 should not overwrite itself");
  62. })();
  63. (function foo5() {
  64. ++foo5;
  65. assert.isTrue(typeof foo5 == "function", "foo5 should not overwrite itself");
  66. })();
  67. }
  68. }
  69. ];
  70. testRunner.runTests(tests, { verbose: false /*so no need to provide baseline*/ });