Browse Source

More operations and tests.

Frederic G. MARAND 5 years ago
parent
commit
3fd70476d5
4 changed files with 86 additions and 3 deletions
  1. 69 1
      __tests__/base.spec.ts
  2. 1 0
      package.json
  3. 16 0
      src/core/functions.ts
  4. 0 2
      src/utils/checks.ts

+ 69 - 1
__tests__/base.spec.ts

@@ -1,4 +1,8 @@
-import {evaluate, sampleFunction} from "../src";
+import {
+  evaluate,
+  Expression,
+  sampleFunction,
+} from "../src";
 
 describe("This is a simple test", () => {
   test("Check the sampleFunction function", () => {
@@ -12,4 +16,68 @@ describe("Simple expression tests", () => {
   test("Check literal value", () => {
     expect(evaluate({ type: "literal", value: 5 })).toBeCloseTo(5);
   });
+
+  test("Check addition", () => {
+    const expr: Expression = {
+      left: {
+        type: "literal",
+        value: 5,
+      },
+      operator: "+",
+      right: {
+        type: "literal",
+        value: 10,
+      },
+      type: "binary",
+    };
+    expect(evaluate(expr)).toBeCloseTo(15);
+  });
+
+  test("Check subtraction", () => {
+    const expr: Expression = {
+      left: {
+        type: "literal",
+        value: 5,
+      },
+      operator: "-",
+      right: {
+        type: "literal",
+        value: 10,
+      },
+      type: "binary",
+    };
+    expect(evaluate(expr)).toBeCloseTo(-5);
+  });
+
+  test("Check multiplication", () => {
+    const expr: Expression = {
+      left: {
+        type: "literal",
+        value: 5,
+      },
+      operator: "*",
+      right: {
+        type: "literal",
+        value: 10,
+      },
+      type: "binary",
+    };
+    expect(evaluate(expr)).toBeCloseTo(50);
+  });
+
+  test("Check division", () => {
+    const expr: Expression = {
+      left: {
+        type: "literal",
+        value: 10,
+      },
+      operator: "/",
+      right: {
+        type: "literal",
+        value: 5,
+      },
+      type: "binary",
+    };
+    expect(evaluate(expr)).toBeCloseTo(2);
+  });
 });

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
   "repository": "https://medium.com/@mtiller/debugging-with-typescript-jest-ts-jest-and-visual-studio-code-ef9ca8644132",
   "scripts": {
     "compile": "tsc",
+    "lint": "tslint -p tsconfig.json",
     "test": "jest"
   },
   "version": "1.0.0"

+ 16 - 0
src/core/functions.ts

@@ -7,6 +7,22 @@ export function evaluate(expr: Expression): number {
       return expr.value;
     }
 
+    case "binary": {
+      switch (expr.operator) {
+        case "+":
+          return evaluate(expr.left) + evaluate(expr.right);
+        case "-":
+          return evaluate(expr.left) - evaluate(expr.right);
+        case "*":
+          return evaluate(expr.left) * evaluate(expr.right);
+        case "/":
+          return evaluate(expr.left) / evaluate(expr.right);
+        /* istanbul ignore next */
+        default: {
+          return assertNever("Unexpected binary operator", expr.operator);
+        }
+      }
+    }
     /* istanbul ignore next */
     default: {
       return assertNever("Unexpected expression type", expr);

+ 0 - 2
src/utils/checks.ts

@@ -1,6 +1,4 @@
 /* istanbul ignore next */
-import {Error} from "tslint/lib/error";
-
 export function assertNever(msg: string, value: never): never {
   throw new Error(msg + ": " + JSON.stringify(value));
 }