예제 #1
0
 public void TestCBlockFluentAPI()
 {
     _w
     .WriteLine("// will open a C block")
     .WithCBlock("void MyMethod()", () =>
     {
         _w.WriteLine("int i = 0;");
     });
     Assert.AreEqual(expected_C, _w.ToString());
 }
예제 #2
0
 public void AddCSX()
 {
     #region Adding EFCoreGenerator.csx
     var mainProgram = new CodegenTextWriter();
     mainProgram.WriteLine($@"
         class Program
         {{
             static void Main()
             {{
                 //var options = new CodegenCS.DbSchema.Templates.EFCoreGenerator.EFCoreGeneratorOptions(inputJsonSchema: @""{_options.InputJsonSchema}"");
                 var options = Newtonsoft.Json.JsonConvert.DeserializeObject<CodegenCS.DbSchema.Templates.EFCoreGenerator.EFCoreGeneratorOptions>(@""
                     {Newtonsoft.Json.JsonConvert.SerializeObject(_options, Newtonsoft.Json.Formatting.Indented).Replace("\"", "\"\"")}
                 "");
                 var generator = new CodegenCS.DbSchema.Templates.EFCoreGenerator.EFCoreGenerator(options);
                 generator.Generate();
                 generator.Save();
             }}
         }}
     ");
     // Export CS template (for customization)
     // Save with CSX extension so that it doesn't interfere with other existing CSPROJs (which by default include *.cs)
     GeneratorContext[typeof(EFCoreGenerator).Name + ".csx"].WriteLine(
         $"//This file is supposed to be launched using: codegencs run {typeof(EFCoreGenerator).Name}.csx" + Environment.NewLine
         + new StreamReader(typeof(EFCoreGenerator).Assembly.GetManifestResourceStream(typeof(EFCoreGenerator).FullName + ".cs")).ReadToEnd() + Environment.NewLine
         + mainProgram.ToString()
         );
     #endregion
 }
        public void TestCallback1()
        {
            Action <CodegenTextWriter> callback = (w) => w.WriteLine("Hello3");

            _w
            // Inside indented block we call anonymous Action
            .WithCurlyBraces("void MyMethod1()", () =>
            {
                _w.WriteLine("Hello");
            })

            // Inside indented block we call anonymous Action<CodegenTextWriter>
            .WithCurlyBraces("void MyMethod2()", (w) =>
            {
                w.WriteLine("Hello2");
            })

            // Inside indented block we call a named Action<CodegenTextWriter>
            .WithCurlyBraces("void MyMethod3()", callback)

            // After (outside) the indented block we call a named Action<CodegenTextWriter>
            .Write(callback)
            ;

            string expected = @"
void MyMethod1()
{
    Hello
}
void MyMethod2()
{
    Hello2
}
void MyMethod3()
{
    Hello3
}
Hello3
";

            Assert.AreEqual(_w.ToString(), expected.TrimStart());
        }
        public void TestMultilineBlock()
        {
            _w
            .WithCurlyBraces("namespace MyNameSpace", (w) =>
            {
                _w.WithCurlyBraces("class MyClass", (w2) =>
                {
                    _w.WriteLine(@"
                         This is a multi-line block
                         This is a multi-line block
                         This is a multi-line block
                         This is a multi-line block
                        ");
                    _w.WriteLine(@"
                             This is a multi-line block
                             This is a multi-line block
                             This is a multi-line block
                             This is a multi-line block
                            ");
                    _w.WriteLine(@"
                                 This is a multi-line block
                                 This is a multi-line block
                                 This is a multi-line block
                                 This is a multi-line block
                            ");
                    _w.WriteLine(@"
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
                            ");
                });
            })
            ;
            string expected = @"
namespace MyNameSpace
{
    class MyClass
    {
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block

        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block

        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block

        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
        This is a multi-line block
                            
    }
}
";

            Assert.AreEqual(_w.ToString(), expected.TrimStart());
        }