configurableTest.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. Object.defineProperty(RegExp, "fakeProp", {
  6. value: 101,
  7. writable: true,
  8. enumerable: true,
  9. configurable: true
  10. });
  11. var propertyConstructorArray = ["fakeProp","$_","$*","$&","$+","$`","$'","input",
  12. "lastMatch","lastParen","leftContext","rightContext",
  13. "index","length","prototype","constructor"
  14. ];
  15. var propertyInstanceArray = ["global","ignoreCase","lastIndex","multiline","source","sticky","dotAll"];
  16. function RegexTests(i,propertyArray)
  17. {
  18. WScript.Echo("starting Property[",i,"]: ",propertyArray[i]);
  19. //Does the property exist
  20. var doesPropExist = RegExp.hasOwnProperty(propertyArray[i]);
  21. WScript.Echo("Does Property exist: ",doesPropExist);
  22. if(!doesPropExist) return;
  23. //Is the property configurable
  24. var isPropConfig = Object.getOwnPropertyDescriptor(RegExp, propertyArray[i]).configurable;
  25. WScript.Echo("Is the Property configurable: ",isPropConfig);
  26. var canRedefine = false;
  27. var canDelete = false;
  28. if(isPropConfig)
  29. {
  30. canRedefine = true;
  31. canDelete = true;
  32. }
  33. var propValueBefore = RegExp[propertyArray[i]];
  34. try
  35. {
  36. Object.defineProperty(RegExp, propertyArray[i], { get : function () { return 'OVERRIDE' } });
  37. }
  38. catch(err)
  39. {
  40. if(isPropConfig) WScript.Echo("Fail");
  41. else WScript.Echo("PASS, Not Configurable and will not allow redefinition");
  42. }
  43. var deleteProp = false;
  44. if(isPropConfig)
  45. {
  46. if(RegExp[propertyArray[i]] =="OVERRIDE") WScript.Echo("PASS");
  47. else WScript.Echo("FAIL, currently equals: ",RegExp[propertyArray[i]]);
  48. }
  49. else
  50. {
  51. if(RegExp[propertyArray[i]] ==propValueBefore) WScript.Echo("PASS");
  52. else WScript.Echo("FAIL, currently equals: ",RegExp[propertyArray[i]]);
  53. }
  54. deleteProp = delete RegExp[propertyArray[i]];
  55. WScript.Echo("can you delete the property: ",canDelete, "did it actually delete?",deleteProp);
  56. if(deleteProp ==canDelete) WScript.Echo("Pass Delete Test");
  57. else WScript.Echo("Fail Delete Test");
  58. }
  59. function RegexInstanceTests(i,propertyArray)
  60. {
  61. var pattern1=new RegExp("e");
  62. WScript.Echo("starting Property[",i,"]: ",propertyArray[i]);
  63. //Does the property exist
  64. var doesPropExist = pattern1.hasOwnProperty(propertyArray[i]);
  65. WScript.Echo("Does Property exist: ",doesPropExist);
  66. if(!doesPropExist) return;
  67. var isPropConfig = Object.getOwnPropertyDescriptor(pattern1, propertyArray[i]).configurable;
  68. WScript.Echo("Is the Property configurable: ",isPropConfig);
  69. var canRedefine = false;
  70. var canDelete = false;
  71. if(isPropConfig)
  72. {
  73. canRedefine = true;
  74. canDelete = true;
  75. }
  76. var propValueBefore = pattern1[propertyArray[i]];
  77. try
  78. {
  79. Object.defineProperty(pattern1, propertyArray[i], { get : function () { return 'OVERRIDE' } });
  80. }
  81. catch(err)
  82. {
  83. if(isPropConfig) WScript.Echo("Fail");
  84. else WScript.Echo("PASS, Not Configurable and will not allow redefinition");
  85. }
  86. var deleteProp = false;
  87. if(isPropConfig)
  88. {
  89. if(pattern1[propertyArray[i]] =="OVERRIDE") WScript.Echo("PASS");
  90. else WScript.Echo("FAIL, currently equals: ",pattern1[propertyArray[i]]);
  91. }
  92. else
  93. {
  94. if(pattern1[propertyArray[i]] ==propValueBefore) WScript.Echo("PASS");
  95. else WScript.Echo("FAIL, currently equals: ",pattern1[propertyArray[i]]);
  96. }
  97. deleteProp = delete pattern1[propertyArray[i]];
  98. WScript.Echo("can you delete the property: ",canDelete, "did it actually delete?",deleteProp);
  99. if(deleteProp ==canDelete) WScript.Echo("Pass Delete Test");
  100. else WScript.Echo("Fail Delete Test");
  101. }
  102. for(var i = 0; i < propertyConstructorArray.length;i++)
  103. {
  104. RegexTests(i,propertyConstructorArray);
  105. WScript.Echo("\n");
  106. }
  107. for(var i = 0; i < propertyInstanceArray.length;i++)
  108. {
  109. RegexTests(i,propertyInstanceArray);
  110. WScript.Echo("\n");
  111. }
  112. for(var i = 0; i < propertyInstanceArray.length;i++)
  113. {
  114. RegexInstanceTests(i,propertyInstanceArray);
  115. WScript.Echo("\n");
  116. }