From 19f640bbc8c60b3d13e8ae564f1d186c688b93e5 Mon Sep 17 00:00:00 2001 From: Pavel Mikula Date: Thu, 10 Mar 2022 15:16:40 +0100 Subject: [PATCH] More UTs --- .../RoslynSymbolicExecutionTest.Branching.cs | 54 ++++++++++++++++--- .../RoslynSymbolicExecutionTest.Loops.cs | 15 ++++++ ...ynSymbolicExecutionTest.PatternMatching.cs | 43 +++++++++++++++ 3 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.PatternMatching.cs diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Branching.cs b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Branching.cs index 3eb762b4edc..76517404bd4 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Branching.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Branching.cs @@ -311,7 +311,7 @@ public void Branching_TrueConstraint_VisitsIfBranch() Tag(""Else""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "If", "End"); } @@ -330,7 +330,7 @@ public void Branching_TrueConstraintNegated_VisitsElseBranch() Tag(""Else""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "Else", "End"); } @@ -349,7 +349,7 @@ public void Branching_FalseConstraint_VisitsElseBranch() Tag(""Else""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "Else", "End"); } @@ -368,7 +368,7 @@ public void Branching_FalseConstraintNegated_VisitsIfBranch() Tag(""Else""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "If", "End"); } @@ -387,7 +387,7 @@ public void Branching_NoConstraint_VisitsBothBranches() Tag(""Else""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "If", "Else", "End"); @@ -399,7 +399,6 @@ public void Branching_BoolConstraints_ComplexCase() const string code = @" var isTrue = true; var isFalse = false; -var isChanding = boolParameter; if (isTrue && isTrue && !isFalse) { if (isFalse || !isTrue) @@ -420,9 +419,50 @@ public void Branching_BoolConstraints_ComplexCase() Tag(""UnreachableElse""); } Tag(""End"");"; - SETestContext.CreateCS(code, new EmptyTestCheck()).Validator.ValidateTagOrder( + SETestContext.CreateCS(code).Validator.ValidateTagOrder( "Reachable", "End"); } + + [TestMethod] + public void Branching_TrueLiteral_VisitsIfBranch_NotSupported() + { + const string code = @" +if (true) +{ + Tag(""If""); +} +else +{ + Tag(""Else""); +} +Tag(""End"");"; + SETestContext.CreateCS(code).Validator.ValidateTagOrder( + "If", + "Else", // ToDo: This should not be here, branch operation symbolic value is not persisted yet + "End"); + } + + [TestMethod] + public void Branching_TrueConstraint_SwitchStatement() + { + const string code = @" +var isTrue = true; +switch (isTrue) +{ + case true: + Tag(""True""); + break; + case false: + Tag(""False""); + break; + default: + Tag(""Default""); +} +Tag(""End"");"; + SETestContext.CreateCS(code).Validator.ValidateTagOrder( + "True", + "End"); + } } } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Loops.cs b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Loops.cs index 0332cb1089a..7ab103f83a8 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Loops.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Loops.cs @@ -149,5 +149,20 @@ public void GoTo_InstructionVisitedMaxTwice() .And.ContainSingle(x => x.HasConstraint(TestConstraint.First) && !x.HasConstraint(BoolConstraint.True)) .And.ContainSingle(x => x.HasConstraint(TestConstraint.First) && x.HasConstraint(BoolConstraint.True) && !x.HasConstraint(DummyConstraint.Dummy)); } + + [DataTestMethod] + [DataRow("for (var i = 0; condition; i++)")] + [DataRow("while (condition)")] + public void Loops_FalseConditionNotExecuted(string loop) + { + var code = $@" +var condition = false; +{loop} +{{ + Tag(""Loop""); +}} +Tag(""End"");"; + SETestContext.CreateCS(code).Validator.ValidateTagOrder("End"); + } } } diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.PatternMatching.cs b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.PatternMatching.cs new file mode 100644 index 00000000000..644d0a84059 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.PatternMatching.cs @@ -0,0 +1,43 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2022 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SonarAnalyzer.UnitTest.TestFramework.SymbolicExecution; + +namespace SonarAnalyzer.UnitTest.SymbolicExecution.Roslyn +{ + public partial class RoslynSymbolicExecutionTest + { + [TestMethod] + public void Patterns_TrueConstraint_SwitchExpression_NotSupported() + { + const string code = @" +var isTrue = true; +var value = isTrue switch +{ + true => true, + false => false +}; +Tag(""Value"", value);"; + SETestContext.CreateCS(code).Validator.ValidateTag("Value", x => x.Should().BeNull()); // Should have BoolConstraint.True instead + } + } +}