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

UTs: Cleanup ResourceTest folder 2/2 #6907

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8d4b756
SonarLint.xml file read base logic and tests
CristianAmbrosini Mar 1, 2023
0a4d736
Removing relativeRootFromSonarLintXml and deprecated 'Files' element
CristianAmbrosini Mar 1, 2023
191024e
Replaced Lazy initialization with null-coalescing assignment operator
CristianAmbrosini Mar 1, 2023
9f75a7f
Removed empty list initializers
CristianAmbrosini Mar 2, 2023
072d3a1
Addressed PR review comments
CristianAmbrosini Mar 2, 2023
a0f2261
Replaced tabs with spaces
CristianAmbrosini Mar 3, 2023
70e2ed6
Add test cases
CristianAmbrosini Mar 3, 2023
121fe61
Bump temporarily UTs ToStringCallCount
CristianAmbrosini Mar 6, 2023
34f5e2a
Small merge fix
CristianAmbrosini Mar 6, 2023
b9fc324
Fix rebase issues 2
CristianAmbrosini Mar 6, 2023
8967f01
Fix UTs
CristianAmbrosini Mar 6, 2023
1b3090a
IgnoreAttribute for parametrized test.
CristianAmbrosini Mar 6, 2023
dad394f
Addressed PR comments
CristianAmbrosini Mar 7, 2023
f3048a7
Second round of comments
CristianAmbrosini Mar 8, 2023
6b68109
Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree
CristianAmbrosini Mar 8, 2023
6a51a15
Fix rebase
CristianAmbrosini Mar 9, 2023
46beb8c
Add test cases
CristianAmbrosini Mar 3, 2023
e42c379
Addressed PR comments
CristianAmbrosini Mar 7, 2023
6d4aa6b
Second round of comments
CristianAmbrosini Mar 8, 2023
7a4f0bb
Migrated ShouldAnalyzeFile test case to ShouldAnalyzeTree
CristianAmbrosini Mar 8, 2023
98c1005
Cleanup resource test and migrated references on UTs
CristianAmbrosini Mar 9, 2023
8fc422b
More cleanup, removed in-memory files
CristianAmbrosini Mar 10, 2023
c688e8c
Use CreateSonarLintXml instiead of GenerateSonarLintXmlContent
CristianAmbrosini Mar 13, 2023
b343f8a
UTs methods renaming
CristianAmbrosini Mar 13, 2023
f0cc86a
Add newline
CristianAmbrosini Mar 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
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,
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Parameters = new List<SonarLintXmlKeyValuePair>()
{
new SonarLintXmlKeyValuePair()
{
Key = key,
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Value = value
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
};
return AnalysisScaffolding.CreateSonarLintXml(TestContext, rulesParameters: ruleParameters);
}
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.