public static void TestEmptyDictionaryReturnNoNamingStylePreferencesObjectReturnsFalse()
        {
            var editorConfigStorageLocation = new NamingStylePreferenceEditorConfigStorageLocation();
            var result = editorConfigStorageLocation.TryGetOption(StructuredAnalyzerConfigOptions.Create(DictionaryAnalyzerConfigOptions.EmptyDictionary), typeof(NamingStylePreferences), out _);

            Assert.False(result, "Expected TryParseReadonlyDictionary to return 'false' for empty dictionary");
        }
        public static void TestObjectTypeThrowsInvalidOperationException()
        {
            var editorConfigStorageLocation = new NamingStylePreferenceEditorConfigStorageLocation();

            Assert.Throws <InvalidOperationException>(() =>
            {
                editorConfigStorageLocation.TryGetOption(StructuredAnalyzerConfigOptions.Create(DictionaryAnalyzerConfigOptions.EmptyDictionary), typeof(object), out var @object);
            });
        }
Exemple #3
0
        public void TestParseEditorConfigEndOfLine(string configurationString, string newLine)
        {
            var storageLocation = FormattingOptions.NewLine.StorageLocations
                                  .OfType <EditorConfigStorageLocation <string> >()
                                  .Single();
            var allRawConventions = StructuredAnalyzerConfigOptions.Create(DictionaryAnalyzerConfigOptions.EmptyDictionary.Add(storageLocation.KeyName, configurationString));

            Assert.True(storageLocation.TryGetOption(allRawConventions, typeof(string), out var parsedNewLine));
            Assert.Equal(newLine, (string?)parsedNewLine);
        }
Exemple #4
0
        public bool TryGetOption(StructuredAnalyzerConfigOptions options, Type type, out object result)
        {
            if (type == typeof(NamingStylePreferences))
            {
                var preferences = options.GetNamingStylePreferences();
                result = preferences;
                return(!preferences.IsEmpty);
            }

            throw ExceptionUtilities.UnexpectedValue(type);
        }
Exemple #5
0
        public bool TryGetOption(StructuredAnalyzerConfigOptions options, Type type, out object?result)
        {
            if (options.TryGetValue(KeyName, out var value))
            {
                var ret = TryGetOption(value, type, out var typedResult);
                result = typedResult;
                return(ret);
            }

            result = null;
            return(false);
        }
Exemple #6
0
        public void TestParseEditorConfigAccessibilityModifiers(string args, int value, ReportDiagnostic severity)
        {
            var storageLocation = CodeStyleOptions2.RequireAccessibilityModifiers.StorageLocations
                                  .OfType <EditorConfigStorageLocation <CodeStyleOption2 <AccessibilityModifiersRequired> > >()
                                  .Single();
            var allRawConventions = StructuredAnalyzerConfigOptions.Create(DictionaryAnalyzerConfigOptions.EmptyDictionary.Add(storageLocation.KeyName, args));

            Assert.True(storageLocation.TryGetOption(allRawConventions, typeof(CodeStyleOption2 <AccessibilityModifiersRequired>), out var parsedCodeStyleOption));
            var codeStyleOption = (CodeStyleOption2 <AccessibilityModifiersRequired>)parsedCodeStyleOption !;

            Assert.Equal((AccessibilityModifiersRequired)value, codeStyleOption.Value);
            Assert.Equal(severity, codeStyleOption.Notification.Severity);
        }
        public static void TestNonEmptyDictionaryReturnsTrue()
        {
            var editorConfigStorageLocation = new NamingStylePreferenceEditorConfigStorageLocation();
            var options = StructuredAnalyzerConfigOptions.Create(new Dictionary <string, string>()
            {
                ["dotnet_naming_rule.methods_and_properties_must_be_pascal_case.severity"]       = "error",
                ["dotnet_naming_rule.methods_and_properties_must_be_pascal_case.symbols"]        = "method_and_property_symbols",
                ["dotnet_naming_rule.methods_and_properties_must_be_pascal_case.style"]          = "pascal_case_style",
                ["dotnet_naming_symbols.method_and_property_symbols.applicable_kinds"]           = "method,property",
                ["dotnet_naming_symbols.method_and_property_symbols.applicable_accessibilities"] = "*",
                ["dotnet_naming_style.pascal_case_style.capitalization"] = "pascal_case"
            }.ToImmutableDictionary(AnalyzerConfigOptions.KeyComparer));

            var result = editorConfigStorageLocation.TryGetOption(options, typeof(NamingStylePreferences), out var value);

            Assert.True(result, "Expected non-empty dictionary to return true");
            var namingStylePreferences = Assert.IsAssignableFrom <NamingStylePreferences>(value);

            Assert.Equal(ReportDiagnostic.Error, namingStylePreferences.Rules.NamingRules[0].EnforcementLevel);
        }
Exemple #8
0
        private static bool TryGetEditorConfigOption <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, Optional <T?> defaultValue, out T?value)
        {
            var hasEditorConfigStorage = false;

            foreach (var storageLocation in option.StorageLocations)
            {
                // This code path will avoid allocating a Dictionary wrapper since we can get direct access to the KeyName.
                if (storageLocation is EditorConfigStorageLocation <T> editorConfigStorageLocation &&
                    analyzerConfigOptions.TryGetValue(editorConfigStorageLocation.KeyName, out var stringValue) &&
                    editorConfigStorageLocation.TryGetOption(stringValue, typeof(T), out value))
                {
                    return(true);
                }

                if (storageLocation is not IEditorConfigStorageLocation configStorageLocation)
                {
                    continue;
                }

                // This option has .editorconfig storage defined, even if the current configuration does not provide a
                // value for it.
                hasEditorConfigStorage = true;
                if (StructuredAnalyzerConfigOptions.TryGetStructuredOptions(analyzerConfigOptions, out var structuredOptions) &&
                    configStorageLocation.TryGetOption(structuredOptions, option.Type, out var objectValue))
                {
                    value = (T?)objectValue;
                    return(true);
                }
            }

            if (defaultValue.HasValue)
            {
                value = defaultValue.Value;
                return(hasEditorConfigStorage);
            }
            else
            {
                value = default;
                return(false);
            }
        }
 public AnalyzerConfigData(AnalyzerConfigOptionsResult result)
 {
     ConfigOptions   = StructuredAnalyzerConfigOptions.Create(result.AnalyzerOptions);
     AnalyzerOptions = result.AnalyzerOptions;
     TreeOptions     = result.TreeOptions;
 }