base.spec.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {
  2. BinaryOperation,
  3. BinaryOperators,
  4. evaluate,
  5. sampleFunction,
  6. } from "../src";
  7. describe("This is a simple test", () => {
  8. test("Check the sampleFunction function", () => {
  9. const actual = sampleFunction("hello");
  10. const expected = "hellohello";
  11. expect(actual).toEqual(expected);
  12. });
  13. });
  14. describe("Simple expression tests", () => {
  15. test("Check literal value", () => {
  16. expect(evaluate({ type: "literal", value: 5 })).toBeCloseTo(5);
  17. });
  18. test("Check addition", () => {
  19. const expr: BinaryOperation = bin("+", 5, 10);
  20. expect(evaluate(expr)).toBeCloseTo(15);
  21. });
  22. test("Check subtraction", () => {
  23. const expr: BinaryOperation = bin("-", 5, 10);
  24. expect(evaluate(expr)).toBeCloseTo(-5);
  25. });
  26. test("Check multiplication", () => {
  27. const expr: BinaryOperation = bin("*", 5, 10);
  28. expect(evaluate(expr)).toBeCloseTo(50);
  29. });
  30. test("Check division", () => {
  31. const expr: BinaryOperation = bin("/", 10, 5);
  32. expect(evaluate(expr)).toBeCloseTo(2);
  33. });
  34. });
  35. function bin(op: BinaryOperators, x: number, y: number): BinaryOperation {
  36. return {
  37. left: {
  38. type: "literal",
  39. value: x,
  40. },
  41. operator: op,
  42. right: {
  43. type: "literal",
  44. value: y,
  45. },
  46. type: "binary",
  47. };
  48. }