codePointAt.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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(Object.getPrototypeOf({}), "echo", { value: function () { WScript.Echo(this); } });
  6. function AssertEqual(actual, expected, msg) { ((actual === expected ? "Passed! " : "Failed (actual: " + actual + ", expected: " + expected + "). Message: ") + msg).echo(); };
  7. Object.defineProperty(Object.getPrototypeOf({}), "equalTo", { value: function (other, msg) { AssertEqual(this.constructor(this), other, msg); } });
  8. try {
  9. String.prototype.codePointAt.call();
  10. } catch (e) {
  11. e.echo();
  12. }
  13. try {
  14. String.prototype.codePointAt.call(null);
  15. } catch (e) {
  16. e.echo();
  17. }
  18. try {
  19. String.prototype.codePointAt.call(undefined);
  20. } catch (e) {
  21. e.echo();
  22. }
  23. try {
  24. new String.prototype.codePointAt();
  25. } catch (e) {
  26. e.echo();
  27. }
  28. try {
  29. String.fromCodePoint.call();
  30. } catch (e) {
  31. "Fail!".echo();
  32. }
  33. try {
  34. String.fromCodePoint.call(null);
  35. } catch (e) {
  36. "Fail!".echo();
  37. }
  38. try {
  39. String.fromCodePoint.call(undefined);
  40. } catch (e) {
  41. "Fail!".echo();
  42. }
  43. try {
  44. new String.fromCodePoint();
  45. } catch (e) {
  46. e.echo();
  47. }
  48. try {
  49. String.fromCodePoint(1.1);
  50. } catch (ex) {
  51. ex.echo();
  52. }
  53. try {
  54. String.fromCodePoint(100000000);
  55. } catch (ex) {
  56. ex.echo();
  57. }
  58. try {
  59. String.fromCodePoint(-0.0001);
  60. } catch (ex) {
  61. ex.echo();
  62. }
  63. try {
  64. String.fromCodePoint(Infinity);
  65. } catch (ex) {
  66. ex.echo();
  67. }
  68. AssertEqual("".codePointAt(0), undefined, "Size = 0, index 0 test.");
  69. AssertEqual("a".codePointAt(-1), undefined, "Size = 1, index -1 test.");
  70. String.fromCodePoint(97).codePointAt(0).equalTo(97, "Simple character test.");
  71. String.fromCodePoint(65536).codePointAt(0).equalTo(65536, "Surrogate pair treated as a single code point.");
  72. String.fromCodePoint(65536).codePointAt(1).equalTo(56320, "Index pointing to a second surrogate code unit returns the value of that code unit.");
  73. String.fromCodePoint(55296).codePointAt(0).equalTo(55296, "Partial surrogate code unit.");
  74. String.fromCodePoint(55295, 56320).codePointAt(0).equalTo(55295, "First surrogate code unit not in range [D800-DBFF].");
  75. String.fromCodePoint(56320, 56320).codePointAt(0).equalTo(56320, "First surrogate code unit not in range [D800-DBFF].");
  76. String.fromCodePoint(65536).codePointAt(0).equalTo(65536, "First surrogate code unit min value.");
  77. String.fromCodePoint(55296, 56320).codePointAt(0).equalTo(65536, "First surrogate code unit min value.");
  78. String.fromCodePoint(1113088).codePointAt(0).equalTo(1113088, "First surrogate code unit max value.");
  79. String.fromCodePoint(56319, 56320).codePointAt(0).equalTo(1113088, "First surrogate code unit max value.");
  80. String.fromCodePoint(55296, 56319).codePointAt(0).equalTo(55296, "Second surrogate code unit not in range [DC00-DFFF].");
  81. String.fromCodePoint(55296, 57344).codePointAt(0).equalTo(55296, "Second surrogate code unit not in range [DC00-DFFF].");
  82. String.fromCodePoint(65536).codePointAt(0).equalTo(65536, "Second surrogate code unit min value.");
  83. String.fromCodePoint(55296, 56320).codePointAt(0).equalTo(65536, "Second surrogate code unit min value.");
  84. String.fromCodePoint(66559).codePointAt(0).equalTo(66559, "Second surrogate code unit max value.");
  85. String.fromCodePoint(55296, 57343).codePointAt(0).equalTo(66559, "Second surrogate code unit max value.");
  86. String.prototype.codePointAt.call(5, 0).equalTo(53, "Calling on a number object instead of string object.");
  87. if(String.fromCodePoint.length !== 1) {
  88. WScript.Echo("String.fromCodePoint length should be 1, actual: " + String.fromCodePoint.length);
  89. }