Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SE - Nullable: Add UTs for Debug.Assert #6956

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static ProgramState ProcessAssertedBoolSymbol(ProgramState state, IOpera
else
{
return operation.TrackedSymbol() is { } symbol
? state.SetSymbolConstraint(symbol, BoolConstraint.From(!isNegated))
? state.SetSymbolConstraint(symbol, BoolConstraint.From(!isNegated)).SetSymbolConstraint(symbol, ObjectConstraint.NotNull)
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
: state;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,11 +750,26 @@ public void Invocation_InformationIsNothing_NoTrackedSymbol()
}

[DataTestMethod]
[DataRow("arg != null")]
[DataRow("arg is not null")]
[DataRow("arg is { }")]
public void Invocation_DebugAssert_LearnsNotNull_Simple(string expression) =>
DebugAssertValues(expression).Should().HaveCount(1).And.ContainSingle(x => x.HasConstraint(ObjectConstraint.NotNull));
[DataRow("arg is not true", "bool?")]
[DataRow("arg is not false", "bool?")]
[DataRow("arg is not 42", "object")]
[DataRow("arg is not { Length: 0 }", "string")]
[DataRow("arg.GetValueOrDefault()", "bool?")]
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
public void Invocation_DebugAssert_DoesNotLearn(string expression, string argType) =>
DebugAssertValues(expression, argType).Should().SatisfyRespectively(x => x.Should().HaveNoConstraints());

[DataTestMethod]
[DataRow("arg != null", "object")]
[DataRow("arg != null", "int?")]
[DataRow("arg != null", "bool?")]
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
[DataRow("arg is not null", "object")]
[DataRow("arg is not null", "int?")]
[DataRow("arg is not null", "bool?")]
[DataRow("arg is { }", "object")]
[DataRow("arg is { }", "int?")]
[DataRow("arg is { }", "bool?")]
public void Invocation_DebugAssert_LearnsNotNull_Simple(string expression, string argType) =>
DebugAssertValues(expression, argType).Should().SatisfyRespectively(x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));

[TestMethod]
public void Invocation_DebugAssert_LearnsNotNull_AndAlso() =>
Expand Down Expand Up @@ -786,6 +801,12 @@ public void Invocation_DebugAssert_LearnsBoolConstraint_Simple() =>
public void Invocation_DebugAssert_LearnsBoolConstraint_Binary() =>
DebugAssertValues("arg == true", "bool").Should().SatisfyRespectively(x => x.Should().HaveOnlyConstraints(ObjectConstraint.NotNull, BoolConstraint.True));

[DataTestMethod]
[DataRow("arg is true", true)]
[DataRow("arg is false", false)]
public void Invocation_DebugAssert_LearnsBoolConstraint_Nullable(string expression, bool expected) =>
DebugAssertValues(expression, "bool?").Should().SatisfyRespectively(x => x.Should().HaveOnlyConstraints(BoolConstraint.From(expected)));
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

[TestMethod]
public void Invocation_DebugAssert_LearnsBoolConstraint_AlwaysEnds() =>
DebugAssertValues("false", "bool").Should().BeEmpty();
Expand Down Expand Up @@ -823,6 +844,18 @@ public static void Assert() { }
new SETestContext(code, AnalyzerLanguage.CSharp, Array.Empty<SymbolicCheck>()).Validator.ValidateTagOrder("End");
}

[DataTestMethod]
[DataRow("int?")]
[DataRow("bool?")]
public void Invocation_DebugAssert_NullableHasValue_Simple(string argType) =>
DebugAssertValues("arg.HasValue", argType).Should().SatisfyRespectively(x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

[DataTestMethod]
[DataRow("int?")]
[DataRow("bool?")]
public void Invocation_DebugAssert_NullableHasValue_Binary(string argType) =>
DebugAssertValues("arg.HasValue == true", argType).Should().SatisfyRespectively(x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));

private static SymbolicValue[] DebugAssertValues(string expression, string argType = "object")
{
var code = $@"
Expand Down