Skip to content

Commit

Permalink
UTs: Cleanup ResourceTest folder 2/2 (#6907)
Browse files Browse the repository at this point in the history
  • Loading branch information
1 parent bbefe66 commit e841ab6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 194 deletions.
30 changes: 0 additions & 30 deletions analyzers/its/sources/AnalyzeGenerated.CS/SonarLint.xml

This file was deleted.

30 changes: 0 additions & 30 deletions analyzers/its/sources/SkipGenerated.CS/SonarLint.xml

This file was deleted.

Expand Up @@ -34,7 +34,7 @@ public class ParameterLoaderTest
[TestMethod]
[DataRow("path//aSonarLint.xml")] // different name
[DataRow("path//SonarLint.xmla")] // different extension
public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters(string filePath)
public void SetParameterValues_WithInvalidSonarLintPath_DoesNotPopulateParameters(string filePath)
{
// Arrange
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml")));
Expand All @@ -50,7 +50,7 @@ public void SetParameterValues_WhenNoSonarLintIsGiven_DoesNotPopulateParameters(
[TestMethod]
[DataRow("a/SonarLint.xml")] // unix path
[DataRow("a\\SonarLint.xml")]
public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperties(string filePath)
public void SetParameterValues_WithValidSonarLintPath_PopulatesProperties(string filePath)
{
// Arrange
var compilation = CreateCompilationWithOption(filePath, SourceText.From(File.ReadAllText("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml")));
Expand All @@ -64,7 +64,7 @@ public void SetParameterValues_WhenGivenValidSonarLintFilePath_PopulatesProperti
}

[TestMethod]
public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_PopulatesProperties()
public void SetParameterValues_SonarLintFileWithIntParameterType_PopulatesProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml");
Expand All @@ -78,35 +78,39 @@ public void SetParameterValues_WhenGivenSonarLintFileHasIntParameterType_Populat
}

[TestMethod]
public void SetParameterValues_WhenGivenSonarLintFileHasStringParameterType_OnlyOneParameter_PopulatesProperty()
public void SetParameterValues_SonarLintFileWithStringParameterType_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithStringParameter\\SonarLint.xml");
var parameterValue = "1";
var filePath = GenerateSonarLintXmlWithParametrizedRule("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()
public void SetParameterValues_SonarLintFileWithBooleanParameterType_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\RuleWithBooleanParameter\\SonarLint.xml");
var parameterValue = true;
var filePath = GenerateSonarLintXmlWithParametrizedRule("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]
public void SetParameterValues_WhenGivenValidSonarLintFileAndDoesNotContainAnalyzerParameters_DoesNotPopulateProperties()
public void SetParameterValues_SonarLintFileWithoutRuleParameters_DoesNotPopulateProperties()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\All_properties_cs\\SonarLint.xml");
Expand Down Expand Up @@ -177,10 +181,12 @@ public void SetParameterValues_WithMalformedXml_DoesNotPopulateProperties(string
}

[TestMethod]
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotPopulateProperties()
public void SetParameterValues_SonarLintFileWithStringInsteadOfIntParameterType_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfInt\\SonarLint.xml");
var parameterValue = "fooBar";
var filePath = GenerateSonarLintXmlWithParametrizedRule("S1067", "max", parameterValue);
var compilation = CreateCompilationWithOption(filePath);
var analyzer = new ExpressionComplexity(); // Cannot use mock because we use reflection to find properties.

// Act
Expand All @@ -191,10 +197,12 @@ public void SetParameterValues_WithWrongPropertyType_StringInsteadOfInt_DoesNotP
}

[TestMethod]
public void SetParameterValues_WithWrongPropertyType_StringInsteadOfBoolean_DoesNotPopulateProperties()
public void SetParameterValues_SonarLintFileWithStringInsteadOfBooleanParameterType_PopulatesProperty()
{
// Arrange
var compilation = CreateCompilationWithOption("ResourceTests\\SonarLintXml\\StringInsteadOfBoolean\\SonarLint.xml");
var parameterValue = "fooBar";
var filePath = GenerateSonarLintXmlWithParametrizedRule("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 GenerateSonarLintXmlWithParametrizedRule(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
}
}
}
};
return AnalysisScaffolding.CreateSonarLintXml(TestContext, rulesParameters: ruleParameters);
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit e841ab6

Please sign in to comment.