dotAll.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. const dotAllMatch = [
  7. "hel\nlo",
  8. "hel\rlo",
  9. "hel\u2028lo",
  10. "hel\u2029lo"
  11. ];
  12. const alwaysMatch = [
  13. "hel\vlo",
  14. "hel\flo",
  15. "hel\u0085lo"
  16. ];
  17. const neverMatch = [
  18. "hel\n\nlo",
  19. "hel\rllo",
  20. "hel\u2028llo",
  21. "hell\u2029lo",
  22. "hel \vlo",
  23. "hel \flo",
  24. "hel \u0085lo"
  25. ];
  26. const tests = [
  27. {
  28. name : "Match without dotAll",
  29. body : function ()
  30. {
  31. const reg = /hel.lo/;
  32. neverMatch.forEach(function (string) {
  33. assert.isFalse(reg.test(string), "Shouldn't match");
  34. });
  35. dotAllMatch.forEach(function (string) {
  36. assert.isFalse(reg.test(string), "Shouldn't match - strings that match with dotAll flag without the flag");
  37. });
  38. alwaysMatch.forEach(function (string) {
  39. assert.isTrue(reg.test(string), "Should match - strings that match without dotAll flag");
  40. });
  41. }
  42. },
  43. {
  44. name : "Match with dotAll",
  45. body : function ()
  46. {
  47. const reg = /hel.lo/s;
  48. neverMatch.forEach(function (string) {
  49. assert.isFalse(reg.test(string), "Shouldn't match");
  50. });
  51. dotAllMatch.forEach(function (string) {
  52. assert.isTrue(reg.test(string), "Should match - strings that match with dotAll flag");
  53. });
  54. alwaysMatch.forEach(function (string) {
  55. assert.isTrue(reg.test(string), "Should match - strings that match without dotAll flag");
  56. });
  57. }
  58. },
  59. {
  60. name : "Properties of dotAll property",
  61. body : function ()
  62. {
  63. const withFlag = /stuff/s;
  64. const withoutFlag = /stuff/;
  65. assert.isTrue(withFlag.dotAll, "dotAll flag has correct value");
  66. assert.isFalse(withoutFlag.dotAll, "dotAll flag has correct value");
  67. assert.isTrue(delete withFlag.dotAll, "deleting dotAll property returns true");
  68. assert.doesNotThrow(()=>{"use strict"; delete withFlag.dotAll;}, "deleting dotAll property does not throw in strict mode");
  69. assert.isTrue(delete withoutFlag.dotAll, "deleting dotAll property returns true");
  70. assert.doesNotThrow(()=>{"use strict"; delete withoutFlag.dotAll;}, "deleting dotAll property does not throw in strict mode");
  71. assert.isFalse(withFlag.hasOwnProperty("dotAll"), "dotAll property is not a property of individual RegExp");
  72. assert.isFalse(withoutFlag.hasOwnProperty("dotAll"), "dotAll property is not a property of individual RegExp");
  73. }
  74. },
  75. {
  76. name : "Properties of prototype dotAll property",
  77. body : function ()
  78. {
  79. const dotAll = RegExp.prototype.dotAll;
  80. assert.isTrue(RegExp.prototype.hasOwnProperty("dotAll"), "RegExp prototype has dotAll property");
  81. const desc = Object.getOwnPropertyDescriptor(RegExp.prototype, "dotAll");
  82. assert.isTrue(desc.configurable, "dotAll property of prototype is configurable");
  83. assert.isFalse(desc.enumerable, "dotAll property of prototype is not enumerable");
  84. assert.areEqual(dotAll, undefined, "dotAll property of prototype is undefined");
  85. RegExp.prototype.dotAll = 5;
  86. assert.areEqual(dotAll, undefined, "writing to dotAll property of prototype is a no-op");
  87. assert.isTrue(delete RegExp.prototype.dotAll, "deleting dotAll property returns true");
  88. assert.isFalse(RegExp.prototype.hasOwnProperty("dotAll"), "RegExp prototype has no dotAll property after deletion");
  89. const withFlag = /stuff/s;
  90. const withoutFlag = /stuff/;
  91. assert.areEqual(withFlag.dotAll, undefined, "After deleting dotAll from prototype its undefined");
  92. assert.areEqual(withoutFlag.dotAll, undefined, "After deleting dotAll from prototype its undefined");
  93. }
  94. }
  95. ];
  96. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });