Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Jun 14, 2024
1 parent baec5d5 commit 324ae0c
Showing 1 changed file with 203 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,209 @@ public Sample()
new Context(code, SyntaxKind.Block).ValidateAllEmpty();
}

#region CaptureReferenceTests

[TestMethod]
public void FlowCaptrure_NullCoalescingAssignment()
{
/* Block 1
* #0=stringParam
* |
* Block 2
* #1=#0
* #1 is null --+
* | |
* Block 3 |
* #0=Hello |
* | |
* Exit <-----+
*/
const string code = """stringParam ??= "Hello";""";
var context = CreateContextCS(code, additionalParameters: "string stringParam");
context.ValidateEntry(LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("stringParam"));
context.Validate(context.Cfg.Blocks[3], null);
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_NullCoalescingOperator()
{
/* Block 1
* #0=stringParam
* #0 is null
* / \
* / \
* Block 3 Block 4
* #1=#0 #1="Hello"
* \ /
* Block 5
* s=#1
* |
* Exit
*/
const string code = """string s = stringParam ?? "Hello";""";
var context = CreateContextCS(code, additionalParameters: "string stringParam");
context.ValidateEntry(LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[3], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[4], null);
context.Validate(context.Cfg.Blocks[5], null, LiveIn("stringParam"));
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_NullConditionalOperators()
{
/* Block 1
* #0=stringParam
* #0 is null
* / \
* / \
* Block 2 Block 3
* #1=Length #1="Hello"
* \ /
* Block 4
* result=#1
* |
* Exit
*/
const string code = """int? anInt = stringParam.?Length;""";
var context = CreateContextCS(code, additionalParameters: "string stringParam");
context.ValidateEntry(LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[2], null);
context.Validate(context.Cfg.Blocks[3], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[4], null, LiveIn("stringParam"));
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_TernaryConditionalOperator()
{
/* Block 1
* boolParameter
* / \
* / \
* Block 2 Block 3
* #0=test #0="Hello"
* \ /
* Block 4
* result=#0
* |
* Exit
*/
const string code = """string s = boolParameter ? stringParam : "Hello";""";
var context = CreateContextCS(code, additionalParameters: "string stringParam");
context.ValidateEntry(LiveIn("boolParameter", "stringParam"), LiveOut("boolParameter", "stringParam"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("boolParameter", "stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[3], null, LiveIn("stringParam"), LiveOut("stringParam"));
context.Validate(context.Cfg.Blocks[4], null, LiveIn("stringParam"));
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_NullCoalescingOperator_ConsequentCalls()
{
/* Block 1
* #0=s1
* #0 is null
* / \
* / \
* Block 2 Block 3 +--F--> Block 4 --------+
* #1=#0 #2=s2 | #1=#2 |
* \ / | |
* Block 6 <-+ +--T--> Block 5-------->|
* s=#1 | #1="Hello" |
* | +-------------------------+
* Exit
*/
const string code = """string s = s1 ?? s2 ?? "Hello";""";
var context = CreateContextCS(code, additionalParameters: "string s1, string s2");
context.ValidateEntry(LiveIn("s1", "s2"), LiveOut("s1", "s2"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("s1", "s2"), LiveOut("s1, s2"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("s1", "s2"), LiveOut("s1"));
context.Validate(context.Cfg.Blocks[3], null, LiveIn("s1", "s2"), LiveOut("s2"));
context.Validate(context.Cfg.Blocks[4], null, LiveIn("s2"), LiveOut("s2"));
context.Validate(context.Cfg.Blocks[5], null, LiveIn("s2"));
context.Validate(context.Cfg.Blocks[6], null, LiveIn("s1, s2"));
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_NullCoalescingOperator_Nested()
{
/* Block 1
* #0=s1
* #0 is null
* / \
* / \
* Block 2 Block 3 +------F----> Block 4 --------+
* #1=#0 s2 is null | #2="NestedTrue" |
* \ / | |
* Block 6 +------T----> Block 5-------->|
* s=#1 #2="NestedFalse" |
* | |
* | Block6<--+
* | #2 is null
* | / \
* | T F
* | / \
* | Block8 Block7
* | #1="Hello" #1=#2"
* | | |
* +------------>Block9<---------+---------+
* s1=#1
* |
* Exit
*/
const string code = """string s = s1 ?? (s2 is null? "NestedTrue" : "NestedFalse") ?? "Hello";""";
var context = CreateContextCS(code, additionalParameters: "string s1, string s2");
context.ValidateEntry(LiveIn("s1", "s2"), LiveOut("s1", "s2"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("s1", "s2"), LiveOut("s1, s2"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("s1", "s2"), LiveOut("s1"));
context.Validate(context.Cfg.Blocks[3], null, LiveIn("s1", "s2"), LiveOut("s2"));
context.Validate(context.Cfg.Blocks[4], null, LiveIn("s2"), LiveOut("s2"));
context.Validate(context.Cfg.Blocks[5], null, LiveIn("s2"));
context.Validate(context.Cfg.Blocks[6], null, LiveIn("s1, s2"));
context.ValidateExit();
}

[TestMethod]
public void FlowCaptrure_NullCoalescingOperator_Overwrite()
{
/* Block 1
* #0=s1
* |
* Block 2
* #1=(s1="overwrite")
* #1 is null
* / \
* T F
* / \
* Block3 Block4
* #2=#1 #2="value"
* \ /
* \ /
* Block5
* s1=#2
*/
const string code = """s1 = (s1 = "overwrite") ?? "value";""";
var context = CreateContextCS(code, additionalParameters: "string s1");
context.ValidateEntry(LiveIn("s1"), LiveOut("s1"));
context.Validate(context.Cfg.Blocks[1], null, LiveIn("s1"), LiveOut("s1"));
context.Validate(context.Cfg.Blocks[2], null, LiveIn("s1"), LiveOut("s1"));
context.Validate(context.Cfg.Blocks[3], null);
context.Validate(context.Cfg.Blocks[4], null);
context.Validate(context.Cfg.Blocks[5], null);
context.ValidateExit();
}

#endregion

private static Context CreateContextCS(string methodBody, string localFunctionName = null, string additionalParameters = null)
{
additionalParameters = additionalParameters is null ? null : ", " + additionalParameters;
Expand Down

0 comments on commit 324ae0c

Please sign in to comment.