builtin_string_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package goja
  2. import "testing"
  3. func TestSubstr(t *testing.T) {
  4. const SCRIPT = `
  5. assert.sameValue('abc'.substr(0, false), '', 'start: 0, length: false');
  6. assert.sameValue('abc'.substr(1, false), '', 'start: 1, length: false');
  7. assert.sameValue('abc'.substr(2, false), '', 'start: 2, length: false');
  8. assert.sameValue('abc'.substr(3, false), '', 'start: 3, length: false');
  9. assert.sameValue('abc'.substr(0, NaN), '', 'start: 0, length: NaN');
  10. assert.sameValue('abc'.substr(1, NaN), '', 'start: 1, length: NaN');
  11. assert.sameValue('abc'.substr(2, NaN), '', 'start: 2, length: NaN');
  12. assert.sameValue('abc'.substr(3, NaN), '', 'start: 3, length: NaN');
  13. assert.sameValue('abc'.substr(0, ''), '', 'start: 0, length: ""');
  14. assert.sameValue('abc'.substr(1, ''), '', 'start: 1, length: ""');
  15. assert.sameValue('abc'.substr(2, ''), '', 'start: 2, length: ""');
  16. assert.sameValue('abc'.substr(3, ''), '', 'start: 3, length: ""');
  17. assert.sameValue('abc'.substr(0, null), '', 'start: 0, length: null');
  18. assert.sameValue('abc'.substr(1, null), '', 'start: 1, length: null');
  19. assert.sameValue('abc'.substr(2, null), '', 'start: 2, length: null');
  20. assert.sameValue('abc'.substr(3, null), '', 'start: 3, length: null');
  21. assert.sameValue('abc'.substr(0, -1), '', '0, -1');
  22. assert.sameValue('abc'.substr(0, -2), '', '0, -2');
  23. assert.sameValue('abc'.substr(0, -3), '', '0, -3');
  24. assert.sameValue('abc'.substr(0, -4), '', '0, -4');
  25. assert.sameValue('abc'.substr(1, -1), '', '1, -1');
  26. assert.sameValue('abc'.substr(1, -2), '', '1, -2');
  27. assert.sameValue('abc'.substr(1, -3), '', '1, -3');
  28. assert.sameValue('abc'.substr(1, -4), '', '1, -4');
  29. assert.sameValue('abc'.substr(2, -1), '', '2, -1');
  30. assert.sameValue('abc'.substr(2, -2), '', '2, -2');
  31. assert.sameValue('abc'.substr(2, -3), '', '2, -3');
  32. assert.sameValue('abc'.substr(2, -4), '', '2, -4');
  33. assert.sameValue('abc'.substr(3, -1), '', '3, -1');
  34. assert.sameValue('abc'.substr(3, -2), '', '3, -2');
  35. assert.sameValue('abc'.substr(3, -3), '', '3, -3');
  36. assert.sameValue('abc'.substr(3, -4), '', '3, -4');
  37. assert.sameValue('abc'.substr(0, 1), 'a', '0, 1');
  38. assert.sameValue('abc'.substr(0, 2), 'ab', '0, 1');
  39. assert.sameValue('abc'.substr(0, 3), 'abc', '0, 1');
  40. assert.sameValue('abc'.substr(0, 4), 'abc', '0, 1');
  41. assert.sameValue('abc'.substr(1, 1), 'b', '1, 1');
  42. assert.sameValue('abc'.substr(1, 2), 'bc', '1, 1');
  43. assert.sameValue('abc'.substr(1, 3), 'bc', '1, 1');
  44. assert.sameValue('abc'.substr(1, 4), 'bc', '1, 1');
  45. assert.sameValue('abc'.substr(2, 1), 'c', '2, 1');
  46. assert.sameValue('abc'.substr(2, 2), 'c', '2, 1');
  47. assert.sameValue('abc'.substr(2, 3), 'c', '2, 1');
  48. assert.sameValue('abc'.substr(2, 4), 'c', '2, 1');
  49. assert.sameValue('abc'.substr(3, 1), '', '3, 1');
  50. assert.sameValue('abc'.substr(3, 2), '', '3, 1');
  51. assert.sameValue('abc'.substr(3, 3), '', '3, 1');
  52. assert.sameValue('abc'.substr(3, 4), '', '3, 1');
  53. assert.sameValue('abc'.substr(0), 'abc', 'start: 0, length: unspecified');
  54. assert.sameValue('abc'.substr(1), 'bc', 'start: 1, length: unspecified');
  55. assert.sameValue('abc'.substr(2), 'c', 'start: 2, length: unspecified');
  56. assert.sameValue('abc'.substr(3), '', 'start: 3, length: unspecified');
  57. assert.sameValue(
  58. 'abc'.substr(0, undefined), 'abc', 'start: 0, length: undefined'
  59. );
  60. assert.sameValue(
  61. 'abc'.substr(1, undefined), 'bc', 'start: 1, length: undefined'
  62. );
  63. assert.sameValue(
  64. 'abc'.substr(2, undefined), 'c', 'start: 2, length: undefined'
  65. );
  66. assert.sameValue(
  67. 'abc'.substr(3, undefined), '', 'start: 3, length: undefined'
  68. );
  69. assert.sameValue('A—', String.fromCharCode(65, 0x2014));
  70. `
  71. testScript1(TESTLIB+SCRIPT, _undefined, t)
  72. }
  73. func TestStringMatchSym(t *testing.T) {
  74. const SCRIPT = `
  75. function Prefix(p) {
  76. this.p = p;
  77. }
  78. Prefix.prototype[Symbol.match] = function(s) {
  79. return s.substring(0, this.p.length) === this.p;
  80. }
  81. var prefix1 = new Prefix("abc");
  82. var prefix2 = new Prefix("def");
  83. "abc123".match(prefix1) === true && "abc123".match(prefix2) === false &&
  84. "def123".match(prefix1) === false && "def123".match(prefix2) === true;
  85. `
  86. testScript1(SCRIPT, valueTrue, t)
  87. }
  88. func TestGenericSplitter(t *testing.T) {
  89. const SCRIPT = `
  90. function MyRegexp(pattern, flags) {
  91. if (pattern instanceof MyRegexp) {
  92. pattern = pattern.wrapped;
  93. }
  94. this.wrapped = new RegExp(pattern, flags);
  95. }
  96. MyRegexp.prototype.exec = function() {
  97. return this.wrapped.exec.apply(this.wrapped, arguments);
  98. }
  99. Object.defineProperty(MyRegexp.prototype, "lastIndex", {
  100. get: function() {
  101. return this.wrapped.lastIndex;
  102. },
  103. set: function(v) {
  104. this.wrapped.lastIndex = v;
  105. }
  106. });
  107. Object.defineProperty(MyRegexp.prototype, "flags", {
  108. get: function() {
  109. return this.wrapped.flags;
  110. }
  111. });
  112. MyRegexp[Symbol.species] = MyRegexp;
  113. MyRegexp.prototype[Symbol.split] = RegExp.prototype[Symbol.split];
  114. var r = new MyRegexp(/ /);
  115. var res = "a b c".split(r);
  116. res.length === 3 && res[0] === "a" && res[1] === "b" && res[2] === "c";
  117. `
  118. testScript1(SCRIPT, valueTrue, t)
  119. }
  120. func TestStringIterSurrPair(t *testing.T) {
  121. const SCRIPT = `
  122. var lo = '\uD834';
  123. var hi = '\uDF06';
  124. var pair = lo + hi;
  125. var string = 'a' + pair + 'b' + lo + pair + hi + lo;
  126. var iterator = string[Symbol.iterator]();
  127. var result;
  128. result = iterator.next();
  129. if (result.value !== 'a') {
  130. throw new Error("at 0: " + result.value);
  131. }
  132. result = iterator.next();
  133. if (result.value !== pair) {
  134. throw new Error("at 1: " + result.value);
  135. }
  136. `
  137. testScript1(SCRIPT, _undefined, t)
  138. }
  139. func TestValueStringBuilder(t *testing.T) {
  140. t.Run("substringASCII", func(t *testing.T) {
  141. t.Parallel()
  142. var sb valueStringBuilder
  143. str := newStringValue("a\U00010000b")
  144. sb.WriteSubstring(str, 0, 1)
  145. res := sb.String()
  146. if res != asciiString("a") {
  147. t.Fatal(res)
  148. }
  149. })
  150. t.Run("substringASCIIPure", func(t *testing.T) {
  151. t.Parallel()
  152. var sb valueStringBuilder
  153. str := newStringValue("ab")
  154. sb.WriteSubstring(str, 0, 1)
  155. res := sb.String()
  156. if res != asciiString("a") {
  157. t.Fatal(res)
  158. }
  159. })
  160. t.Run("substringUnicode", func(t *testing.T) {
  161. t.Parallel()
  162. var sb valueStringBuilder
  163. str := newStringValue("a\U00010000b")
  164. sb.WriteSubstring(str, 1, 3)
  165. res := sb.String()
  166. if !res.SameAs(unicodeStringFromRunes([]rune{0x10000})) {
  167. t.Fatal(res)
  168. }
  169. })
  170. t.Run("substringASCIIUnicode", func(t *testing.T) {
  171. t.Parallel()
  172. var sb valueStringBuilder
  173. str := newStringValue("a\U00010000b")
  174. sb.WriteSubstring(str, 0, 2)
  175. res := sb.String()
  176. if !res.SameAs(unicodeStringFromRunes([]rune{'a', 0xD800})) {
  177. t.Fatal(res)
  178. }
  179. })
  180. t.Run("substringUnicodeASCII", func(t *testing.T) {
  181. t.Parallel()
  182. var sb valueStringBuilder
  183. str := newStringValue("a\U00010000b")
  184. sb.WriteSubstring(str, 2, 4)
  185. res := sb.String()
  186. if !res.SameAs(unicodeStringFromRunes([]rune{0xDC00, 'b'})) {
  187. t.Fatal(res)
  188. }
  189. })
  190. }