From 73e9d915e359c73150204ba8e29c19a882236bf6 Mon Sep 17 00:00:00 2001 From: Will Molloy Date: Fri, 17 Apr 2020 07:47:30 +1200 Subject: [PATCH] fix AbstractSlot#equals method which incorrectly compared the class (#45) * fix AbstractSlot#equals method which incorrectly compared the class * improve coverage on AbstractSlotTest --- .../mutters/core/AbstractSlot.java | 2 +- .../mutters/core/AbstractSlotTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mutters-core/src/main/java/com/rabidgremlin/mutters/core/AbstractSlot.java b/mutters-core/src/main/java/com/rabidgremlin/mutters/core/AbstractSlot.java index eb66e1c..6cbf827 100644 --- a/mutters-core/src/main/java/com/rabidgremlin/mutters/core/AbstractSlot.java +++ b/mutters-core/src/main/java/com/rabidgremlin/mutters/core/AbstractSlot.java @@ -34,7 +34,7 @@ public final boolean equals(Object o) { return true; } - if (o == null || getClass() != o.getClass()) + if (!(o instanceof Slot)) { return false; } diff --git a/mutters-core/src/test/java/com/rabidgremlin/mutters/core/AbstractSlotTest.java b/mutters-core/src/test/java/com/rabidgremlin/mutters/core/AbstractSlotTest.java index 7b992db..9a285eb 100644 --- a/mutters-core/src/test/java/com/rabidgremlin/mutters/core/AbstractSlotTest.java +++ b/mutters-core/src/test/java/com/rabidgremlin/mutters/core/AbstractSlotTest.java @@ -30,6 +30,20 @@ public Optional> match(String token, Context context) } } + private static final class TestSlot2 extends AbstractSlot + { + protected TestSlot2(String name) + { + super(name); + } + + @Override + public Optional> match(String token, Context context) + { + return Optional.empty(); + } + } + @Test public void testGetName() { @@ -50,8 +64,12 @@ public void testEquals() Slot slot = new TestSlot("my-slot"); Slot slotWithSameName = new TestSlot("my-slot"); Slot slotWithDifferentName = new TestSlot("my-slot-2"); + Slot slotWithSameNameDifferentClass = new TestSlot2("my-slot"); assertEquals(slot, slotWithSameName); assertNotEquals(slot, slotWithDifferentName); + assertEquals(slot, slotWithSameNameDifferentClass); + assertEquals(slot, slot); + assertNotEquals(slot, 1); } @Test