for-in-basic.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. test("iterate through empty string", () => {
  2. const a = [];
  3. for (const property in "") {
  4. a.push(property);
  5. }
  6. expect(a).toEqual([]);
  7. });
  8. test("iterate through number", () => {
  9. const a = [];
  10. for (const property in 123) {
  11. a.push(property);
  12. }
  13. expect(a).toEqual([]);
  14. });
  15. test("iterate through empty object", () => {
  16. const a = [];
  17. for (const property in {}) {
  18. a.push(property);
  19. }
  20. expect(a).toEqual([]);
  21. });
  22. test("iterate through string", () => {
  23. const a = [];
  24. for (const property in "hello") {
  25. a.push(property);
  26. }
  27. expect(a).toEqual(["0", "1", "2", "3", "4"]);
  28. });
  29. test("iterate through object", () => {
  30. const a = [];
  31. for (const property in { a: 1, b: 2, c: 2 }) {
  32. a.push(property);
  33. }
  34. expect(a).toEqual(["a", "b", "c"]);
  35. });
  36. test("iterate through undefined", () => {
  37. for (const property in undefined) {
  38. expect.fail();
  39. }
  40. });
  41. test("use already-declared variable", () => {
  42. var property;
  43. for (property in "abc");
  44. expect(property).toBe("2");
  45. });
  46. test("allow binding patterns", () => {
  47. expect(`for (let [a, b] in foo) {}`).toEval();
  48. });