• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
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
15try {
16  String.prototype[Symbol.iterator].call(undefined);
17  assert(false);
18} catch (e) {
19  assert(e instanceof TypeError);
20}
21
22try {
23  String.prototype[Symbol.iterator].call(Symbol('foo'));
24  assert(false);
25} catch (e) {
26  assert(e instanceof TypeError);
27}
28
29var str = 'foobar';
30var iterator = str[Symbol.iterator]();
31
32try {
33  iterator.next.call(5);
34  assert (false);
35} catch (e) {
36  assert(e instanceof TypeError);
37}
38
39try {
40  iterator.next.call({});
41  assert(false);
42} catch (e) {
43  assert(e instanceof TypeError);
44}
45
46var str = 'Example string.';
47var expected = ['E', 'x', 'a', 'm', 'p', 'l', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g', '.'];
48
49var iterator = str[Symbol.iterator]();
50var next = iterator.next();
51var idx = 0;
52
53while (!next.done) {
54  assert(next.value === expected[idx++]);
55  assert(next.done === false);
56  next = iterator.next();
57}
58
59assert(next.value === undefined);
60
61var str = '^o��һR��]ŭ媖?᯾豼עW��';
62var expected = ['^', 'o', '��', 'һ', 'R', '��', ']', 'ŭ', '媖', '?', '᯾', '豼', 'ע', 'W', '��'];
63
64iterator = str[Symbol.iterator]();
65next = iterator.next();
66idx = 0;
67
68while (!next.done) {
69  assert(next.value === expected[idx++]);
70  assert(next.done === false);
71  next = iterator.next();
72}
73
74assert(next.value === undefined);
75
76assert(iterator.toString() === '[object String Iterator]');
77assert(iterator[Symbol.toStringTag] === 'String Iterator');
78