Skip to content

Commit

Permalink
Strip directives by commenting out rather than omitting, to keep Diag…
Browse files Browse the repository at this point in the history
…nostic location correct
  • Loading branch information
andersforsgren committed Dec 10, 2019
1 parent a24ff33 commit 7f1adf6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/aggregator-ruleng/DirectivesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ internal bool Parse()

internal string GetRuleCode()
{
return string.Join(Environment.NewLine, ruleCode, firstCodeLine, ruleCode.Length - firstCodeLine);
StringBuilder sb = new StringBuilder();
// Keep directive lines commented out, to maintain source location of rule code for diagnostics.
for(int i=0; i<ruleCode.Length; i++)
sb.AppendLine(i < firstCodeLine ? $"//{ruleCode[i]}" : ruleCode[i]);
return sb.ToString();
}

// directives
Expand Down
21 changes: 18 additions & 3 deletions src/unittests-ruleng/RuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class StringExtensions
{
internal static string[] Mince(this string ruleCode)
{
return ruleCode.Split(Environment.NewLine);
return ruleCode.Split('\n').Select(line => line.TrimEnd('\r')).ToArray();
}
}

Expand Down Expand Up @@ -349,6 +349,21 @@ public async Task ImportDirective_Fail()
);
}

[Fact]
public void Diagnostic_Location_Returned_Correctly()
{
string ruleCode = @".import=""System.Diagnostics""
Debug.WriteLine(""test"");
return string.Empty
";

var engine = new RuleEngine(logger, ruleCode.Mince(), SaveMode.Default, dryRun: true);
var (success, diagnostics) = engine.VerifyRule();
Assert.False(success);
Assert.Single(diagnostics);
Assert.Equal(2, diagnostics[0].Location.GetLineSpan().StartLinePosition.Line);
}

[Fact]
public async Task DeleteWorkItem()
{
Expand Down Expand Up @@ -487,7 +502,7 @@ public async Task CustomNumericField_HasValue_Succeeds()
witClient.GetWorkItemAsync(workItemId, expand: WorkItemExpand.All).Returns(workItem);
string ruleCode = @"
var customField = self.GetFieldValue<decimal>(""MyOrg.CustomNumericField"", 3.0m);
return customField.ToString(""N"");
return customField.ToString(""N"", System.Globalization.CultureInfo.InvariantCulture);
";

var engine = new RuleEngine(logger, ruleCode.Mince(), SaveMode.Default, dryRun: true);
Expand All @@ -512,7 +527,7 @@ public async Task CustomNumericField_NoValue_ReturnsDefault()
witClient.GetWorkItemAsync(workItemId, expand: WorkItemExpand.All).Returns(workItem);
string ruleCode = @"
var customField = self.GetFieldValue<decimal>(""MyOrg.CustomNumericField"", 3.0m);
return customField.ToString(""N"");
return customField.ToString(""N"", System.Globalization.CultureInfo.InvariantCulture);
";

var engine = new RuleEngine(logger, ruleCode.Mince(), SaveMode.Default, dryRun: true);
Expand Down

0 comments on commit 7f1adf6

Please sign in to comment.