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

Strip directives by commenting out rather than omitting #85

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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