12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import {
- BinaryOperation,
- BinaryOperators,
- evaluate,
- } from "../src";
- describe("Simple expression tests", () => {
- test("Check literal value", () => {
- expect(evaluate({ type: "literal", value: 5 })).toBeCloseTo(5);
- });
- test("Check addition", () => {
- const expr: BinaryOperation = bin("+", 5, 10);
- expect(evaluate(expr)).toBeCloseTo(15);
- });
- test("Check subtraction", () => {
- const expr: BinaryOperation = bin("-", 5, 10);
- expect(evaluate(expr)).toBeCloseTo(-5);
- });
- test("Check multiplication", () => {
- const expr: BinaryOperation = bin("*", 5, 10);
- expect(evaluate(expr)).toBeCloseTo(50);
- });
- test("Check division", () => {
- const expr: BinaryOperation = bin("/", 10, 5);
- expect(evaluate(expr)).toBeCloseTo(2);
- });
- });
- function bin(op: BinaryOperators, x: number, y: number): BinaryOperation {
- return {
- left: {
- type: "literal",
- value: x,
- },
- operator: op,
- right: {
- type: "literal",
- value: y,
- },
- type: "binary",
- };
- }
|