Esempio n. 1
0
        public async Task It_Can_Render_With_SectionLambda_WithContext_Async()
        {
            var stubble = new StubbleVisitorRenderer();
            var output  = await stubble.RenderAsync("{{#Foo}}{{/Foo}}", new { Foo = new Func <dynamic, string, object>((ctx, str) => ctx.Bar), Bar = "FooBar" });

            Assert.Equal("FooBar", output);
        }
Esempio n. 2
0
        public async Task It_Can_Render_Enumerators_Async()
        {
            var stubble = new StubbleVisitorRenderer();
            var output  = await stubble.RenderAsync("{{#Items}}{{.}}{{/Items}}", new { Items = "abcdefg".ToCharArray().GetEnumerator() });

            Assert.Equal("abcdefg", output);
        }
Esempio n. 3
0
        public async Task It_Can_Render_With_LambdaToken_Interpolation_NoDynamic_Async()
        {
            var stubble = new StubbleVisitorRenderer();
            var output  = await stubble.RenderAsync("{{Foo}}", new { Foo = new Func <object>(() => "{{Bar}}"), Bar = "FooBar" });

            Assert.Equal("FooBar", output);
        }
Esempio n. 4
0
        public async Task It_Can_Render_With_LambdaSection_NoDynamic_Async()
        {
            var stubble = new StubbleVisitorRenderer();
            var output  = await stubble.RenderAsync("{{#Foo}}Foo{{/Foo}}", new { Foo = new Func <string, object>((str) => str + " Bar") });

            Assert.Equal("Foo Bar", output);
        }
Esempio n. 5
0
        public async Task <string> RenderAsync(string template, object content)
        {
            var settings = new Stubble.Core.Settings.RenderSettings
            {
                ThrowOnDataMiss = false
            };

            return(await _stubble.RenderAsync(template, content, settings));
        }
Esempio n. 6
0
        public async Task StringRendererSpecTest_Async(SpecTest data)
        {
            OutputStream.WriteLine(data.Name);
            var stubble = new StubbleVisitorRenderer();
            var output  = await(data.Partials != null ? stubble.RenderAsync(data.Template, data.Data, data.Partials) : stubble.RenderAsync(data.Template, data.Data));

            OutputStream.WriteLine("Expected \"{0}\", Actual \"{1}\"", data.Expected, output);
            Assert.Equal(data.Expected, output);
        }
Esempio n. 7
0
        public async Task It_Should_Error_After_N_Recursions_Async()
        {
            const string rowTemplate = @"
            <div class='row'>
                {{#content}}
                    {{#is_column}}
                        {{>column}}
                    {{/is_column}}
                {{/content}}
            </div>";

            const string columnTemplate = @"
            <div class='column'>
                {{#content}}
                    {{#is_text}}
                        {{>text}}
                    {{/is_text}}
                    {{#is_row}}
                        {{>row}}
                    {{/is_row}}
                {{/content}}
            </div>";

            const string textTemplate = @"
            <span class='text'>
                {{text}}
            </span>";

            var treeData = new
            {
                is_row  = true,
                content = new
                {
                    is_column = true,
                    content   = new[]
                    {
                        new
                        {
                            is_text = true,
                            text    = "Hello World!"
                        }
                    }
                }
            };

            var stubble = new StubbleVisitorRenderer();
            var ex      =
                await Assert.ThrowsAsync <StubbleException>(async() => await stubble.RenderAsync(rowTemplate, treeData, new Dictionary <string, string>
            {
                { "row", rowTemplate },
                { "column", columnTemplate },
                { "text", textTemplate }
            }));

            Assert.Equal("You have reached the maximum recursion limit of 256.", ex.Message);
        }
Esempio n. 8
0
        public async Task StringRendererSpecTest_Async(SpecTest data)
        {
            var settings = RenderSettings.GetDefaultRenderSettings();

            settings.CultureInfo = data.CultureInfo ?? settings.CultureInfo;

            OutputStream.WriteLine(data.Name);
            var stubble = new StubbleVisitorRenderer();
            var output  = await(data.Partials != null ? stubble.RenderAsync(data.Template, data.Data, data.Partials, settings) : stubble.RenderAsync(data.Template, data.Data, settings));

            OutputStream.WriteLine("Expected \"{0}\", Actual \"{1}\"", data.Expected, output);
            Assert.Equal(data.Expected, output);
        }
Esempio n. 9
0
        public async Task It_Should_Allow_Async_Lambda_Method_Without_Context_In_Async_Renderer()
        {
            var stubble = new StubbleVisitorRenderer();

            var output = await stubble.RenderAsync("{{#TestMethodAsync}}Hello World!{{/TestMethodAsync}}", new {
                TestMethodAsync = new Func <string, Task <object> >(async str =>
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(100));
                    return(str.ToLower());
                }),
            });

            Assert.Equal("hello world!", output);
        }
Esempio n. 10
0
        /// <summary>
        ///     Executes the job.
        /// </summary>
        /// <param name="stubble">Stubble to use for file generation.</param>
        /// <returns>Awaitable task.</returns>
        public async Task Execute(StubbleVisitorRenderer stubble)
        {
            var templatePath = type switch
            {
                JobType.Class => @".\Templates\Class\Class.txt",
                JobType.Flag => @".\Templates\Flag\Flag.txt",
                JobType.Enumerated => @".\Templates\Enumerated\Enumerated.txt",
                JobType.Interface => @".\Templates\Interface\Interface.txt",
                _ => throw new ArgumentException()
            };

            var outputDir = data.Namespace.GetOutputPathByNameSpace();

            Directory.CreateDirectory(outputDir);

            using var streamReader = new StreamReader(templatePath, Encoding.UTF8);
            var output = await stubble.RenderAsync(await streamReader.ReadToEndAsync(), data);

            await File.WriteAllTextAsync($"{outputDir}\\{data.Name}.cs", output);
        }
    }
 /// <summary>
 /// Generate content based on template (uses mustache syntax)
 /// </summary>
 protected async Task <string> GenerateContentAsync(string template, UserNotification notification)
 {
     return(!string.IsNullOrWhiteSpace(template)
         ? await _stubbleRenderer.RenderAsync(template, notification.Notification.Data)
         : template);
 }