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

Merge release/dev17.13 to main #76387

Merged
merged 16 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Feedback & more tests
  • Loading branch information
tmat committed Dec 11, 2024
commit c6ec29879de0e81ca95170bb01d6151b33a1663e
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics.NamingStyles;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.UnitTests;
using Roslyn.Test.Utilities;
using Xunit;
using static Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles.SymbolSpecification;
Expand All @@ -17,9 +18,6 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.NamingStyle

public class EditorConfigNamingStyleParserTests
{
private static NamingStylePreferences ParseDictionary(Dictionary<string, string> options)
=> EditorConfigNamingStyleParser.ParseDictionary(new DictionaryAnalyzerConfigOptions(options.ToImmutableDictionary()));

[Fact]
public void TestPascalCaseRule()
{
Expand All @@ -32,7 +30,7 @@ public void TestPascalCaseRule()
["dotnet_naming_symbols.method_and_property_symbols.applicable_accessibilities"] = "*",
["dotnet_naming_style.pascal_case_style.capitalization"] = "pascal_case"
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -80,7 +78,7 @@ public void TestPascalCaseRuleWithKeyCapitalization()
["dotnet_naming_symbols.method_and_property_symbols.applicable_accessibilities"] = "*",
["dotnet_naming_style.pascal_case_style.capitalization"] = "pascal_case"
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
var namingRule = Assert.Single(result.NamingRules);
var namingStyle = Assert.Single(result.NamingStyles);
var symbolSpec = Assert.Single(result.SymbolSpecifications);
Expand All @@ -102,7 +100,7 @@ public void TestAsyncMethodsAndLocalFunctionsRule()
["dotnet_naming_style.end_in_async_style.capitalization "] = "pascal_case",
["dotnet_naming_style.end_in_async_style.required_suffix"] = "Async",
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -144,7 +142,7 @@ public void TestRuleWithoutCapitalization()
["dotnet_naming_symbols.any_async_methods.required_modifiers"] = "async",
["dotnet_naming_style.end_in_async.required_suffix"] = "Async",
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Empty(result.NamingStyles);
}

Expand All @@ -160,7 +158,7 @@ public void TestPublicMembersCapitalizedRule()
["dotnet_naming_symbols.public_symbols.applicable_accessibilities"] = "public,internal,protected,protected_internal",
["dotnet_naming_style.first_word_upper_case_style.capitalization"] = "first_word_upper",
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -208,7 +206,7 @@ public void TestNonPublicMembersLowerCaseRule()
["dotnet_naming_symbols.non_public_symbols.applicable_accessibilities"] = "private",
["dotnet_naming_style.all_lower_case_style.capitalization"] = "all_lower",
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -250,7 +248,7 @@ public void TestParametersAndLocalsAreCamelCaseRule()
["dotnet_naming_style.camel_case_style.capitalization"] = "camel_case",
};

var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -293,7 +291,7 @@ public void TestLocalFunctionsAreCamelCaseRule()
["dotnet_naming_style.camel_case_style.capitalization"] = "camel_case",
};

var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -329,7 +327,7 @@ public void TestNoRulesAreReturned()
["dotnet_naming_symbols.non_public_symbols.applicable_accessibilities"] = "private",
["dotnet_naming_style.all_lower_case_style.capitalization"] = "all_lower",
};
var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);
Assert.Empty(result.NamingRules);
Assert.Empty(result.NamingStyles);
Assert.Empty(result.SymbolSpecifications);
Expand Down Expand Up @@ -362,7 +360,7 @@ public void TestApplicableKindsParse(string specification, object[] typeOrSymbol
}

var kinds = typeOrSymbolKinds.Select(NamingStylesTestOptionSets.ToSymbolKindOrTypeKind).ToArray();
var result = ParseDictionary(rule);
var result = OptionsTestHelpers.ParseNamingStylePreferences(rule);
Assert.Equal(kinds, result.SymbolSpecifications.SelectMany(x => x.ApplicableSymbolKindList));
}

Expand Down Expand Up @@ -392,7 +390,7 @@ public void TestApplicableAccessibilitiesParse(string specification, Accessibili
rule["dotnet_naming_symbols.accessibilities.applicable_accessibilities"] = specification;
}

var result = ParseDictionary(rule);
var result = OptionsTestHelpers.ParseNamingStylePreferences(rule);
Assert.Equal(accessibilities, result.SymbolSpecifications.SelectMany(x => x.ApplicableAccessibilityList));
}

Expand All @@ -416,8 +414,8 @@ public void TestRequiredModifiersParse()
["dotnet_naming_style.pascal_case.capitalization "] = "pascal_case",
};

var csharpResult = ParseDictionary(charpRule);
var vbResult = ParseDictionary(vbRule);
var csharpResult = OptionsTestHelpers.ParseNamingStylePreferences(charpRule);
var vbResult = OptionsTestHelpers.ParseNamingStylePreferences(vbRule);

Assert.Equal(csharpResult.SymbolSpecifications.SelectMany(x => x.RequiredModifierList.Select(y => y.Modifier)),
vbResult.SymbolSpecifications.SelectMany(x => x.RequiredModifierList.Select(y => y.Modifier)));
Expand All @@ -439,7 +437,7 @@ public void TestPrefixParse()
["dotnet_naming_rule.must_be_pascal_cased_and_prefixed.severity"] = "warning",
};

var result = ParseDictionary(rule);
var result = OptionsTestHelpers.ParseNamingStylePreferences(rule);
Assert.Single(result.NamingRules);
var namingRule = result.NamingRules.Single();
Assert.Single(result.NamingStyles);
Expand Down Expand Up @@ -485,7 +483,7 @@ public void TestEditorConfigParseForApplicableSymbolKinds()
[InlineData("B", "A", "A", "*", "*")]
public void TestOrderedByAccessibilityBeforeName(string firstName, string secondName, string firstNameAfterOrdering, string firstAccessibility, string secondAccessibility)
{
var namingStylePreferences = ParseDictionary(new Dictionary<string, string>()
var namingStylePreferences = OptionsTestHelpers.ParseNamingStylePreferences(new Dictionary<string, string>()
{
[$"dotnet_naming_rule.{firstName}.severity"] = "error",
[$"dotnet_naming_rule.{firstName}.symbols"] = "first_symbols",
Expand Down Expand Up @@ -521,7 +519,7 @@ public void TestOrderedByAccessibilityBeforeName(string firstName, string second
[InlineData("B", "A", "A", "", "")]
public void TestOrderedByModifiersBeforeName(string firstName, string secondName, string firstNameAfterOrdering, string firstModifiers, string secondModifiers)
{
var namingStylePreferences = ParseDictionary(new Dictionary<string, string>()
var namingStylePreferences = OptionsTestHelpers.ParseNamingStylePreferences(new Dictionary<string, string>()
{
[$"dotnet_naming_rule.{firstName}.severity"] = "error",
[$"dotnet_naming_rule.{firstName}.symbols"] = "first_symbols",
Expand Down Expand Up @@ -557,7 +555,7 @@ public void TestOrderedByModifiersBeforeName(string firstName, string secondName
[InlineData("B", "A", "A", "*", "*")]
public void TestOrderedBySymbolsBeforeName(string firstName, string secondName, string firstNameAfterOrdering, string firstSymbols, string secondSymbols)
{
var namingStylePreferences = ParseDictionary(new Dictionary<string, string>()
var namingStylePreferences = OptionsTestHelpers.ParseNamingStylePreferences(new Dictionary<string, string>()
{
[$"dotnet_naming_rule.{firstName}.severity"] = "error",
[$"dotnet_naming_rule.{firstName}.symbols"] = "first_symbols",
Expand Down Expand Up @@ -597,7 +595,7 @@ public void DuplicateRuleKeys()
["dotnet_naming_style.STYLE.capitalization"] = "pascal_case",
};

var result = ParseDictionary(dictionary);
var result = OptionsTestHelpers.ParseNamingStylePreferences(dictionary);

AssertEx.AssertEqualToleratingWhitespaceDifferences("""
<NamingPreferencesInfo SerializationVersion="5">
Expand Down Expand Up @@ -638,8 +636,8 @@ public void DuplicateRuleKeys()
<NamingStyle ID="3" Name="STYLE" Prefix="" Suffix="" WordSeparator="" CapitalizationScheme="PascalCase" />
</NamingStyles>
<NamingRules>
<SerializableNamingRule SymbolSpecificationID="4" NamingStyleID="5" EnforcementLevel="Warning" />
<SerializableNamingRule SymbolSpecificationID="6" NamingStyleID="7" EnforcementLevel="Warning" />
<SerializableNamingRule SymbolSpecificationID="1" NamingStyleID="3" EnforcementLevel="Warning" />
<SerializableNamingRule SymbolSpecificationID="0" NamingStyleID="2" EnforcementLevel="Warning" />
</NamingRules>
</NamingPreferencesInfo>
""",
Expand Down Expand Up @@ -676,11 +674,11 @@ public void Priorities()
</NamingRules>
</NamingPreferencesInfo>
""",
ParseDictionary(dictionary).Inspect(excludeNodes: ["SymbolSpecifications", "NamingStyles"]));
OptionsTestHelpers.ParseNamingStylePreferences(dictionary).Inspect(excludeNodes: ["SymbolSpecifications", "NamingStyles"]));

// adding priorities reverses the order:
dictionary.Add("dotnet_naming_rule.R1.priority", "0");
dictionary.Add("dotnet_naming_rule.R2.priority", "1");
// adding priorities reverses the order - R2 (P0) is now ordered before R1 (P1):
dictionary.Add("dotnet_naming_rule.R2.priority", "0");
dictionary.Add("dotnet_naming_rule.R1.priority", "1");

AssertEx.AssertEqualToleratingWhitespaceDifferences("""
<NamingPreferencesInfo SerializationVersion="5">
Expand All @@ -690,6 +688,6 @@ public void Priorities()
</NamingRules>
</NamingPreferencesInfo>
""",
ParseDictionary(dictionary).Inspect(excludeNodes: ["SymbolSpecifications", "NamingStyles"]));
OptionsTestHelpers.ParseNamingStylePreferences(dictionary).Inspect(excludeNodes: ["SymbolSpecifications", "NamingStyles"]));
}
}
15 changes: 15 additions & 0 deletions src/Features/TestUtilities/Options/NamingStyleTestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.NamingStyles;

namespace Microsoft.CodeAnalysis.Test.Utilities;

internal static class NamingStyleTestUtilities
{
public static string Inspect(this NamingRule rule)
=> $"{rule.NamingStyle.Inspect()} {rule.SymbolSpecification.Inspect()} {rule.EnforcementLevel}";

public static string Inspect(this NamingStyle style)
=> $"{style.Name} prefix='{style.Prefix}' suffix='{style.Suffix}' separator='{style.WordSeparator}'";

public static string Inspect(this SymbolSpecification symbol)
=> $"{symbol.Name} {Inspect(symbol.ApplicableSymbolKindList)} {Inspect(symbol.ApplicableAccessibilityList)} {Inspect(symbol.RequiredModifierList)}";

public static string Inspect<T>(ImmutableArray<T> items) where T : notnull
=> string.Join(",", items.Select(item => item.ToString()));

public static string Inspect(this NamingStylePreferences preferences, string[]? excludeNodes = null)
{
var xml = preferences.CreateXElement();
Expand Down Expand Up @@ -43,6 +57,7 @@ public static string Inspect(this NamingStylePreferences preferences, string[]?
if (!guidMap.TryGetValue(guid, out var existingOrdinal))
{
existingOrdinal = ordinal++;
guidMap.Add(guid, existingOrdinal);
}

attribute.Value = existingOrdinal.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ Solution UpdateOptions(Solution oldSolution)
language,
entryWriter: (name, value) => lazyBuilder[name] = value,
triviaWriter: null,
// lower priority than any that would be specified explicitly in editorconfig:
priority: int.MaxValue - preferences.NamingRules.Length);
setPrioritiesToPreserveOrder: true);
}
else
{
Expand Down
Loading
Loading