uchar_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.i18n.uCharTest');
  15. goog.setTestOnly('goog.i18n.uCharTest');
  16. goog.require('goog.i18n.uChar');
  17. goog.require('goog.testing.jsunit');
  18. function testToHexString() {
  19. var result = goog.i18n.uChar.toHexString('\uD869\uDED6');
  20. assertEquals('U+2A6D6', result);
  21. }
  22. function testPadString() {
  23. var result = goog.i18n.uChar.padString_('abc', 4, '0');
  24. assertEquals('0abc', result);
  25. }
  26. function testToCharCode() {
  27. var result = goog.i18n.uChar.toCharCode('\uD869\uDED6');
  28. assertEquals(0x2A6D6, result);
  29. }
  30. function testcodePointAt() {
  31. // Basic cases.
  32. assertEquals(0x006C, goog.i18n.uChar.getCodePointAround('Hello!', 2));
  33. assertEquals(
  34. 0x2708 /* Airplane symbol (non-ASCII) */,
  35. goog.i18n.uChar.getCodePointAround('Hello\u2708', 5));
  36. // Supplementary characters.
  37. assertEquals(0x2A6D6, goog.i18n.uChar.getCodePointAround('\uD869\uDED6', 0));
  38. assertEquals(-0x2A6D6, goog.i18n.uChar.getCodePointAround('\uD869\uDED6', 1));
  39. assertEquals(
  40. 0xD869, goog.i18n.uChar.getCodePointAround(
  41. '\uD869' +
  42. 'w',
  43. 0));
  44. assertEquals(
  45. 0xDED6, goog.i18n.uChar.getCodePointAround(
  46. '\uD869' +
  47. 'w' +
  48. '\uDED6',
  49. 2));
  50. }
  51. function testBuildSupplementaryCodePoint() {
  52. var result = goog.i18n.uChar.buildSupplementaryCodePoint(0xD869, 0xDED6);
  53. assertEquals(0x2A6D6, result);
  54. assertNull(goog.i18n.uChar.buildSupplementaryCodePoint(0xDED6, 0xD869));
  55. assertNull(goog.i18n.uChar.buildSupplementaryCodePoint(0xD869, 0xAC00));
  56. }
  57. function testCharCount() {
  58. assertEquals(2, goog.i18n.uChar.charCount(0x2A6D6));
  59. assertEquals(1, goog.i18n.uChar.charCount(0xAC00));
  60. }
  61. function testIsSupplementaryCodePoint() {
  62. assertTrue(goog.i18n.uChar.isSupplementaryCodePoint(0x2A6D6));
  63. assertFalse(goog.i18n.uChar.isSupplementaryCodePoint(0xAC00));
  64. }
  65. function testIsLeadSurrogateCodepoint() {
  66. assertTrue(goog.i18n.uChar.isLeadSurrogateCodePoint(0xD869));
  67. assertFalse(goog.i18n.uChar.isLeadSurrogateCodePoint(0xDED6));
  68. assertFalse(goog.i18n.uChar.isLeadSurrogateCodePoint(0xAC00));
  69. }
  70. function testIsTrailSurrogateCodePoint() {
  71. assertTrue(goog.i18n.uChar.isTrailSurrogateCodePoint(0xDED6));
  72. assertFalse(goog.i18n.uChar.isTrailSurrogateCodePoint(0xD869));
  73. assertFalse(goog.i18n.uChar.isTrailSurrogateCodePoint(0xAC00));
  74. }
  75. function testFromCharCode() {
  76. var result = goog.i18n.uChar.fromCharCode(0x2A6D6);
  77. assertEquals('\uD869\uDED6', result);
  78. }
  79. function testFromCharCode_invalidValues() {
  80. var result = goog.i18n.uChar.fromCharCode(-1);
  81. assertEquals(null, result);
  82. result =
  83. goog.i18n.uChar.fromCharCode(goog.i18n.uChar.CODE_POINT_MAX_VALUE_ + 1);
  84. assertEquals(null, result);
  85. result = goog.i18n.uChar.fromCharCode(null);
  86. assertEquals(null, result);
  87. result = goog.i18n.uChar.fromCharCode(undefined);
  88. assertEquals(null, result);
  89. result = goog.i18n.uChar.fromCharCode(NaN);
  90. assertEquals(null, result);
  91. }