switch-as-statement.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. describe("switch statement is a valid statement and gets executed", () => {
  2. test("switch statement in a block", () => {
  3. let hit = false;
  4. {
  5. switch (true) {
  6. case true:
  7. hit = true;
  8. }
  9. expect(hit).toBeTrue();
  10. }
  11. });
  12. test("switch statement in an if statement when true", () => {
  13. let hit = false;
  14. var a = true;
  15. if (a)
  16. switch (true) {
  17. case true:
  18. hit = true;
  19. }
  20. else
  21. switch (true) {
  22. case true:
  23. expect().fail();
  24. }
  25. expect(hit).toBeTrue();
  26. });
  27. test("switch statement in an if statement when false", () => {
  28. let hit = false;
  29. var a = false;
  30. if (a)
  31. switch (a) {
  32. default:
  33. expect().fail();
  34. }
  35. else
  36. switch (a) {
  37. default:
  38. hit = true;
  39. }
  40. expect(hit).toBeTrue();
  41. });
  42. test("switch statement in an while statement", () => {
  43. var a = 0;
  44. var loops = 0;
  45. while (a < 1 && loops++ < 5)
  46. switch (a) {
  47. case 0:
  48. a = 1;
  49. }
  50. expect(a).toBe(1);
  51. });
  52. test("switch statement in an for statement", () => {
  53. var loops = 0;
  54. for (let a = 0; a < 1 && loops++ < 5; )
  55. switch (a) {
  56. case 0:
  57. a = 1;
  58. }
  59. expect(loops).toBe(1);
  60. });
  61. });