Browse Source

Refactor tests.

Frederic G. MARAND 5 years ago
parent
commit
0935d227ad
1 changed files with 21 additions and 49 deletions
  1. 21 49
      __tests__/base.spec.ts

+ 21 - 49
__tests__/base.spec.ts

@@ -1,6 +1,7 @@
 import {
+  BinaryOperation,
+  BinaryOperators,
   evaluate,
-  Expression,
   sampleFunction,
 } from "../src";
 
@@ -18,66 +19,37 @@ describe("Simple expression tests", () => {
   });
 
   test("Check addition", () => {
-    const expr: Expression = {
-      left: {
-        type: "literal",
-        value: 5,
-      },
-      operator: "+",
-      right: {
-        type: "literal",
-        value: 10,
-      },
-      type: "binary",
-    };
+    const expr: BinaryOperation = bin("+", 5, 10);
     expect(evaluate(expr)).toBeCloseTo(15);
   });
 
   test("Check subtraction", () => {
-    const expr: Expression = {
-      left: {
-        type: "literal",
-        value: 5,
-      },
-      operator: "-",
-      right: {
-        type: "literal",
-        value: 10,
-      },
-      type: "binary",
-    };
+    const expr: BinaryOperation = bin("-", 5, 10);
     expect(evaluate(expr)).toBeCloseTo(-5);
   });
 
   test("Check multiplication", () => {
-    const expr: Expression = {
-      left: {
-        type: "literal",
-        value: 5,
-      },
-      operator: "*",
-      right: {
-        type: "literal",
-        value: 10,
-      },
-      type: "binary",
-    };
+    const expr: BinaryOperation = bin("*", 5, 10);
     expect(evaluate(expr)).toBeCloseTo(50);
   });
 
   test("Check division", () => {
-    const expr: Expression = {
-      left: {
-        type: "literal",
-        value: 10,
-      },
-      operator: "/",
-      right: {
-        type: "literal",
-        value: 5,
-      },
-      type: "binary",
-    };
+    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",
+  };
+}