/
SonarLintXmlTest.cs
82 lines (70 loc) · 3.72 KB
/
SonarLintXmlTest.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
/*
* 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 System.Xml.Serialization;
namespace SonarAnalyzer.UnitTest.Helpers;
[TestClass]
public class SonarLintXmlTest
{
[TestMethod]
public void SonarLintXml_DeserializeFile_ExpectedValues()
{
var deserializer = new XmlSerializer(typeof(SonarLintXml));
using TextReader textReader = new StreamReader("ResourceTests\\SonarLintXml\\All_properties_small_template\\SonarLint.xml");
var sonarLintXml = (SonarLintXml)deserializer.Deserialize(textReader);
AssertSettings(sonarLintXml.Settings);
AssertRules(sonarLintXml.Rules);
}
private static void AssertSettings(List<SonarLintXmlKeyValuePair> settings)
{
settings.Should().HaveCount(10);
AssertKeyValuePair(settings[0], "sonar.cs.ignoreHeaderComments", "true");
AssertKeyValuePair(settings[1], "sonar.cs.analyzeGeneratedCode", "false");
AssertKeyValuePair(settings[2], "sonar.cs.file.suffixes", ".cs");
AssertKeyValuePair(settings[3], "sonar.cs.roslyn.ignoreIssues", "false");
AssertKeyValuePair(settings[4], "sonar.exclusions", "Fake/Exclusions/**/*,Fake/Exclusions/Second*/**/*");
AssertKeyValuePair(settings[5], "sonar.inclusions", "Fake/Inclusions/**/*,Fake/Inclusions/Second*/**/*");
AssertKeyValuePair(settings[6], "sonar.global.exclusions", "Fake/GlobalExclusions/**/*,Fake/GlobalExclusions/Second*/**/*");
AssertKeyValuePair(settings[7], "sonar.test.exclusions", "Fake/TestExclusions/**/*,Fake/TestExclusions/Second*/**/*");
AssertKeyValuePair(settings[8], "sonar.test.inclusions", "Fake/TestInclusions/**/*,Fake/TestInclusions/Second*/**/*");
AssertKeyValuePair(settings[9], "sonar.global.test.exclusions", "Fake/GlobalTestExclusions/**/*,Fake/GlobalTestExclusions/Second*/**/*");
}
private static void AssertRules(List<SonarLintXmlRule> rules)
{
rules.Should().HaveCount(4);
rules.Where(x => x.Parameters.Any()).Should().HaveCount(2);
rules[0].Key.Should().BeEquivalentTo("S2225");
rules[0].Parameters.Should().BeEmpty();
rules[1].Key.Should().BeEquivalentTo("S2342");
rules[1].Parameters.Should().HaveCount(2);
AssertKeyValuePair(rules[1].Parameters[0], "format", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?$");
AssertKeyValuePair(rules[1].Parameters[1], "flagsAttributeFormat", "^([A-Z]{1,3}[a-z0-9]+)*([A-Z]{2})?s$");
rules[2].Key.Should().BeEquivalentTo("S2346");
rules[2].Parameters.Should().BeEmpty();
rules[3].Key.Should().BeEquivalentTo("S1067");
rules[3].Parameters.Should().HaveCount(1);
AssertKeyValuePair(rules[3].Parameters[0], "max", "1");
}
private static void AssertKeyValuePair(SonarLintXmlKeyValuePair pair, string expectedKey, string expectedValue)
{
pair.Key.Should().BeEquivalentTo(expectedKey);
pair.Value.Should().BeEquivalentTo(expectedValue);
}
}