From b867af24b86498f69c73661a6fd3a92cff481526 Mon Sep 17 00:00:00 2001
From: nilstes <nils.tesdal@gmail.com>
Date: Thu, 13 Sep 2018 19:16:59 +0200
Subject: [PATCH] ferdig med eksempel?

---
 dao/calculator.js      | 36 +++++++++++++++++++++++++++++++
 dao/calculator.test.js | 36 +++++++++++++++++++++++++++++++
 dao/dum.test.js        | 49 ++++++++++++++++++++++++++++++++++++++++++
 dao/sum.test.js        | 20 ++++++++---------
 server.js              | 12 +++++------
 5 files changed, 136 insertions(+), 17 deletions(-)
 create mode 100644 dao/calculator.js
 create mode 100644 dao/calculator.test.js
 create mode 100644 dao/dum.test.js

diff --git a/dao/calculator.js b/dao/calculator.js
new file mode 100644
index 0000000..d30fbed
--- /dev/null
+++ b/dao/calculator.js
@@ -0,0 +1,36 @@
+module.exports = class Calculator {
+  constructor() {}
+
+  calculate(expression) {
+    let pos = expression.indexOf("+");
+    if (pos >= 0) {
+      return (
+        this.calculate(expression.substr(0, pos)) +
+        this.calculate(expression.substr(pos + 1))
+      );
+    } else {
+      pos = expression.indexOf("-");
+      if (pos >= 0) {
+        return (
+          this.calculate(expression.substr(0, pos)) -
+          this.calculate(expression.substr(pos + 1))
+        );
+      } else {
+        // Remove ALL whitespaces
+        expression = expression.replace(/\s+/g, "");
+        if (expression === "") {
+          return 0;
+        }
+
+        let num = Number(expression);
+        if (!Number.isInteger(num)) {
+          console.log("'" + expression + "' is not an integer");
+          throw new Error("'" + expression + "' is not an integer");
+        } else {
+          return num;
+        }
+      }
+    }
+    return 0;
+  }
+};
diff --git a/dao/calculator.test.js b/dao/calculator.test.js
new file mode 100644
index 0000000..35942bd
--- /dev/null
+++ b/dao/calculator.test.js
@@ -0,0 +1,36 @@
+const Calculator = require("./calculator");
+let calc = new Calculator();
+
+beforeEach(() => {
+  console.log("calculator.test: beforeEach");
+});
+
+afterEach(() => {
+  console.log("calculator.test: afterEach");
+});
+
+beforeAll(() => {
+  console.log("calculator.test: beforeAll");
+});
+
+afterAll(() => {
+  console.log("calculator.test: afterAll");
+});
+
+test("test plus and minus with any number of arguments", () => {
+  expect(calc.calculate("")).toBe(0);
+  expect(calc.calculate("2")).toBe(2);
+  expect(calc.calculate("2+2")).toBe(4);
+  expect(calc.calculate("2+4+3-3+5")).toBe(11);
+});
+
+test("test that whitespace is allowed", () => {
+  expect(calc.calculate(" \t\n\r2 +\n3")).toBe(5);
+});
+
+test("test that only digits and plus and minus and whitespace is allowed", () => {
+  let illegal = ["1.2", "1,2", "1/2", "1*2", "1 plus 2"];
+  for (i in illegal) {
+    expect(() => calc.calculate(illegal[i])).toThrow();
+  }
+});
diff --git a/dao/dum.test.js b/dao/dum.test.js
new file mode 100644
index 0000000..4a8056c
--- /dev/null
+++ b/dao/dum.test.js
@@ -0,0 +1,49 @@
+beforeEach(() => {
+  console.log("dum.test: beforeEach");
+});
+
+afterEach(() => {
+  console.log("dum.test: afterEach");
+});
+
+beforeAll(() => {
+  console.log("dum.test: beforeAll");
+});
+
+afterAll(() => {
+  console.log("dum.test: afterAll");
+});
+
+test("test at 1 er 1", () => {
+  console.log("dum.test: test 1");
+  expect(1).toBe(1);
+});
+
+test("test at 2 er 2", () => {
+  console.log("dum.test: test 2");
+  expect(2).toBe(2);
+});
+
+test("test alle expects", () => {
+  // Kjør kode
+  // før vi verifiserer resultatet
+
+  expect(2 + 2).toBe(4);
+  expect(2 + 2).toEqual(4);
+  expect(1 + 1).not.toBe(0);
+  expect("data").toEqual("data");
+  expect(true).toBeTruthy();
+  expect(false).not.toBeTruthy();
+  expect("1").toBeDefined();
+  expect("1").not.toBeUndefined();
+  expect(2).toBeLessThan(5);
+  expect("Christoph").toMatch(/stop/); // Regular expression
+});
+
+someCode = () => {
+  throw Error("Feil");
+};
+
+test("test exception", () => {
+  expect(someCode).toThrow();
+});
diff --git a/dao/sum.test.js b/dao/sum.test.js
index ea88684..91d5a7b 100644
--- a/dao/sum.test.js
+++ b/dao/sum.test.js
@@ -1,24 +1,22 @@
-const sum = require('./sum');
+const sum = require("./sum");
 
-
-/*
 beforeEach(() => {
-  initializeCityDatabase();
+  console.log("sum.test: beforeEach");
 });
 
 afterEach(() => {
-  clearCityDatabase();
+  console.log("sum.test: afterEach");
 });
 
-test('city database has Vienna', () => {
-  expect(isCity('Vienna')).toBeTruthy();
+beforeAll(() => {
+  console.log("sum.test: beforeAll");
 });
 
-test('city database has San Juan', () => {
-  expect(isCity('San Juan')).toBeTruthy();
-}); */
+afterAll(() => {
+  console.log("sum.test: afterAll");
+});
 
 test("adds 1 + 2 to equal 3", () => {
   console.log("Running simple test");
   expect(sum(1, 2)).toBe(3);
-});
\ No newline at end of file
+});
diff --git a/server.js b/server.js
index 22ebb02..3e28234 100644
--- a/server.js
+++ b/server.js
@@ -19,25 +19,25 @@ let personDao = new PersonDao(pool);
 
 app.get("/person", (req, res) => {
   console.log("/person: fikk request fra klient");
-  personDao.getAll((status, json) => {
+  personDao.getAll((status, data) => {
     res.status(status);
-    res.json(json);
+    res.json(data);
   });
 });
 
 app.get("/person/:personId", (req, res) => {
   console.log("/person/:personId: fikk request fra klient");
-  personDao.getOne(req.params.personId, (status, json) => {
+  personDao.getOne(req.params.personId, (status, data) => {
     res.status(status);
-    res.json(json);
+    res.json(data);
   });
 });
 
 app.post("/person", (req, res) => {
   console.log("Fikk POST-request fra klienten");
-  personDao.createOne(req.body, (status, json) => {
+  personDao.createOne(req.body, (status, data) => {
     res.status(status);
-    res.json(json);
+    res.json(data);
   });
 });
 
-- 
GitLab