Skip to content

Commit

Permalink
Fix quality flaw: complete coverage of MethodYield equals method
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops committed Dec 2, 2016
1 parent 96a84d3 commit e3ce850
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public boolean equals(Object obj) {
MethodYield other = (MethodYield) obj;
if (!Arrays.equals(parametersConstraints, other.parametersConstraints)
|| exception != other.exception
|| resultIndex != other.resultIndex) {
|| resultIndex != other.resultIndex
|| varArgs != other.varArgs) {
return false;
}
if (resultConstraint != null) {
Expand Down
49 changes: 49 additions & 0 deletions java-frontend/src/test/java/org/sonar/java/se/MethodYieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public void test_creation_of_states() throws Exception {
assertThat(generatedStatesFromFirstYield).hasSize(1);
}

@Test
public void test_toString() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java");
MethodYield yield = getMethodBehavior(sev, "bar").getValue().yields().get(0);
assertThat(yield.toString()).isEqualTo("{params: [TRUE, NOT_NULL], result: null (-1), exceptional: false}");
}

private static enum Status {
A, B
}
Expand Down Expand Up @@ -108,6 +115,48 @@ public void all_constraints_should_be_valid_to_generate_a_new_state() throws Exc
assertThat(generatedStatesFromFirstYield.iterator().next().getConstraintWithStatus(sv2, Status.B)).isNotNull();
}

@Test
public void test_yield_equality() {
MethodYield yield = new MethodYield(1, false);
MethodYield otherYield;

assertThat(yield).isNotEqualTo(null);
assertThat(yield).isNotEqualTo(new Object());

// same instance
assertThat(yield).isEqualTo(yield);

// same constraints, same nb of parameters, same exceptional aspect
assertThat(yield).isEqualTo(new MethodYield(1, false));

// arity is taken into account
assertThat(yield).isNotEqualTo(new MethodYield(0, false));

// varargs is taken into account
assertThat(yield).isNotEqualTo(new MethodYield(1, true));

// same arity and constraints but exceptional path
otherYield = new MethodYield(1, false);
otherYield.exception = true;
assertThat(yield).isNotEqualTo(otherYield);

// same arity and constraints but different return value
otherYield = new MethodYield(1, false);
otherYield.resultIndex = 0;
assertThat(yield).isNotEqualTo(otherYield);

// same arity but different return constraint
otherYield = new MethodYield(1, false);
otherYield.resultConstraint = ObjectConstraint.NOT_NULL;
assertThat(yield).isNotEqualTo(otherYield);

// same return constraint
yield.resultConstraint = ObjectConstraint.NOT_NULL;
otherYield = new MethodYield(1, false);
otherYield.resultConstraint = ObjectConstraint.NOT_NULL;
assertThat(yield).isEqualTo(otherYield);
}

@Test
public void constraints_on_varargs() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/VarArgsYields.java");
Expand Down

0 comments on commit e3ce850

Please sign in to comment.