for-of-basic.js 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. load("test-common.js");
  2. try {
  3. assertThrowsError(() => {
  4. for (const _ of 123) {}
  5. }, {
  6. error: TypeError,
  7. message: "for..of right-hand side must be iterable"
  8. });
  9. assertThrowsError(() => {
  10. for (const _ of {foo: 1, bar: 2}) {}
  11. }, {
  12. error: TypeError,
  13. message: "for..of right-hand side must be iterable"
  14. });
  15. assertVisitsAll(visit => {
  16. for (const num of [1, 2, 3]) {
  17. visit(num);
  18. }
  19. }, [1, 2, 3]);
  20. assertVisitsAll(visit => {
  21. for (const char of "hello") {
  22. visit(char);
  23. }
  24. }, ["h", "e", "l", "l", "o"]);
  25. assertVisitsAll(visit => {
  26. for (const char of new String("hello")) {
  27. visit(char);
  28. }
  29. }, ["h", "e", "l", "l", "o"]);
  30. var char;
  31. for (char of "abc");
  32. assert(char === "c");
  33. console.log("PASS");
  34. } catch (e) {
  35. console.log("FAIL: " + e);
  36. }