for-in-basic.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. const expected = [
  48. ["1", "3", []],
  49. ["s", undefined, []],
  50. ["l", "n", ["g", "N", "a", "m", "e"]],
  51. ];
  52. let counter = 0;
  53. for (let [a, , b, ...c] in { 123: 1, sm: 2, longName: 3 }) {
  54. expect(a).toBe(expected[counter][0]);
  55. expect(b).toBe(expected[counter][1]);
  56. expect(c).toEqual(expected[counter][2]);
  57. counter++;
  58. }
  59. expect(counter).toBe(3);
  60. });
  61. test("allow member expression as variable", () => {
  62. const f = {};
  63. for (f.a in "abc");
  64. expect(f.a).toBe("2");
  65. });