diff --git a/dao/calculator.js b/dao/calculator.js new file mode 100644 index 0000000000000000000000000000000000000000..d30fbedfd42978e1e6cc73225ef7bc2f92e4d81f --- /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 0000000000000000000000000000000000000000..35942bdff850283bde390b5f155c37eb08bc5b39 --- /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 0000000000000000000000000000000000000000..4a8056cd90baaff44ce72fcf71dd6c9e9de5466a --- /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 ea88684e08b08a90b906bbe8eb2f1ca68b211887..91d5a7b2bb5d703d1b5b713dbbece06f7ab7790c 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 22ebb020970484f57229b6f73c17ec3ee0c4fd72..3e28234bd1eb9a4923da3be7f344f4ecb7ec26e4 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); }); });