Skip to content
Snippets Groups Projects
Commit b867af24 authored by nilstes's avatar nilstes
Browse files

ferdig med eksempel?

parent c2cc6d8e
No related branches found
No related tags found
No related merge requests found
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;
}
};
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();
}
});
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();
});
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
});
......@@ -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);
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment