OCP Exercise1 Test Case PointInsideAreasCalculatorTests.calculateMethodShouldReturnTrueIfPointInShapes is asserting wrong value!
marcellodesales opened this issue · 0 comments
marcellodesales commented
Problem
- The test case at https://github.com/dangeabunea/pluralsight-refactoring-solid-java17/blob/main/2-ocp/exercises/exercise1/before/src/test/java/PointInsideAreasCalculatorTests.java#L53-L62 is failing because of the wrong assertion.
- Point (6, 6) is outside of the rectangle (4, 4)... https://github.com/dangeabunea/pluralsight-refactoring-solid-java17/blob/main/2-ocp/exercises/exercise1/before/src/test/java/PointInsideAreasCalculatorTests.java#L56
Possible Solution
Implemented on my personal fork: marcellodesales#3
NOTE: This proposed solution also expects thehashCode()
andequals()
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
);