/// <summary>
 ///     Initializes a new instance of the <see cref="NestedClosureCodeFragment" /> class.
 /// </summary>
 /// <param name="parameter">The nested closure parameter's name.</param>
 /// <param name="methodCall">The method call used as the body of the nested closure.</param>
 public NestedClosureCodeFragment(string parameter, MethodCallCodeFragment methodCall)
 {
     Parameter   = parameter;
     MethodCalls = new List <MethodCallCodeFragment> {
         methodCall
     };
 }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual string Fragment(MethodCallCodeFragment fragment)
        {
            var builder = new StringBuilder();

            var current = fragment;

            while (current != null)
            {
                builder
                .Append(".")
                .Append(current.Method)
                .Append("(");

                for (var i = 0; i < current.Arguments.Count; i++)
                {
                    if (i != 0)
                    {
                        builder.Append(", ");
                    }

                    builder.Append(UnknownLiteral(current.Arguments[i]));
                }

                builder.Append(")");

                current = current.ChainedCall;
            }

            return(builder.ToString());
        }
 public override MethodCallCodeFragment GenerateUseProvider(
     string connectionString,
     MethodCallCodeFragment providerOptions)
 => new MethodCallCodeFragment(
     "UseTestProvider",
     providerOptions == null
             ? new object[] { connectionString }
             : new object[] { connectionString, new NestedClosureCodeFragment("x", providerOptions) });
        public void Generate_MethodCallCodeFragment_works_with_arrays()
        {
            var method = new MethodCallCodeFragment("Test", new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { "foo", "bar" });

            var result = new CSharpUtilities().Generate(method);

            Assert.Equal(".Test(new byte[] { 1, 2 }, new int[] { 3, 4 }, new string[] { \"foo\", \"bar\" })", result);
        }
示例#5
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public override MethodCallCodeFragment GenerateUseProvider(
     string connectionString,
     MethodCallCodeFragment providerOptions)
 => new MethodCallCodeFragment(
     nameof(SqliteDbContextOptionsBuilderExtensions.UseSqlite),
     providerOptions == null
             ? new object[] { connectionString }
             : new object[] { connectionString, new NestedClosureCodeFragment("x", providerOptions) });
示例#6
0
        public void Fragment_MethodCallCodeFragment_works_with_arrays()
        {
            var method = new MethodCallCodeFragment(_testFuncMethodInfo, new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { "foo", "bar" });

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".TestFunc(new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { \"foo\", \"bar\" })", result);
        }
        public void Generate_MethodCallCodeFragment_works_when_niladic()
        {
            var method = new MethodCallCodeFragment("Test");

            var result = new CSharpUtilities().Generate(method);

            Assert.Equal(".Test()", result);
        }
        public void Generate_MethodCallCodeFragment_works()
        {
            var method = new MethodCallCodeFragment("Test", true, 42);

            var result = new CSharpUtilities().Generate(method);

            Assert.Equal(".Test(true, 42)", result);
        }
示例#9
0
        public void Fragment_MethodCallCodeFragment_works_without_MethodInfo()
        {
            var method = new MethodCallCodeFragment("TestFunc", true, 42);

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".TestFunc(true, 42)", result);
        }
        public void Fragment_MethodCallCodeFragment_works_when_niladic()
        {
            var method = new MethodCallCodeFragment("Test");

            var result = new CSharpHelper().Fragment(method);

            Assert.Equal(".Test()", result);
        }
        public void Fragment_MethodCallCodeFragment_works()
        {
            var method = new MethodCallCodeFragment("Test", true, 42);

            var result = new CSharpHelper().Fragment(method);

            Assert.Equal(".Test(true, 42)", result);
        }
        public void Fragment_MethodCallCodeFragment_works_with_arrays()
        {
            var method = new MethodCallCodeFragment("Test", new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { "foo", "bar" });

            var result = new CSharpHelper().Fragment(method);

            Assert.Equal(".Test(new byte[] { 1, 2 }, new[] { 3, 4 }, new[] { \"foo\", \"bar\" })", result);
        }
 private MethodCallCodeFragment(
     MethodInfo methodInfo,
     MethodCallCodeFragment chainedCall,
     object?[] arguments)
     : this(methodInfo, arguments)
 {
     ChainedCall = chainedCall;
 }
示例#14
0
        public void Fragment_MethodCallCodeFragment_works_with_identifier_chained()
        {
            var method = new MethodCallCodeFragment(_testFuncMethodInfo, "One").Chain(new MethodCallCodeFragment(_testFuncMethodInfo));

            var result = new CSharpHelper(TypeMappingSource).Fragment(method, instanceIdentifier: "builder");

            Assert.Equal($@"builder{EOL}    .TestFunc(""One""){EOL}    .TestFunc()", result);
        }
示例#15
0
        public void Fragment_MethodCallCodeFragment_works_when_niladic()
        {
            var method = new MethodCallCodeFragment(_testFuncMethodInfo);

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".TestFunc()", result);
        }
示例#16
0
        public void Fragment_MethodCallCodeFragment_works_with_type_qualified()
        {
            var method = new MethodCallCodeFragment(_testFuncMethodInfo, true, 42);

            var result = new CSharpHelper(TypeMappingSource).Fragment(method, instanceIdentifier: "builder");

            Assert.Equal("builder.TestFunc(true, 42)", result);
        }
示例#17
0
        public void Fragment_MethodCallCodeFragment_works_when_chaining_on_chain_with_call()
        {
            var method = new MethodCallCodeFragment("One", Array.Empty <object>(), new MethodCallCodeFragment("Two"))
                         .Chain(new MethodCallCodeFragment("Three", Array.Empty <object>(), new MethodCallCodeFragment("Four")));

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".One().Two().Three().Four()", result);
        }
示例#18
0
        public void Fragment_MethodCallCodeFragment_works_when_chaining()
        {
            var method = new MethodCallCodeFragment("Test")
                         .Chain("Test");

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".Test().Test()", result);
        }
示例#19
0
        public void Fragment_MethodCallCodeFragment_works_when_chaining_on_chain_with_call()
        {
            var method = new MethodCallCodeFragment(_testFuncMethodInfo, "One").Chain(new MethodCallCodeFragment(_testFuncMethodInfo, "Two"))
                         .Chain(new MethodCallCodeFragment(_testFuncMethodInfo, "Three").Chain(new MethodCallCodeFragment(_testFuncMethodInfo, "Four")));

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(@$ ".TestFunc(" "One" "){EOL}.TestFunc(" "Two" "){EOL}.TestFunc(" "Three" "){EOL}.TestFunc(" "Four" ")", result);
        }
        public void Fragment_MethodCallCodeFragment_works_when_nested_closure()
        {
            var method = new MethodCallCodeFragment(
                "Test",
                new NestedClosureCodeFragment("x", new MethodCallCodeFragment("Test")));

            var result = new CSharpHelper().Fragment(method);

            Assert.Equal(".Test(x => x.Test())", result);
        }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public override MethodCallCodeFragment GenerateUseProvider(
     string connectionString,
     MethodCallCodeFragment providerOptions)
 {
     return(new MethodCallCodeFragment(
                nameof(MySQLDbContextOptionsExtensions.UseMySQL),
                providerOptions == null
       ? new object[] { connectionString }
       : new object[] { connectionString, new NestedClosureCodeFragment("x", providerOptions) }));
 }
        public void Generate_MethodCallCodeFragment_works_when_nested_closure()
        {
            var method = new MethodCallCodeFragment(
                "Test",
                new NestedClosureCodeFragment("x", new MethodCallCodeFragment("Test")));

            var result = new CSharpUtilities().Generate(method);

            Assert.Equal(".Test(x => x.Test())", result);
        }
示例#23
0
        public void Fragment_MethodCallCodeFragment_works_when_nested_closure()
        {
            var method = new MethodCallCodeFragment(
                _testFuncMethodInfo,
                new NestedClosureCodeFragment("x", new MethodCallCodeFragment(_testFuncMethodInfo)));

            var result = new CSharpHelper(TypeMappingSource).Fragment(method);

            Assert.Equal(".TestFunc(x => x.TestFunc())", result);
        }
        public override MethodCallCodeFragment GenerateProviderOptions()
        {
            var serverVersionCall = new MethodCallCodeFragment(
                nameof(MySqlDbContextOptionsBuilder.ServerVersion),
                _options.ServerVersion.ToString());

            var providerOptions = base.GenerateProviderOptions();

            return(providerOptions == null
                ? serverVersionCall
                : serverVersionCall.Chain(providerOptions));
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override MethodCallCodeFragment GenerateUseProvider(
            string connectionString,
            MethodCallCodeFragment providerOptions)
        {
            // Strip scaffolding specific connection string options first.
            connectionString = new MySqlScaffoldingConnectionSettings(connectionString).GetProviderCompatibleConnectionString();

            return(new MethodCallCodeFragment(
                       nameof(MySqlDbContextOptionsExtensions.UseMySql),
                       providerOptions == null
                    ? new object[] { connectionString }
                    : new object[] { connectionString, new NestedClosureCodeFragment("x", providerOptions) }));
        }
        public virtual void UseProviderMethodIsGeneratedCorrectlyWithOptions()
        {
            var codeGenerator = new MySQLCodeGenerator(
                new ProviderCodeGeneratorDependencies(
                    Enumerable.Empty <IProviderCodeGeneratorPlugin>()));

            var providerOptions = new MethodCallCodeFragment("SetProviderOption");

            var result = codeGenerator.GenerateUseProvider("Data Source=Test", providerOptions);

            Assert.AreEqual("UseMySQL", result.Method);
            Assert.That(result.Arguments, Has.Exactly(1).EqualTo("Data Source=Test"));
            Assert.IsInstanceOf <NestedClosureCodeFragment>(result.Arguments[1]);
            NestedClosureCodeFragment nestedClosure = (NestedClosureCodeFragment)result.Arguments[1];

            Assert.AreEqual("x", nestedClosure.Parameter);
            Assert.AreSame(providerOptions, nestedClosure.MethodCall);
            Assert.Null(result.ChainedCall);
        }
示例#27
0
        public virtual void Use_provider_method_is_generated_correctly_with_options()
        {
            var codeGenerator = new SpannerCodeGenerator(
                new ProviderCodeGeneratorDependencies(Enumerable.Empty <IProviderCodeGeneratorPlugin>()));

            var providerOptions = new MethodCallCodeFragment("SetProviderOption");

            var result = codeGenerator.GenerateUseProvider("Source=projects/p1/instances/i1/databases/d1", providerOptions);

            Assert.Equal("UseSpanner", result.Method);
            Assert.Collection(
                result.Arguments,
                a => Assert.Equal("Source=projects/p1/instances/i1/databases/d1", a),
                a =>
            {
                var nestedClosure = Assert.IsType <NestedClosureCodeFragment>(a);

                Assert.Equal("x", nestedClosure.Parameter);
                Assert.Same(providerOptions, nestedClosure.MethodCall);
            });
            Assert.Null(result.ChainedCall);
        }
示例#28
0
        private string Fragment(MethodCallCodeFragment fragment, int indent)
        {
            var builder = new IndentedStringBuilder();

            var current = fragment;

            while (current != null)
            {
                builder
                .Append('.')
                .Append(current.Method)
                .Append('(');

                for (var i = 0; i < current.Arguments.Count; i++)
                {
                    if (i != 0)
                    {
                        builder.Append(", ");
                    }

                    var argument = current.Arguments[i];
                    if (argument is NestedClosureCodeFragment nestedFragment)
                    {
                        builder.Append(Fragment(nestedFragment, indent));
                    }
                    else
                    {
                        builder.Append(UnknownLiteral(argument));
                    }
                }

                builder.Append(')');

                current = current.ChainedCall;
            }

            return(builder.ToString());
        }
        public virtual void Use_provider_method_is_generated_correctly_with_options()
        {
            var codeGenerator = new NpgsqlCodeGenerator(
                new ProviderCodeGeneratorDependencies(
                    Enumerable.Empty <IProviderCodeGeneratorPlugin>()));

            var providerOptions = new MethodCallCodeFragment("SetProviderOption");

            var result = codeGenerator.GenerateUseProvider("Server=test;Username=test;Password=test;Database=test", providerOptions);

            Assert.Equal("UseNpgsql", result.Method);
            Assert.Collection(
                result.Arguments,
                a => Assert.Equal("Server=test;Username=test;Password=test;Database=test", a),
                a =>
            {
                var nestedClosure = Assert.IsType <NestedClosureCodeFragment>(a);

                Assert.Equal("x", nestedClosure.Parameter);
                Assert.Same(providerOptions, nestedClosure.MethodCall);
            });
            Assert.Null(result.ChainedCall);
        }
示例#30
0
        public virtual void Use_provider_method_is_generated_correctly_with_options()
        {
            var codeGenerator = new SqlServerCodeGenerator(
                new ProviderCodeGeneratorDependencies(
                    Enumerable.Empty <IProviderCodeGeneratorPlugin>()));

            var providerOptions = new MethodCallCodeFragment(_setProviderOptionMethodInfo);

            var result = codeGenerator.GenerateUseProvider("Data Source=Test", providerOptions);

            Assert.Equal("UseSqlServer", result.Method);
            Assert.Collection(
                result.Arguments,
                a => Assert.Equal("Data Source=Test", a),
                a =>
            {
                var nestedClosure = Assert.IsType <NestedClosureCodeFragment>(a);

                Assert.Equal("x", nestedClosure.Parameter);
                Assert.Same(providerOptions, nestedClosure.MethodCalls[0]);
            });
            Assert.Null(result.ChainedCall);
        }