Skip to content

Commit

Permalink
More cleanup, removed in-memory files
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini committed Mar 10, 2023
1 parent 902989a commit 80360bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,32 @@ public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_Populat
public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_OnlyOneParameter_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithStringParameter\\SonarLint.xml");
var parameterValue = "1";
var filePath = GenerateSonarLintWithParametrizedRule("S2342", "flagsAttributeFormat", parameterValue);
var compilation = CreateCompilationWithOption(filePath);
var analyzer = new EnumNameShouldFollowRegex(); // Cannot use mock because we use reflection to find properties.

// Act
ParameterLoader.SetParameterValues(analyzer, compilation.SonarLintFile());

// Assert
analyzer.FlagsEnumNamePattern.Should().Be("1"); // value from XML file
analyzer.FlagsEnumNamePattern.Should().Be(parameterValue); // value from XML file
}

[TestMethod]
public void SetParameterValues_WhenGivenSonarLintFileHasBooleanParameterType_OnlyOneParameter_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithBooleanParameter\\SonarLint.xml");
var parameterValue = true;
var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue.ToString());
var compilation = CreateCompilationWithOption(filePath);
var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties.

// Act
ParameterLoader.SetParameterValues(analyzer, compilation.SonarLintFile());

// Assert
analyzer.IsRegularExpression.Should().BeTrue(); // value from XML file
analyzer.IsRegularExpression.Should().Be(parameterValue); // value from XML file
}

[TestMethod]
Expand Down Expand Up @@ -180,7 +184,9 @@ public void SetParameterValues_WithMalformedXml_DoesNotPopulateProperties(string
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfInt\\SonarLint.xml");
var parameterValue = "fooBar";
var filePath = GenerateSonarLintWithParametrizedRule("S1067", "max", parameterValue);
var compilation = CreateCompilationWithOption(filePath);
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -194,7 +200,9 @@ public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotP
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfBoolean_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfBoolean\\SonarLint.xml");
var parameterValue = "fooBar";
var filePath = GenerateSonarLintWithParametrizedRule("S1451", "isRegularExpression", parameterValue);
var compilation = CreateCompilationWithOption(filePath);
var analyzer = new CheckFileLicense(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -213,5 +221,25 @@ private static SonarCompilationReportingContext CreateCompilationWithOption(stri
var compilationContext = new CompilationAnalysisContext(compilation, options, _ => { }, _ => true, default);
return new(AnalysisScaffolding.CreateSonarAnalysisContext(), compilationContext);
}
private string GenerateSonarLintWithParametrizedRule(string ruleId, string key, string value)
{
var ruleParameters = new List<SonarLintXmlRule>()
{
new SonarLintXmlRule()
{
Key = ruleId,
Parameters = new List<SonarLintXmlKeyValuePair>()
{
new SonarLintXmlKeyValuePair()
{
Key = key,
Value = value
}
}
}
};
var sonarLintXml = AnalysisScaffolding.GenerateSonarLintXmlContent(rulesParameters: ruleParameters);
return TestHelper.WriteFile(TestContext, "SonarLint.xml", sonarLintXml);
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 80360bb

Please sign in to comment.