Skip to content

Commit

Permalink
do not consider null in equality checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed May 17, 2022
1 parent bd820bb commit fbc3215
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 44 deletions.
7 changes: 3 additions & 4 deletions pitest-entry/src/main/java/org/pitest/sequence/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import java.util.IdentityHashMap;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class Context {
public final class Context {

private final boolean debug;
private final Map<Slot,Object> slots;
Expand Down Expand Up @@ -46,11 +45,11 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (!(o instanceof Context)) {
return false;
}
Context context = (Context) o;
return debug == context.debug && Objects.equals(slots, context.slots);
return slots.equals(context.slots);
}

@Override
Expand Down
46 changes: 6 additions & 40 deletions pitest-entry/src/main/java/org/pitest/sequence/SequenceQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -192,18 +191,15 @@ private Set<StateContext<T>> run(List<T> sequence, Context initialContext) {


private static <T> void addState(Set<StateContext<T>> set, StateContext<T> state) {
if (state == null) {
return;
}

if (state.state == null) {
return;
}

if (state.state instanceof Split) {
final Split<T> split = (Split<T>) state.state;
addState(set, new StateContext<T>(split.out1, state.context));
addState(set, new StateContext<T>(split.out2, state.context));
if (split.out1 != null) {
addState(set, new StateContext<T>(split.out1, state.context));
}
if (split.out2 != null) {
addState(set, new StateContext<T>(split.out2, state.context));
}
} else {
set.add(state);
}
Expand Down Expand Up @@ -261,33 +257,3 @@ class Split<T> implements State<T> {
enum EndMatch implements State {
MATCH
}

/**
* Pair class to hold state and context.
*/
class StateContext<T> {

StateContext(State<T> state, Context context) {
this.state = state;
this.context = context;
}
final State<T> state;
final Context context;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StateContext<?> that = (StateContext<?>) o;
return Objects.equals(state, that.state) && Objects.equals(context, that.context);
}

@Override
public int hashCode() {
return Objects.hash(state, context);
}
}
33 changes: 33 additions & 0 deletions pitest-entry/src/main/java/org/pitest/sequence/StateContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.pitest.sequence;

import java.util.Objects;

/**
* Pair class to hold state and context.
*/
final class StateContext<T> {

StateContext(State<T> state, Context context) {
this.state = state;
this.context = context;
}
final State<T> state;
final Context context;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof StateContext)) {
return false;
}
StateContext<?> that = (StateContext<?>) o;
return state.equals(that.state) && context.equals(that.context);
}

@Override
public int hashCode() {
return Objects.hash(state, context);
}
}
16 changes: 16 additions & 0 deletions pitest-entry/src/test/java/org/pitest/sequence/ContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.pitest.sequence;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

public class ContextTest {

@Test
public void obeysHashCodeEqualsContract() {
EqualsVerifier.forClass(Context.class)
.withNonnullFields("slots")
.withIgnoredFields("debug")
.verify();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.pitest.sequence;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

public class StateContextTest {
@Test
public void obeysHashCodeEqualsContract() {
EqualsVerifier.forClass(StateContext.class)
.withNonnullFields("state", "context")
.verify();
}
}

0 comments on commit fbc3215

Please sign in to comment.