Пример #1
0
            public void Should_Throw_If_Method_Is_Null()
            {
                // Given, When
                var result = Record.Exception(() => MethodAliasGenerator.Generate(null));

                // Then
                Assert.IsArgumentNullException(result, "method");
            }
            public void Should_Throw_If_Method_Is_Null()
            {
                // Given, When
                var result = Record.Exception(() => MethodAliasGenerator.Generate(null));

                // Then
                Assert.IsType <ArgumentNullException>(result);
                Assert.Equal("method", ((ArgumentNullException)result).ParamName);
            }
Пример #3
0
        private static string GetAliasCode(Script context)
        {
            var result = new List <string>();

            foreach (var alias in context.Aliases)
            {
                result.Add(alias.Type == ScriptAliasType.Method
                    ? MethodAliasGenerator.Generate(alias.Method)
                    : PropertyAliasGenerator.Generate(alias.Method));
            }
            return(string.Join("\r\n", result));
        }
Пример #4
0
            public void Should_Throw_If_Method_Is_Not_An_Cake_Script_Method()
            {
                // Given
                var method = typeof(MethodAliasGeneratorFixture).GetMethod("NotAScriptMethod");

                // When
                var result = Record.Exception(() => MethodAliasGenerator.Generate(method));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal("The method 'NotAScriptMethod' is not a method alias.",
                             result.Message);
            }
Пример #5
0
            public void Should_Throw_If_Declaring_Type_Is_Not_Static()
            {
                // Given
                var method = GetType().GetMethod("Should_Throw_If_Declaring_Type_Is_Not_Static");

                // When
                var result = Record.Exception(() => MethodAliasGenerator.Generate(method));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal("The type 'Cake.Core.Tests.Unit.Scripting.CodeGen.MethodAliasGeneratorTests+TheGeneratorMethod' is not static.",
                             result.Message);
            }
Пример #6
0
            public void Should_Return_Correctly_Generated_Wrapper_For_Non_Generic_Type_With_Generic_Argument()
            {
                const string expected = "public void NonGeneric_ExtensionMethodWithGenericParameter(System.Action<System.Int32> value){" +
                                        "Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.NonGeneric_ExtensionMethodWithGenericParameter" +
                                        "(Context, value);}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethod("NonGeneric_ExtensionMethodWithGenericParameter");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
Пример #7
0
            public void Should_Return_Correctly_Generated_Wrapper_For_Generic_Type_With_Generic_Return_Value()
            {
                const string expected = "public TTest Generic_ExtensionMethodWithGenericReturnValue<TTest>(TTest value){" +
                                        "return Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.Generic_ExtensionMethodWithGenericReturnValue<TTest>" +
                                        "(Context, value);}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethods().SingleOrDefault(x => x.Name == "Generic_ExtensionMethodWithGenericReturnValue");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
Пример #8
0
            public void Should_Return_Correctly_Generated_Wrapper_For_Method_With_Return_Value()
            {
                const string expected = "public System.String NonGeneric_ExtensionMethodWithReturnValue(){" +
                                        "return Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.NonGeneric_ExtensionMethodWithReturnValue" +
                                        "(Context);}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethod("NonGeneric_ExtensionMethodWithReturnValue");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
            public void Should_Return_Correctly_Generated_Wrapper_For_Non_Generic_Type_Without_Arguments()
            {
                const string expected = "public void NonGeneric_ExtensionMethodWithNoParameters(){" +
                                        "Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.NonGeneric_ExtensionMethodWithNoParameters" +
                                        "(GetContext());}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethod("NonGeneric_ExtensionMethodWithNoParameters");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
Пример #10
0
            public void Should_Return_Correctly_Generated_Wrapper_For_Non_Generic_Type_With_Parameter_Array_Argument()
            {
                const string expected = "public void NonGeneric_ExtensionMethodWithParameterArray(params System.Int32[] values){" +
                                        "Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.NonGeneric_ExtensionMethodWithParameterArray" +
                                        "(GetContext(),values);}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethod("NonGeneric_ExtensionMethodWithParameterArray");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
Пример #11
0
            public void Should_Return_Correctly_Generated_Wrapper_For_Generic_Type_Without_Arguments()
            {
                const string expected = "public void Generic_ExtensionMethod<TTest>(){" +
                                        "Cake.Core.Tests.Fixtures.MethodAliasGeneratorFixture.Generic_ExtensionMethod<TTest>" +
                                        "(GetContext());}";

                var method = typeof(MethodAliasGeneratorFixture).GetMethods().SingleOrDefault(x => x.Name == "Generic_ExtensionMethod");

                // When
                var result = MethodAliasGenerator.Generate(method);

                // Then
                Assert.Equal(expected, result);
            }
Пример #12
0
        private static string GenerateCode(MethodInfo method)
        {
            string code;

            if (method.IsDefined(typeof(CakeMethodAliasAttribute)))
            {
                code = MethodAliasGenerator.Generate(method);
            }
            else if (method.IsDefined(typeof(CakePropertyAliasAttribute)))
            {
                code = PropertyAliasGenerator.Generate(method);
            }
            else
            {
                throw new InvalidOperationException("Unknown alias type.");
            }
            return(code);
        }
Пример #13
0
        private static string GetAliasCode(Script context)
        {
            var result = new Dictionary <string, string>();

            foreach (var alias in context.Aliases)
            {
                string hash, code = alias.Type == ScriptAliasType.Method
                    ? MethodAliasGenerator.Generate(alias.Method, out hash)
                    : PropertyAliasGenerator.Generate(alias.Method, out hash);

                string @namespace = alias.Method.DeclaringType.Namespace ?? "@null";
                if (result.ContainsKey(hash))
                {
                    var message = $"{alias.Type.ToString().ToLowerInvariant()} \"{alias.Name}\" excluded from code generation and will need to be fully qualified on ICakeContext.";
                    if (context.ExcludedNamespaces.ContainsKey(@namespace))
                    {
                        context.ExcludedNamespaces[@namespace].Add(message);
                    }
                    else
                    {
                        context.ExcludedNamespaces.Add(@namespace,
                                                       new List <string>()
                        {
                            message
                        });
                    }
                    continue;
                }
                else if (context.ExcludedNamespaces.ContainsKey(@namespace))
                {
                    var message = $"{alias.Type.ToString().ToLowerInvariant()} \"{alias.Name}\" was included in code generation, but will need to be fully qualified on ICakeContext.";
                    context.ExcludedNamespaces[@namespace].Add(message);
                }

                result.Add(hash, code);
            }
            return(string.Join("\r\n", result.Values));
        }
Пример #14
0
        public string Generate(string name)
        {
            var method = _methods.SingleOrDefault(x => x.Name == name);

            return(MethodAliasGenerator.Generate(method).NormalizeGeneratedCode());
        }