From 42c0090967465bdc673ee5059cee86a8412279f1 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Wed, 24 May 2017 11:49:58 +0200 Subject: [PATCH] Fix quality flaw: coverage of toString methods (#1490) --- .../org/sonar/java/se/ProgramStateTest.java | 18 ++++++++++--- .../RelationalSymbolicValueTest.java | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/java-frontend/src/test/java/org/sonar/java/se/ProgramStateTest.java b/java-frontend/src/test/java/org/sonar/java/se/ProgramStateTest.java index 4c1fc7a6ee4..2f33ddd2169 100644 --- a/java-frontend/src/test/java/org/sonar/java/se/ProgramStateTest.java +++ b/java-frontend/src/test/java/org/sonar/java/se/ProgramStateTest.java @@ -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() { @@ -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 } "); } diff --git a/java-frontend/src/test/java/org/sonar/java/se/symbolicvalues/RelationalSymbolicValueTest.java b/java-frontend/src/test/java/org/sonar/java/se/symbolicvalues/RelationalSymbolicValueTest.java index 3eba29c689c..9dfef62e4fd 100644 --- a/java-frontend/src/test/java/org/sonar/java/se/symbolicvalues/RelationalSymbolicValueTest.java +++ b/java-frontend/src/test/java/org/sonar/java/se/symbolicvalues/RelationalSymbolicValueTest.java @@ -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; @@ -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; @@ -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; @@ -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"); + } }