Skip to content

Commit

Permalink
More UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Mar 10, 2022
1 parent 5144ae5 commit 19f640b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
Expand All @@ -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)
Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 19f640b

Please sign in to comment.