base.spec.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. evaluate,
  3. Expression,
  4. sampleFunction,
  5. } from "../src";
  6. describe("This is a simple test", () => {
  7. test("Check the sampleFunction function", () => {
  8. const actual = sampleFunction("hello");
  9. const expected = "hellohello";
  10. expect(actual).toEqual(expected);
  11. });
  12. });
  13. describe("Simple expression tests", () => {
  14. test("Check literal value", () => {
  15. expect(evaluate({ type: "literal", value: 5 })).toBeCloseTo(5);
  16. });
  17. test("Check addition", () => {
  18. const expr: Expression = {
  19. left: {
  20. type: "literal",
  21. value: 5,
  22. },
  23. operator: "+",
  24. right: {
  25. type: "literal",
  26. value: 10,
  27. },
  28. type: "binary",
  29. };
  30. expect(evaluate(expr)).toBeCloseTo(15);
  31. });
  32. test("Check subtraction", () => {
  33. const expr: Expression = {
  34. left: {
  35. type: "literal",
  36. value: 5,
  37. },
  38. operator: "-",
  39. right: {
  40. type: "literal",
  41. value: 10,
  42. },
  43. type: "binary",
  44. };
  45. expect(evaluate(expr)).toBeCloseTo(-5);
  46. });
  47. test("Check multiplication", () => {
  48. const expr: Expression = {
  49. left: {
  50. type: "literal",
  51. value: 5,
  52. },
  53. operator: "*",
  54. right: {
  55. type: "literal",
  56. value: 10,
  57. },
  58. type: "binary",
  59. };
  60. expect(evaluate(expr)).toBeCloseTo(50);
  61. });
  62. test("Check division", () => {
  63. const expr: Expression = {
  64. left: {
  65. type: "literal",
  66. value: 10,
  67. },
  68. operator: "/",
  69. right: {
  70. type: "literal",
  71. value: 5,
  72. },
  73. type: "binary",
  74. };
  75. expect(evaluate(expr)).toBeCloseTo(2);
  76. });
  77. });