Skip to content

Commit

Permalink
Fix quality flaw: coverage of toString methods (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck authored May 24, 2017
1 parent a555496 commit 42c0090
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public void testStackUnstack() {

@Test
public void testToString() {
SymbolicValue sv3 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue() {
@Override
public String toString() {
return "SV_3";
}
};
ProgramState state = ProgramState.EMPTY_STATE.stackValue(sv3);
Symbol variable = new JavaSymbol.VariableJavaSymbol(0, "x", new JavaSymbol(JavaSymbol.TYP, 0, "A", Symbols.unknownSymbol));
SymbolicValue sv4 = new SymbolicValue() {
Expand All @@ -92,10 +97,15 @@ public String toString() {
}
};
state = state.put(variable, sv4);
SymbolicValue sv5 = new SymbolicValue();
state = state.stackValue(sv5);
SymbolicValue sv5 = new SymbolicValue() {
@Override
public String toString() {
return "SV_5";
}
};
state = state.stackValue(sv5, variable);
// FIXME to string is not really nice by displaying classes and order is not guaranteed.
assertThat(state.toString()).contains("A#x->SV_4", "SV_NULL", "SV_TRUE", "SV_FALSE");
assertThat(state.toString()).contains("A#x->SV_4", "SV_NULL", "SV_TRUE", "SV_FALSE", "A#x->SV_5", "SV_3");
//.isEqualTo("{ A#x->SV_4} { SV_0_NULL-> class org.sonar.java.se.constraint.ObjectConstraint->NULL SV_1_TRUE-> class org.sonar.java.se.constraint.BooleanConstraint->TRUE class org.sonar.java.se.constraint.ObjectConstraint->NOT_NULL SV_2_FALSE-> class org.sonar.java.se.constraint.BooleanConstraint->FALSE class org.sonar.java.se.constraint.ObjectConstraint->NOT_NULL} { [SV_5, SV_3] } { A#x } ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.java.se.symbolicvalues;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
Expand All @@ -27,6 +28,8 @@
import org.sonar.java.collections.PMap;
import org.sonar.java.model.InternalSyntaxToken;
import org.sonar.java.model.expression.BinaryExpressionTreeImpl;
import org.sonar.java.resolve.JavaSymbol;
import org.sonar.java.resolve.Symbols;
import org.sonar.java.se.JavaCheckVerifier;
import org.sonar.java.se.ProgramState;
import org.sonar.java.se.checks.DivisionByZeroCheck;
Expand All @@ -35,6 +38,7 @@
import org.sonar.java.se.constraint.Constraint;
import org.sonar.java.se.constraint.ConstraintManager;
import org.sonar.java.se.constraint.ObjectConstraint;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.Tree;

Expand Down Expand Up @@ -428,4 +432,25 @@ public void test_equality() throws Exception {
.map(k -> relationalSV(k, a, b))
.forEach(sv -> assertThat(sv.isEquality()).isFalse());
}

@Test
public void test_to_string() throws Exception {
RelationalSymbolicValue rsv = new RelationalSymbolicValue(EQUAL);
Symbol var = new JavaSymbol.VariableJavaSymbol(0, "x", new JavaSymbol(JavaSymbol.TYP, 0, "A", Symbols.unknownSymbol));
SymbolicValue left = new SymbolicValue() {
@Override
public String toString() {
return "left";
}
};
SymbolicValue right = new SymbolicValue() {
@Override
public String toString() {
return "right";
}
};

rsv.computedFrom(ImmutableList.of(new ProgramState.SymbolicValueSymbol(right, null), new ProgramState.SymbolicValueSymbol(left, var)));
assertThat(rsv).hasToString("left(A#x)==right");
}
}

0 comments on commit 42c0090

Please sign in to comment.