<commit_before>﻿namespace WpfAnalyzers.Test.WPF0011ContainingTypeShouldBeRegisteredOwnerTests
{
    using Gu.Roslyn.Asserts;
    using NUnit.Framework;

    public static class CodeFix
    {
        private static readonly WPF0011ContainingTypeShouldBeRegisteredOwner Analyzer = new();
        private static readonly UseContainingTypeFix Fix = new();
        private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.WPF0011ContainingTypeShouldBeRegisteredOwner);

        [Test]
        public static void Message()
        {
            var barControlCode = @"
namespace N
{
    using System.Windows.Controls;

    public class BarControl : Control
    {
    }
}";

            var code = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        // registering for an owner that is not containing type.
        private static readonly DependencyPropertyKey BarPropertyKey = DependencyProperty.RegisterReadOnly(
            ""Bar"",
            typeof(int),
            ↓typeof(BarControl),
            new PropertyMetadata(default(int)));

        public static readonly DependencyProperty BarProperty = BarPropertyKey.DependencyProperty;

        public int Bar
        {
            get { return (int)this.GetValue(BarProperty); }
            protected set {  this.SetValue(BarPropertyKey, value);}
        }
    }
}";

            RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Register containing type: 'N.FooControl' as owner"), barControlCode, code);
        }

        [TestCase("BarControl")]
        [TestCase("BarControl<T>")]
        public static void DependencyPropertyRegister(string typeName)
        {
            var barControlCode = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class BarControl : Control
    {
    }
}".AssertReplace("class BarControl", $"class {typeName}");

            var before = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        // registering for an owner that is not containing type.
        public static readonly DependencyProperty BarProperty = DependencyProperty.Register(
            nameof(Bar),
            typeof(int),
            ↓typeof(BarControl),
            new PropertyMetadata(default(int)));

        public int Bar
        {
            get { return (int)this.GetValue(BarProperty); }
            set { this.SetValue(BarProperty, value); }
        }
    }
}".AssertReplace("typeof(BarControl)", $"typeof({typeName.AssertReplace("<T>", "<int>")})");

            var after = @"
namespace N
{
    using System.Windows;<commit_msg>Fix test.<commit_after>﻿namespace WpfAnalyzers.Test.WPF0011ContainingTypeShouldBeRegisteredOwnerTests
{
    using Gu.Roslyn.Asserts;
    using NUnit.Framework;

    public static class CodeFix
    {
        private static readonly WPF0011ContainingTypeShouldBeRegisteredOwner Analyzer = new();
        private static readonly UseContainingTypeFix Fix = new();
        private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.WPF0011ContainingTypeShouldBeRegisteredOwner);

        [Test]
        public static void Message()
        {
            var barControlCode = @"
namespace N
{
    using System.Windows.Controls;

    public class BarControl : Control
    {
    }
}";

            var code = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        // registering for an owner that is not containing type.
        private static readonly DependencyPropertyKey BarPropertyKey = DependencyProperty.RegisterReadOnly(
            ""Bar"",
            typeof(int),
            ↓typeof(BarControl),
            new PropertyMetadata(default(int)));

        public static readonly DependencyProperty BarProperty = BarPropertyKey.DependencyProperty;

        public int Bar
        {
            get { return (int)this.GetValue(BarProperty); }
            protected set {  this.SetValue(BarPropertyKey, value);}
        }
    }
}";

            RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Register containing type: 'N.FooControl' as owner"), barControlCode, code);
        }

        [TestCase("BarControl")]
        [TestCase("BarControl<T>")]
        public static void DependencyPropertyRegister(string typeName)
        {
            var barControlCode = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class BarControl : Control
    {
    }
}".AssertReplace("class BarControl", $"class {typeName}");

            var before = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : Control
    {
        // registering for an owner that is not containing type.
        public static readonly DependencyProperty BarProperty = DependencyProperty.Register(
            nameof(Bar),
            typeof(int),
            ↓typeof(BarControl),
            new PropertyMetadata(default(int)));

        public int Bar
        {
            get { return (int)this.GetValue(BarProperty); }
            set { this.SetValue(BarProperty, value); }
        }
    }
}".AssertReplace("typeof(BarControl)", $"typeof({typeName.Replace("<T>", "<int>")})");

            var after = @"
namespace N
{
    using System.Windows;
{'fraction_non_alphanumeric': 0.09142130134496547, 'fraction_numerical': 0.004362050163576881, 'mean_word_length': 2.5850162866449513, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7153490', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1383, 'n_words': 346}