dangeabunea/pluralsight-refactoring-solid-java17

OCP Exercise1 Test Case PointInsideAreasCalculatorTests.calculateMethodShouldReturnTrueIfPointInShapes is asserting wrong value!

marcellodesales opened this issue · 0 comments

Problem

Possible Solution

Implemented on my personal fork: marcellodesales#3
NOTE: This proposed solution also expects the hashCode() and equals() to be overridden in each shape...

    public Map<Shape, Boolean> isPointInsideAreas(int x, int y, Set<Shape> shapes) {
        Map<Shape, Boolean> shapeAreas = new HashMap<>(shapes.size());
        for (var s : shapes) {
            shapeAreas.put(s, s.isPointInsideArea(x, y));
        }
        return shapeAreas;
    }
  • A more generic test verification can be done
    public static void assertResults(Map<Shape, Boolean> shapesExpectedResults,  Map<Shape, Boolean> shapesInput) {
        for (Map.Entry<Shape,Boolean> entry : shapesInput.entrySet()) {
            assert shapesExpectedResults.get(entry.getKey()).equals(entry.getValue());
        }
    }
  • The input parameters for the tests can use a Map.of() instead of a list, as we can map the expected results
    Map<Shape, Boolean> shapesExpectedResults = Map.of(
         new Rectangle(0,0,4,4), false,
                new Circle(10,10, 7), true
    );