Beispiel #1
0
            public async Task Conversion_operators_that_are_expression_bodied_can_be_mutated()
            {
                var input = await CreateInput <ConversionOperatorDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public class Point
                    {
                        public int x, y;
    
                        public Point(int x, int y)
                        {
                            this.x = x;
                            this.y = y;
                        }

                        public static implicit operator string (Point p) => x.ToString() + "","" + y.ToString();
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource2 = SourceOfInstrumentedMember <ConversionOperatorDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource2[0], Does.Contain("public static implicit operator string (Point p)"));
                Assert.That(instrumentedMethodSource2[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource2[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource2[3], Does.Contain(@"   return x.ToString() + "","" + y.ToString();"));
                Assert.That(instrumentedMethodSource2[4], Does.Contain("}"));
            }
Beispiel #2
0
            public async Task Operators_that_are_expression_bodied_can_be_mutated()
            {
                var input = await CreateInput <OperatorDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public class Point
                    {
                        public int x, y;
    
                        public Point(int x, int y)
                        {
                            this.x = x;
                            this.y = y;
                        }

                        public static Point operator *(Point a, Point b) => new Point(a.x * b.x, a.y * b.y);
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <OperatorDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public static Point operator *(Point a, Point b)"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("   return new Point(a.x * b.x, a.y * b.y);"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
Beispiel #3
0
            public async Task Normal_methods_that_are_empty_can_be_instrumented()
            {
                var input = await CreateInput <MethodDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public static class DummyClass
                    {
                        public static void DoNothing()
                        {
                        }
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <MethodDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public static void DoNothing()"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("}"));
            }
Beispiel #4
0
            public async Task Constructors_that_are_expression_bodied_can_be_mutated()
            {
                var input = await CreateInput <ConstructorDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public class DummyClass
                    {
                        private readonly int thing;

                        public DummyClass(int a) => this.thing = a + 1;
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <ConstructorDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public DummyClass(int a)"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("   this.thing = a + 1;"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
Beispiel #5
0
            public async Task Destructors_can_be_mutated()
            {
                var input = await CreateInput <DestructorDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public class DummyClass
                    {
                        ~DummyClass()
                        {
                            System.Console.WriteLine(""bye!"");
                        }
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <DestructorDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("~DummyClass()"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("   System.Console.WriteLine(\"bye!\");"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
            public async Task Normal_properties_with_only_a_setter_can_be_instrumented()
            {
                var input = await CreateInput <PropertyDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public static class DummyClass
                    {
                        private int magicNumber = 41;

                        public int MagicNumber
                        {            
                            set { magicNumber = value + 1; }
                        }
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <PropertyDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public int MagicNumber"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain("set"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[4], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[5], Does.Contain("    magicNumber = value + 1;"));
                Assert.That(instrumentedMethodSource[6], Does.Contain("}"));
            }
Beispiel #7
0
            public async Task Partial_methods_can_be_instrumented()
            {
                var input = await CreateInput <MethodDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public partial class DummyClass
                    {
                        public partial void MagicNumber(int a);
                    }

                    public partial class DummyClass
                    {
                        public partial void MagicNumber(int a)
                        {
                            return 40 + 2;
                        }
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <MethodDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public partial void MagicNumber(int a)"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("   return 40 + 2;"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
Beispiel #8
0
        public async Task The_callback_is_called_once_for_each_instrumented_method()
        {
            var input = await CreateInput <MemberDeclarationSyntax>(@"
            namespace DummyNamespace
            {
                public class DummyClass
                {
                    public int MethodA(int a) { return 42; }
                    public void MethodB() { }
                    public int MethodC() => 42;
                    public void MethodD(string s) => System.Console.WriteLine(s);
                }
            }");

            var received = new List <Tuple <string, string> >();

            await InstrumentationImpl.InstrumentDocument(
                input.OriginalSyntaxTree,
                input.OriginalDocument,
                (methodId, fullMethodName) => received.Add(new Tuple <string, string>(methodId, fullMethodName)),
                () => 0);

            Assert.That(received.Count, Is.EqualTo(4));
            Assert.That(received[0].Item2, Does.Contain("MethodA"));
            Assert.That(received[1].Item2, Does.Contain("MethodB"));
            Assert.That(received[2].Item2, Does.Contain("MethodC"));
            Assert.That(received[3].Item2, Does.Contain("MethodD"));
        }
        public async Task Event_properties_can_be_instrumented()
        {
            var input = await CreateInput <EventDeclarationSyntax>(@"
            namespace DummyNamespace
            {
                using System;

                public class DummyClass
                {
                    private event EventHandler<EventArgs> someEvent;

                    public event EventHandler<EventArgs> SomeEvent
                    {
                        add { someEvent += value; }
                        remove { someEvent -= value; }
                    }
                }
            }");

            Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

            var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

            var instrumentedMethodSource = SourceOfInstrumentedMember <EventDeclarationSyntax>(instrumentedSyntaxTree);

            Assert.That(instrumentedMethodSource[0], Does.Contain("public event EventHandler<EventArgs> SomeEvent"));
            Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
            Assert.That(instrumentedMethodSource[2], Does.Contain("add"));
            Assert.That(instrumentedMethodSource[3], Does.Contain("{"));
            Assert.That(instrumentedMethodSource[4], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
            Assert.That(instrumentedMethodSource[5], Does.Contain("    someEvent += value;"));
            Assert.That(instrumentedMethodSource[6], Does.Contain("}"));
            Assert.That(instrumentedMethodSource[8], Does.Contain("remove"));
            Assert.That(instrumentedMethodSource[9], Does.Contain("{"));
            Assert.That(instrumentedMethodSource[10], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
            Assert.That(instrumentedMethodSource[11], Does.Contain("    someEvent -= value;"));
            Assert.That(instrumentedMethodSource[12], Does.Contain("}"));
        }
            public async Task Indexers_with_expression_bodies_accessors_can_be_instrumented()
            {
                var input = await CreateInput <IndexerDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public class DummyClass
                    {
                        private int[] arr = new int[100];

                        public int this[int i]
                        {
                            get => arr[i];
                            set => arr[i] = value;
                        }
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <IndexerDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public int this[int i]"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain("get"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[4], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[5], Does.Contain("    return arr[i];"));
                Assert.That(instrumentedMethodSource[6], Does.Contain("}"));
                Assert.That(instrumentedMethodSource[8], Does.Contain("set"));
                Assert.That(instrumentedMethodSource[9], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[10], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[11], Does.Contain("    arr[i] = value;"));
                Assert.That(instrumentedMethodSource[12], Does.Contain("}"));
            }
Beispiel #11
0
            public async Task Expression_bodied_methods_with_return_type_that_is_not_predefined_type_can_be_instrumented()
            {
                var input = await CreateInput <MethodDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public static class DummyClass
                    {
                        public Task<System.Generic.List<int>> GetThings() => new[]{1,2,3}.ToList();        
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <MethodDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public Task<System.Generic.List<int>> GetThings()"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("   return new[]{1, 2, 3}.ToList();"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
Beispiel #12
0
            public async Task Expression_bodied_methods_that_do_not_return_a_value_can_be_instrumented()
            {
                var input = await CreateInput <MethodDeclarationSyntax>(@"
                namespace DummyNamespace
                {
                    public static class DummyClass
                    {
                        public void DoSomething() => System.Console.WriteLine(""hello"");
                    }
                }");

                Assert.That(input.MemberToInstrument.CanInstrument(), Is.True);

                var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                    input.OriginalSyntaxTree, input.OriginalDocument, (_, __) => {}, () => 0);

                var instrumentedMethodSource = SourceOfInstrumentedMember <MethodDeclarationSyntax>(instrumentedSyntaxTree);

                Assert.That(instrumentedMethodSource[0], Does.Contain("public void DoSomething()"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("    System.Console.WriteLine(\"hello\");"));
                Assert.That(instrumentedMethodSource[4], Does.Contain("}"));
            }
Beispiel #13
0
        public async Task The_generated_member_id_is_used()
        {
            var input = await CreateInput <MemberDeclarationSyntax>(@"
            namespace DummyNamespace
            {
                public class DummyClass
                {
                    public int MethodA(int a) { return 42; }
                }
            }");

            var instrumentedSyntaxTree = await InstrumentationImpl.InstrumentDocument(
                input.OriginalSyntaxTree,
                input.OriginalDocument,
                (_, __) => {},
                () => 12345);

            var instrumentedMethodSource = SourceOfInstrumentedMember <MethodDeclarationSyntax>(instrumentedSyntaxTree);

            Assert.That(instrumentedMethodSource[0], Does.Contain("public int MethodA(int a)"));
            Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
            Assert.That(instrumentedMethodSource[2], Does.Contain(
                            $"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}12345"));
        }
        public async Task Properties_where_getter_and_setters_are_mixture_of_auto_and_normal_can_be_instrumented()
        {
            var input1 = await CreateInput <PropertyDeclarationSyntax>(@"
            namespace DummyNamespace
            {
                public static class DummyClass
                {
                    private static int magicNumber = 41;

                    public static int MagicNumber
                    {
                        get { return magicNumber + 1; }
                        set => magicNumber = value + 1;
                    }
                }
            }");

            var input2 = await CreateInput <PropertyDeclarationSyntax>(@"
            namespace DummyNamespace
            {
                public static class DummyClass
                {
                    private static int magicNumber = 41;

                    public static int MagicNumber
                    {
                        get => magicNumber + 1;
                        set { magicNumber = value + 1; }
                    }
                }
            }");

            Assert.That(input1.MemberToInstrument.CanInstrument(), Is.True);
            Assert.That(input2.MemberToInstrument.CanInstrument(), Is.True);

            var instrumentedSyntaxTree1 = await InstrumentationImpl.InstrumentDocument(
                input1.OriginalSyntaxTree, input1.OriginalDocument, (_, __) => {}, () => 0);

            var instrumentedSyntaxTree2 = await InstrumentationImpl.InstrumentDocument(
                input2.OriginalSyntaxTree, input2.OriginalDocument, (_, __) => {}, () => 0);

            var instrumentedMethodSources = new[]
            {
                SourceOfInstrumentedMember <PropertyDeclarationSyntax>(instrumentedSyntaxTree1),
                SourceOfInstrumentedMember <PropertyDeclarationSyntax>(instrumentedSyntaxTree2)
            };

            foreach (var instrumentedMethodSource in instrumentedMethodSources)
            {
                Assert.That(instrumentedMethodSource[0], Does.Contain("public static int MagicNumber"));
                Assert.That(instrumentedMethodSource[1], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[2], Does.Contain("get"));
                Assert.That(instrumentedMethodSource[3], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[4], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[5], Does.Contain("    return magicNumber + 1;"));
                Assert.That(instrumentedMethodSource[6], Does.Contain("}"));
                Assert.That(instrumentedMethodSource[8], Does.Contain("set"));
                Assert.That(instrumentedMethodSource[9], Does.Contain("{"));
                Assert.That(instrumentedMethodSource[10], Does.Contain($"   System.Console.WriteLine(\"{InstrumentationImpl.CoverageOutputLinePrefix}"));
                Assert.That(instrumentedMethodSource[11], Does.Contain("    magicNumber = value + 1;"));
                Assert.That(instrumentedMethodSource[12], Does.Contain("}"));
            }
        }