Object.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. function write(value) {
  6. WScript.Echo(value);
  7. }
  8. function RunTest(testCase, testCount) {
  9. var testFunction = testCase[0];
  10. var testScenario = testCase[1];
  11. testScenario = " (test " + testCount + "): " + testScenario;
  12. write(testScenario);
  13. try {
  14. var result = testFunction();
  15. if (result == true) {
  16. write("PASS");
  17. }
  18. } catch (e) {
  19. var resultString = "FAILED" + testScenario;
  20. write(resultString + " :: " + e.message);
  21. }
  22. }
  23. function RunAllTests(){
  24. for(var i = 0; i < testList.length; ++i){
  25. RunTest(testList[i], i + 1);
  26. }
  27. }
  28. var testList = [
  29. [Test1, "Object getOwnPropertyDescriptor throws TypeError when the first parameter is either null or undefined"],
  30. [Test2, "Freezing an object with deleted properties"],
  31. [Test3, "Object getOwnPropertyDescriptor works fine when the first parameter is a built-in type except null or undefined"],
  32. ];
  33. // Utility functions
  34. function Verify(expression, expectedValue, actualValue) {
  35. if (expectedValue != actualValue) {
  36. write("Failed: Expected " + expression + " = " + expectedValue + ", got " + actualValue);
  37. return false;
  38. }
  39. write("Success: Expected " + expression + " = " + expectedValue + ", got " + actualValue);
  40. return true;
  41. }
  42. //Tests
  43. // Object getOwnPropertyDescriptor throws TypeError when parameter is not Object. ES5 spec 15.2.3.3.
  44. function Test1() {
  45. var exception;
  46. exception = null;
  47. try {
  48. eval("Object.getOwnPropertyDescriptor(null, 'foo', {})");
  49. } catch (ex) {
  50. exception = ex;
  51. }
  52. if(!Verify("Object getOwnPropertyDescriptor throws TypeError when 1st parameter is null", true, exception instanceof TypeError)) return false;
  53. exception = null;
  54. try {
  55. eval("Object.getOwnPropertyDescriptor(undefined, 'foo', {})");
  56. } catch (ex) {
  57. exception = ex;
  58. }
  59. if(!Verify("Object getOwnPropertyDescriptor throws TypeError when 1st parameter is undefined", true, exception instanceof TypeError)) return false;
  60. return true;
  61. }
  62. // BLUE:227417: Freezing an object with deleted properties
  63. function Test2()
  64. {
  65. var x = {};
  66. x.c = 1;
  67. delete x.c;
  68. Object.freeze(x);
  69. return true;
  70. }
  71. function Test3()
  72. {
  73. if (!Verify("Object getOwnPropertyDescriptor does not throw when 1st parameter is boolean", undefined, Object.getOwnPropertyDescriptor(true, 'foo'))) return false;
  74. if (!Verify("Object getOwnPropertyDescriptor does not throw when 1st parameter is number", undefined, Object.getOwnPropertyDescriptor(123, 'foo'))) return false;
  75. if (!Verify("Object getOwnPropertyDescriptor works fine when 1st parameter is string", 3, Object.getOwnPropertyDescriptor('foo', 'length').value)) return false;
  76. return true;
  77. }
  78. RunAllTests();