/
SonarLintXmlReaderTest.cs
115 lines (102 loc) · 5.43 KB
/
SonarLintXmlReaderTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.IO;
using Microsoft.CodeAnalysis.Text;
namespace SonarAnalyzer.UnitTest.Helpers;
[TestClass]
public class SonarLintXmlReaderTest
{
[DataTestMethod]
[DataRow(LanguageNames.CSharp, "cs")]
[DataRow(LanguageNames.VisualBasic, "vbnet")]
public void SonarLintXmlReader_WhenAllValuesAreSet_ExpectedValues(string language, string xmlLanguageName)
{
var sut = CreateSonarLintXmlReader($"ResourceTests\\SonarLintXml\\All_Properties_{xmlLanguageName}\\SonarLint.xml");
sut.IgnoreHeaderComments(language).Should().BeTrue();
sut.AnalyzeGeneratedCode(language).Should().BeFalse();
AssertArrayContent(sut.Exclusions, nameof(sut.Exclusions));
AssertArrayContent(sut.Inclusions, nameof(sut.Inclusions));
AssertArrayContent(sut.GlobalExclusions, nameof(sut.GlobalExclusions));
AssertArrayContent(sut.TestExclusions, nameof(sut.TestExclusions));
AssertArrayContent(sut.TestInclusions, nameof(sut.TestInclusions));
AssertArrayContent(sut.GlobalTestExclusions, nameof(sut.GlobalTestExclusions));
sut.ParametrizedRules.Should().HaveCount(8);
var rule = sut.ParametrizedRules.First(x => x.Key.Equals("S2342"));
rule.Parameters[0].Key.Should().Be("format");
rule.Parameters[0].Value.Should().Be("^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?$");
rule.Parameters[1].Key.Should().Be("flagsAttributeFormat");
rule.Parameters[1].Value.Should().Be("^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?s$");
static void AssertArrayContent(string[] array, string folder)
{
array.Should().HaveCount(2);
array[0].Should().BeEquivalentTo($"Fake/{folder}/**/*");
array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*");
}
}
[TestMethod]
public void SonarLintXmlReader_PartiallyMissingProperties_ExpectedAndDefaultValues()
{
var sut = CreateSonarLintXmlReader("ResourceTests\\SonarLintXml\\Partially_missing_properties\\SonarLint.xml");
sut.IgnoreHeaderComments().Should().BeFalse();
sut.AnalyzeGeneratedCode().Should().BeTrue();
AssertArrayContent(sut.Exclusions, nameof(sut.Exclusions));
AssertArrayContent(sut.Inclusions, nameof(sut.Inclusions));
sut.GlobalExclusions.Should().NotBeNull().And.HaveCount(0);
sut.TestExclusions.Should().NotBeNull().And.HaveCount(0);
sut.TestInclusions.Should().NotBeNull().And.HaveCount(0);
sut.GlobalTestExclusions.Should().NotBeNull().And.HaveCount(0);
sut.ParametrizedRules.Should().NotBeNull().And.HaveCount(0);
}
[DataTestMethod]
[DataRow("")]
[DataRow("this is not an xml")]
[DataRow(@"<?xml version=""1.0"" encoding=""UTF - 8""?><AnalysisInput><Settings>")]
public void SonarLintXmlReader_WithMalformedXml_DefaultBehaviour(string sonarLintXmlContent) =>
CheckSonarLintXmlReaderDefaultValues(new SonarLintXmlReader(SourceText.From(sonarLintXmlContent)));
[TestMethod]
public void SonarLintXmlReader_MissingProperties_DefaultBehaviour() =>
CheckSonarLintXmlReaderDefaultValues(CreateSonarLintXmlReader("ResourceTests\\SonarLintXml\\Missing_properties\\SonarLint.xml"));
[TestMethod]
public void SonarLintXmlReader_WithIncorrectValueType_DefaultBehaviour() =>
CheckSonarLintXmlReaderDefaultValues(CreateSonarLintXmlReader("ResourceTests\\SonarLintXml\\Incorrect_value_type\\SonarLint.xml"));
[TestMethod]
public void SonarLintXmlReader_CheckEmpty_DefaultBehaviour() =>
CheckSonarLintXmlReaderDefaultValues(SonarLintXmlReader.Empty);
private static void CheckSonarLintXmlReaderDefaultValues(SonarLintXmlReader sut)
{
sut.AnalyzeGeneratedCode().Should().BeFalse();
sut.IgnoreHeaderComments().Should().BeFalse();
sut.Exclusions.Should().NotBeNull().And.HaveCount(0);
sut.Inclusions.Should().NotBeNull().And.HaveCount(0);
sut.GlobalExclusions.Should().NotBeNull().And.HaveCount(0);
sut.TestExclusions.Should().NotBeNull().And.HaveCount(0);
sut.TestInclusions.Should().NotBeNull().And.HaveCount(0);
sut.GlobalTestExclusions.Should().NotBeNull().And.HaveCount(0);
sut.ParametrizedRules.Should().NotBeNull().And.HaveCount(0);
}
private static void AssertArrayContent(string[] array, string folder)
{
array.Should().HaveCount(2);
array[0].Should().BeEquivalentTo($"Fake/{folder}/**/*");
array[1].Should().BeEquivalentTo($"Fake/{folder}/Second*/**/*");
}
private static SonarLintXmlReader CreateSonarLintXmlReader(string relativePath) =>
new(SourceText.From(File.ReadAllText(relativePath)));
}