From 93497fb5336368f92fd0b0d2d196217e6a2a4365 Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Sat, 2 Jan 2021 15:35:11 +0100 Subject: [PATCH] Test for #292 --- .../Valid.cs | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/WpfAnalyzers.Test/WPF0004ClrMethodShouldMatchRegisteredNameTests/Valid.cs b/WpfAnalyzers.Test/WPF0004ClrMethodShouldMatchRegisteredNameTests/Valid.cs index ae300e38..b69d5984 100644 --- a/WpfAnalyzers.Test/WPF0004ClrMethodShouldMatchRegisteredNameTests/Valid.cs +++ b/WpfAnalyzers.Test/WPF0004ClrMethodShouldMatchRegisteredNameTests/Valid.cs @@ -1,4 +1,4 @@ -namespace WpfAnalyzers.Test.WPF0004ClrMethodShouldMatchRegisteredNameTests +namespace WpfAnalyzers.Test.WPF0004ClrMethodShouldMatchRegisteredNameTests { using Gu.Roslyn.Asserts; using NUnit.Framework; @@ -461,5 +461,78 @@ public static int Meh(FrameworkElement element) RoslynAssert.Valid(Analyzer, code); } + + [Test] + public static void AttachedPropertyNullableEnumerable() + { + var binary = MetadataReferences.CreateBinary(@" +namespace BinaryReference +{ + using System.Collections; + using System.Windows; + using System.Windows.Controls; + + public static class DataGridExt + { + /// + /// An of rows where each row is an with the values. + /// + public static readonly DependencyProperty EnumerableProperty = DependencyProperty.RegisterAttached( + ""Enumerable"", + typeof(IEnumerable), + typeof(DataGridExt), + new PropertyMetadata(default(IEnumerable))); + + /// Helper for setting on . + /// to set on. + /// Enumerable property value. + public static void SetEnumerable(this DataGrid element, IEnumerable? value) + { + if (element is null) + { + throw new System.ArgumentNullException(nameof(element)); + } + + element.SetValue(EnumerableProperty, value); + } + + /// Helper for getting from . + /// to read from. + /// Enumerable property value. + [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] + [AttachedPropertyBrowsableForType(typeof(DataGrid))] + public static IEnumerable? GetEnumerable(this DataGrid element) + { + if (element is null) + { + throw new System.ArgumentNullException(nameof(element)); + } + + return (IEnumerable)element.GetValue(EnumerableProperty); + } + } +}"); + var code = @" +namespace N +{ + using System.Collections.ObjectModel; + using System.Windows.Controls; + + using BinaryReference; + + public static class C + { + public static DataGrid M1() + { + var dataGrid = new DataGrid(); + var xs = new ObservableCollection(new[] { 1, 2 }); + dataGrid.SetValue(DataGridExt.EnumerableProperty, xs); + return dataGrid; + } + } +}"; + + RoslynAssert.Valid(Analyzer, code, metadataReferences:MetadataReferences.FromAttributes().Add(binary)); + } } }