Quellcode durchsuchen

implement subtract, multiply and divide operations

Harry de Boer vor 5 Jahren
Ursprung
Commit
b3f62f526e

+ 15 - 0
backend/src/main/java/nl/quintor/hdboer/aegoncalc/controllers/SimpleCalculatorController.java

@@ -22,4 +22,19 @@ public class SimpleCalculatorController {
         return calculator.add(val, addend);
     }
 
+    @GetMapping("/numbers/{val}/subtract/{subtrahend}")
+    Double subtract(@PathVariable int val, @PathVariable int subtrahend) {
+        return calculator.subtract(val, subtrahend);
+    }
+
+    @GetMapping("/numbers/{val}/multiply/{multiplier}")
+    Double multiply(@PathVariable int val, @PathVariable int multiplier) {
+        return calculator.multiply(val, multiplier);
+    }
+
+    @GetMapping("/numbers/{val}/divide/{divisor}")
+    Double divide(@PathVariable int val, @PathVariable int divisor) {
+        return calculator.divide(val, divisor);
+    }
+
 }

+ 30 - 0
backend/src/test/java/nl/quintor/hdboer/aegoncalc/controllers/SimpleCalculatorControllerIT.java

@@ -37,6 +37,36 @@ class SimpleCalculatorControllerIT {
         );
     }
 
+    @Test
+    public void testSubtract() {
+        ResponseEntity<Double> response = get("/numbers/1/subtract/15");
+
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()),
+                () -> Assertions.assertEquals(-14.0, response.getBody())
+        );
+    }
+
+    @Test
+    public void testMultiply() {
+        ResponseEntity<Double> response = get("/numbers/3/multiply/15");
+
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()),
+                () -> Assertions.assertEquals(45.0, response.getBody())
+        );
+    }
+
+    @Test
+    public void testDivide() {
+        ResponseEntity<Double> response = get("/numbers/3/divide/2");
+
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()),
+                () -> Assertions.assertEquals(1.5, response.getBody())
+        );
+    }
+
     private ResponseEntity<Double> get(String path) {
         return restTemplate.getForEntity("http://localhost:" + port + path, Double.class);
     }

+ 27 - 19
frontend/e2e/src/app.e2e-spec.ts

@@ -21,28 +21,36 @@ describe('workspace-project App', () => {
     expect(await page.getResult()).toEqual('79');
   });
 
-  // it('should subtract', async () => {
-  //   await page.navigateTo();
-  //   await page.setValue1('37')
-  //   await page.setValue2('42')
-  //   await page.clickSubtract();
-  //   expect(await page.getResult()).toEqual('-5');
-  // });
-  //
-  // it('should multiply', async () => {
-  //   await page.navigateTo();
-  //   await page.setValue1('37')
-  //   await page.setValue2('42')
-  //   await page.clickMultiply();
-  //   expect(await page.getResult()).toEqual('1554');
-  // });
-  //
-  // it('should divide', async () => {
+  it('should subtract', async () => {
+    await page.navigateTo();
+    await page.setValue1('37')
+    await page.setValue2('42')
+    await page.clickSubtract();
+    expect(await page.getResult()).toEqual('-5');
+  });
+
+  it('should multiply', async () => {
+    await page.navigateTo();
+    await page.setValue1('37')
+    await page.setValue2('42')
+    await page.clickMultiply();
+    expect(await page.getResult()).toEqual('1554');
+  });
+
+  it('should divide', async () => {
+    await page.navigateTo();
+    await page.setValue1('1')
+    await page.setValue2('8')
+    await page.clickDivide();
+    expect(await page.getResult()).toEqual('0.125');
+  });
+
+  // it('should display error when dividing by zero', async () => {
   //   await page.navigateTo();
   //   await page.setValue1('1')
-  //   await page.setValue2('8')
+  //   await page.setValue2('0')
   //   await page.clickDivide();
-  //   expect(await page.getResult()).toEqual('1.125');
+  //   expect(await page.getResult()).toEqual('error: cannot divide by zero');
   // });
 
   afterEach(async () => {