Example #1
0
        public static void StartingAndEndingWithNewLine()
        {
            var before = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""↓\na\n"";
        }
    }
}";

            var after = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""\n"" +
                       ""a\n"";
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #2
0
        public static void TwoNewLinesEndingWithNewline()
        {
            var before = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""↓a\nb\nc\n"";
        }
    }
}";

            var after = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""a\n"" +
                       ""b\n"" +
                       ""c\n"";
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #3
0
        public void Refactoring_NoPropertyKey_AddsKey()
        {
            const string code      = @"
using System;
namespace Tests
{
    class Thing
    {
        public int Id { get; set; }
        public Thing Related {↓ get; set; }
    }
}
";
            const string fixedCode = @"
using System;
namespace Tests
{
    class Thing
    {
        public int Id { get; set; }
        public Thing Related { get; set; }
        public int RelatedId { get; set; }
    }
}
";

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #4
0
            public static void WithSpanWhenAfterDoesNotMatch()
            {
                var before = @"
class c
{
}";

                var after = @"
class WRONG
{
}";

                var refactoring = new ClassNameToUpperCaseRefactoringProvider();
                var exception   = Assert.Throws <AssertException>(() => RoslynAssert.Refactoring(refactoring, before, new TextSpan(8, 3), after));
                var expected    = @"Mismatch on line 2 of file WRONG.cs.
Expected: class WRONG
Actual:   class C
                ^
Expected:

class WRONG
{
}
Actual:

class C
{
}
";

                CodeAssert.AreEqual(expected, exception.Message);
                exception = Assert.Throws <AssertException>(() => RoslynAssert.Refactoring(refactoring, before, new TextSpan(8, 3), after, title: "To uppercase"));
                CodeAssert.AreEqual(expected, exception.Message);
            }
Example #5
0
        public void Refactoring_WithoutTrackingStatements_AddsTracking(string tracking)
        {
            var code      = TestCode("context.Things↓.ToList();");
            var fixedCode = TestCode($"context.Things.{tracking}().ToList();");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode, TitleFor(tracking));
        }
Example #6
0
        public static void TwoCarriageReturnNewLinesAndEndingWithCarriageReturnNewLine()
        {
            var before = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""↓a\r\nb\r\nc\r\n"";
        }
    }
}";

            var after = @"
namespace N
{
    public static class C
    {
        public static void M()
        {
            var text = ""a\r\n"" +
                       ""b\r\n"" +
                       ""c\r\n"";
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #7
0
        public void Refactoring_WithTrackingStatements_ReplacesLastInvocation(string otherTracking, string targetTracking)
        {
            var code      = TestCode($"context.Things↓.{targetTracking}().{otherTracking}().ToList();");
            var fixedCode = TestCode($"context.Things.{targetTracking}().{targetTracking}().ToList();");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode, TitleFor(targetTracking));
        }
        public static void ChangeToReadOnly()
        {
            var before = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class C : Control
    {
        /// <summary>Identifies the <see cref=""Number""/> dependency property.</summary>
        public static readonly DependencyProperty ↓NumberProperty = DependencyProperty.Register(
            nameof(Number),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        public int Number
        {
            get => (int)this.GetValue(NumberProperty);
            set => this.SetValue(NumberProperty, value);
        }
    }
}";

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

    public class C : Control
    {
        private static readonly DependencyPropertyKey NumberPropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Number),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Number""/> dependency property.</summary>
        public static readonly DependencyProperty NumberProperty = NumberPropertyKey.DependencyProperty;

        public int Number
        {
            get => (int)this.GetValue(NumberProperty);
            set => this.SetValue(NumberPropertyKey, value);
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #9
0
        public void Refactoring_WithReadOnlyDto_DoesNotMapReadOnlyProperty()
        {
            var code      = TestCode(@"
Enumerable.Empty<SimpleEntity>().Select(x => new SimpleEntityReadOnlyIdDto
{↓
});");
            var fixedCode = TestCode(@"
Enumerable.Empty<SimpleEntity>().Select(x => new SimpleEntityReadOnlyIdDto
{
    Name = x.Name
});"); // Id is a readonly property in the dto

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #10
0
        public void Refactoring_Collection_MapsAndAddsToList()
        {
            var code      = TestCode(@"
Enumerable.Empty<CollectionEntity>().Select(x => new CollectionEntityDto
{↓
});");
            var fixedCode = TestCode(@"
Enumerable.Empty<CollectionEntity>().Select(x => new CollectionEntityDto
{
    Worlds = x.Worlds.ToList()
});");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #11
0
        public void Refactoring_WithSetOnlyEntity_DoesNotMapSetOnlyProperty()
        {
            var code      = TestCode(@"
Enumerable.Empty<SetOnlyNameEntity>().Select(x => new SetOnlyNameEntity
{↓
});");
            var fixedCode = TestCode(@"
Enumerable.Empty<SetOnlyNameEntity>().Select(x => new SetOnlyNameEntity
{
    Id = x.Id
});"); // Id is a readonly property in the dto

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #12
0
        public void Refactoring_ChildThingToChildThingDto_MapsAllProperties()
        {
            var code      = TestCode(@"
context.Things.OfType<ChildThing>().Select(x => new ChildThingDto
{↓
});");
            var fixedCode = TestCode(@"
context.Things.OfType<ChildThing>().Select(x => new ChildThingDto
{
    Id = x.Id,
    ChildProperty = x.ChildProperty
});");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #13
0
        public void Refactoring_WithParenthesizedLambda_MapsAllProperties()
        {
            var code      = TestCode(@"
Enumerable.Empty<SimpleEntity>().Select((x) => new SimpleEntityDto
{↓
});");
            var fixedCode = TestCode(@"
Enumerable.Empty<SimpleEntity>().Select((x) => new SimpleEntityDto
{
    Id = x.Id,
    Name = x.Name
});");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #14
0
        public void Refactoring_ThingToThingDto_MapsAllProperties()
        {
            var code      = TestCode(@"
context.Things.Select(x => new ThingDto
{↓
});");
            var fixedCode = TestCode(@"
context.Things.Select(x => new ThingDto
{
    Id = x.Id,
    Name = x.Name,
    IsGood = x.IsGood
});");

            RoslynAssert.Refactoring(Refactoring, code, fixedCode);
        }
Example #15
0
            public static void WithSpan()
            {
                var before = @"
class c
{
}";

                var after = @"
class C
{
}";

                var refactoring = new ClassNameToUpperCaseRefactoringProvider();

                RoslynAssert.Refactoring(refactoring, before, new TextSpan(8, 3), after);
                RoslynAssert.Refactoring(refactoring, before, new TextSpan(8, 3), after, title: "To uppercase");
            }
Example #16
0
            public static void WithPositionIndicated()
            {
                var before = @"
class ↓c
{
}";

                var after = @"
class C
{
}";

                var refactoring = new ClassNameToUpperCaseRefactoringProvider();

                RoslynAssert.Refactoring(refactoring, before, after);
                RoslynAssert.Refactoring(refactoring, before, after, title: "To uppercase");
            }
Example #17
0
        public static void StaticAutoProperty2()
        {
            var before = @"
namespace N
{
    public static class Foo
    {
        public static double ↓Value { get; set; }
    }
}";

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

    public static class Foo
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached(
            ""Value"",
            typeof(double),
            typeof(Foo),
            new PropertyMetadata(default(double)));

        /// <summary>Helper for getting <see cref=""ValueProperty""/> from <paramref name=""element""/>.</summary>
        /// <param name=""element""><see cref=""DependencyObject""/> to read <see cref=""ValueProperty""/> from.</param>
        /// <returns>Value property value.</returns>
        [AttachedPropertyBrowsableForType(typeof(DependencyObject))]
        public static double GetValue(DependencyObject element)
        {
            return (double)element.GetValue(ValueProperty);
        }

        /// <summary>Helper for setting <see cref=""ValueProperty""/> on <paramref name=""element""/>.</summary>
        /// <param name=""element""><see cref=""DependencyObject""/> to set <see cref=""ValueProperty""/> on.</param>
        /// <param name=""value"">Value property value.</param>
        public static void SetValue(DependencyObject element, double value)
        {
            element.SetValue(ValueProperty, value);
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #18
0
        public static void AutoPropertyNotQualifiedMethodAccess()
        {
            var before = @"
namespace N
{
    using System.Windows.Controls;

    public class C : Control
    {
        public int ↓Number { get; set; }

        public int M() => M();
    }
}";

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

    public class C : Control
    {
        /// <summary>Identifies the <see cref=""Number""/> dependency property.</summary>
        public static readonly DependencyProperty NumberProperty = DependencyProperty.Register(
            nameof(Number),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        public int Number
        {
            get => (int)GetValue(NumberProperty);
            set => SetValue(NumberProperty, value);
        }

        public int M() => M();
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #19
0
        public static void AutoProperty2()
        {
            var before = @"
namespace N
{
    using System.Windows.Controls;

    public class FooControl : Control
    {
        public double? ↓Value { get; set; }
    }
}";

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

    public class FooControl : Control
    {
        /// <summary>Identifies the <see cref=""Value""/> dependency property.</summary>
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            nameof(Value),
            typeof(double?),
            typeof(FooControl),
            new PropertyMetadata(default(double?)));

        public double? Value
        {
            get => (double?)this.GetValue(ValueProperty);
            set => this.SetValue(ValueProperty, value);
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after);
        }
Example #20
0
        public static void InsertsReadonlyBackingFieldsAtCorrectPosition()
        {
            var before = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class C : Control
    {
        private static readonly DependencyPropertyKey Value1PropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Value1),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Value1""/> dependency property.</summary>
        public static readonly DependencyProperty Value1Property = Value1PropertyKey.DependencyProperty;

        private static readonly DependencyPropertyKey Value3PropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Value3),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Value3""/> dependency property.</summary>
        public static readonly DependencyProperty Value3Property = Value3PropertyKey.DependencyProperty;

        public int Value1
        {
            get => (int)this.GetValue(Value1Property);
            private set => this.SetValue(Value1PropertyKey, value);
        }

        public int ↓Value2 { get; private set; }

        public int Value3
        {
            get => (int)this.GetValue(Value3Property);
            private set => this.SetValue(Value3PropertyKey, value);
        }
    }
}";

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

    public class C : Control
    {
        private static readonly DependencyPropertyKey Value1PropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Value1),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Value1""/> dependency property.</summary>
        public static readonly DependencyProperty Value1Property = Value1PropertyKey.DependencyProperty;

        private static readonly DependencyPropertyKey Value2PropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Value2),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Value2""/> dependency property.</summary>
        public static readonly DependencyProperty Value2Property = Value2PropertyKey.DependencyProperty;

        private static readonly DependencyPropertyKey Value3PropertyKey = DependencyProperty.RegisterReadOnly(
            nameof(Value3),
            typeof(int),
            typeof(C),
            new PropertyMetadata(default(int)));

        /// <summary>Identifies the <see cref=""Value3""/> dependency property.</summary>
        public static readonly DependencyProperty Value3Property = Value3PropertyKey.DependencyProperty;

        public int Value1
        {
            get => (int)this.GetValue(Value1Property);
            private set => this.SetValue(Value1PropertyKey, value);
        }

        public int Value2
        {
            get => (int)this.GetValue(Value2Property);
            private set => this.SetValue(Value2PropertyKey, value);
        }

        public int Value3
        {
            get => (int)this.GetValue(Value3Property);
            private set => this.SetValue(Value3PropertyKey, value);
        }
    }
}";

            RoslynAssert.Refactoring(Refactoring, before, after, title: "Change to readonly dependency property");
        }