public async Task CompilationRenderer_SpecialTests_Async(SpecTest data)
        {
            var builder = new CompilerSettingsBuilder();

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            if (data.ExpectedException != null)
            {
                var ex = await Assert.ThrowsAsync(data.ExpectedException.GetType(), CompileAndRender);

                Assert.Equal(data.ExpectedException.Message, ex.Message);
            }
            else
            {
                Assert.Equal(data.Expected, await CompileAndRender());
            }

            async Task <string> CompileAndRender()
            {
                var output = data.Partials != null ?
                             await stubble.CompileAsync(data.Template, data.Data, data.Partials) :
                             await stubble.CompileAsync(data.Template, data.Data);

                return((string)output(data.Data));
            }
        }
예제 #2
0
        /// <inheritdoc/>
        public StubbleCompilationRenderer Build()
        {
            var builder = new CompilerSettingsBuilder();

            ConfigureSettings?.Invoke(builder);
            return(new StubbleCompilationRenderer(builder.BuildSettings()));
        }
        public void CompilationRenderer_SpecialTests(SpecTest data)
        {
            var builder = new CompilerSettingsBuilder();

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            if (data.ExpectedException != null)
            {
                var ex = Assert.Throws(data.ExpectedException.GetType(), () =>
                {
                    CompileAndRender();
                });

                Assert.Equal(data.ExpectedException.Message, ex.Message);
            }
            else
            {
                Assert.Equal(data.Expected, CompileAndRender());
            }

            string CompileAndRender()
            {
                var output = data.Partials != null?
                             stubble.Compile(data.Template, data.Data, data.Partials) :
                                 stubble.Compile(data.Template, data.Data);

                return((string)output(data.Data));
            }
        }
예제 #4
0
        public void CompilationRenderer_ItShouldAllowMultipleTruthyChecks(string fooValue, string result)
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddTruthyCheck <string>(val => !val.Equals("Bar"));
            builder.AddTruthyCheck <string>(val => !val.Equals("Boo"));

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new
            {
                Foo = "Bar",
                Bar = "Display Me"
            };

            var func = stubble.Compile("{{#Foo}}{{Bar}}{{/Foo}}", obj);

            var renderResult = func(new
            {
                Foo = fooValue,
                Bar = "Hello World"
            });

            Assert.Equal(result, renderResult);
        }
        public void CompilationRenderer_ShouldBeCaseSensitiveByDefault()
        {
            var builder = new CompilerSettingsBuilder();

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var arg = new
            {
                Foo = "Bar"
            };

            var func = stubble.Compile("{{foo}}", arg);

            Assert.Equal("", func(arg));
        }
        public void It_Can_Retrieve_Values_From_Dynamic()
        {
            dynamic input = new ExpandoObject();

            input.Foo    = "Bar";
            input.Number = 1;
            input.Blah   = new { String = "Test" };

            var builder = new CompilerSettingsBuilder();
            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var func = stubble.Compile <ExpandoObject>("{{Foo}} {{Number}} {{Blah.String}}", input);

            Assert.Equal("Bar 1 ", func(input));
        }
예제 #7
0
        private void CheckNoResultForFalseyValue <T>(T value)
        {
            var builder = new CompilerSettingsBuilder();
            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new
            {
                Val = value
            };

            var func   = stubble.Compile("{{#Val}}Oh Hi{{/Val}}", obj);
            var result = func(obj);

            Assert.Equal("", result);
        }
예제 #8
0
        public void It_Should_Skip_Indexes_Outside_Of_Array()
        {
            var input = new
            {
                Array = new[] { "Foo", "Bar" }
            };

            var builder = new CompilerSettingsBuilder();
            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var func  = stubble.Compile("{{Array.2}}", input);
            var func2 = stubble.Compile("{{Array.10}}", input);

            Assert.Equal("", func(input));
            Assert.Equal("", func2(input));
        }
        public void CompilationRenderer_IgnoreCaseShouldIgnoreCase()
        {
            var builder = new CompilerSettingsBuilder()
                          .SetIgnoreCaseOnKeyLookup(true);

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var arg = new
            {
                Foo = "Bar"
            };

            var ignoreCase = stubble.Compile("{{foo}}", arg);

            Assert.Equal("Bar", ignoreCase(arg));
        }
        public void It_Can_Retrieve_Values_From_Dynamic_CaseInsensitively()
        {
            dynamic input = new ExpandoObject();

            input.Foo    = "Bar";
            input.Number = 1;
            input.Blah   = new { String = "Test" };

            var builder = new CompilerSettingsBuilder().SetIgnoreCaseOnKeyLookup(true);
            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var func = stubble.Compile <ExpandoObject>("{{foo}} {{number}}", input);

            var value = func(input);

            Assert.Equal("Bar 1", value);
        }
예제 #11
0
        public void It_Can_Use_Truthy_Checks(uint fooValue, string expectedResult)
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddTruthyCheck <uint>(val => val > 0);

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new
            {
                Foo = fooValue,
                Bar = "Bar"
            };

            var func = stubble.Compile("{{#Foo}}{{Bar}}{{/Foo}}", obj);

            Assert.Equal(expectedResult, func(obj));
        }
        public void It_Should_Throw_On_Data_Miss_Based_On_RenderSettings()
        {
            var input = new
            {
                Foo = "Foo"
            };

            var builder = new CompilerSettingsBuilder();
            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var ex = Assert.Throws <StubbleDataMissException>(() => stubble.Compile("{{Bar}}", input, new CompilationSettings {
                ThrowOnDataMiss = true
            }));

            Assert.Equal("'Bar' is undefined.", ex.Message);
            Assert.NotNull(ex.Data["Name"]);
            Assert.NotNull(ex.Data["SkipRecursiveLookup"]);
        }
예제 #13
0
        public void ComplicationRenderer_ItShouldAllowCustomValueGetters()
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddValueGetter <DifficultClass>((type, instance, key, ignoreCase) =>
            {
                return(Expression.Call(
                           instance,
                           typeof(DifficultClass).GetMethod("GetValue"),
                           Expression.Constant(key),
                           Expression.Constant(ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)));
            });

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new DifficultClass("Foo", "Bar");

            var func = stubble.Compile("{{_innerVal}} {{_innerVal2}}", obj);

            Assert.Equal("Foo Bar", func(obj));
        }
예제 #14
0
        public void ComplicationRenderer_ItShouldAllowCustomEnumerationConverters()
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddValueGetter <DataRow>((type, instance, key, ignoreCase) =>
            {
                var contains = Expression.Call(Expression.Property(
                                                   Expression.Property(instance, nameof(DataRow.Table)),
                                                   nameof(DataTable.Columns)),
                                               typeof(DataColumnCollection).GetMethod(nameof(DataColumnCollection.Contains)),
                                               Expression.Constant(key));

                return(Expression.Condition(contains,
                                            Expression.Property(instance, "Item", Expression.Constant(key)),
                                            Expression.Constant(null)
                                            ));
            });
            builder.AddEnumerationConverter((DataTable dt) => dt.Rows.Cast <DataRow>());

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new DataTable();

            obj.Columns.Add(new DataColumn("Column1"));
            obj.Columns.Add(new DataColumn("Column2"));
            obj.Columns.Add(new DataColumn("Column3"));

            obj.Rows.Add(1, "foo", "bar");
            obj.Rows.Add(2, "foo", "bar");
            obj.Rows.Add(3, "foo", "bar");

            var func = stubble.Compile("{{#dt}}{{Column1}} {{Column2}}.\n{{/dt}}", new { dt = obj });

            Assert.Equal(@"1 foo.
2 foo.
3 foo.
", func(new { dt = obj }));
        }
예제 #15
0
        public void It_Can_Use_Truthy_Checks()
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddTruthyCheck <uint>(val => val > 0);

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new
            {
                Foo = (uint)10,
                Bar = "Bar"
            };

            var func = stubble.Compile("{{#Foo}}{{Bar}}{{/Foo}}", obj);

            Assert.Equal("Bar", func(obj));
            Assert.Equal("", func(new
            {
                Foo = (uint)0,
                Bar = "Boo"
            }));
        }
예제 #16
0
        public void SetupBenchmark()
        {
            var loader = new DictionaryLoader(new Dictionary <string, string>()
            {
                { "tweet", TweetMustache },
                { "entities", EntitiesMustache },
                { "timeline", TimelineMustache },
            });

            Tweet    = JsonConvert.DeserializeObject <Tweet>(TweetJson);
            Timeline = new Timeline {
                tweets = Enumerable.Range(0, 20).Select(i => Tweet).ToList()
            };

            StubbleVisitorRenderer = new StubbleBuilder()
                                     .Configure(b =>
            {
                b.SetTemplateLoader(loader)
                .SetPartialTemplateLoader(loader);
            })
                                     .Build();

            var settings = new CompilerSettingsBuilder()
                           .SetTemplateLoader(loader)
                           .SetPartialTemplateLoader(loader)
                           .BuildSettings();

            StubbleCompilationRenderer = new StubbleCompilationRenderer(settings);

            TimelineCompiled = StubbleCompilationRenderer.Compile("timeline", Timeline);

            TweetTemplate = new Template();
            TweetTemplate.Load(new StringReader(TweetMustache));

            EntitiesTemplate = new Template();
            EntitiesTemplate.Load(new StringReader(EntitiesMustache));
        }
예제 #17
0
        public void CompilationRenderer_ItShouldAllowMultipleTruthyChecks()
        {
            var builder = new CompilerSettingsBuilder();

            builder.AddTruthyCheck <string>(val => !val.Equals("Bar"));
            builder.AddTruthyCheck <string>(val => !val.Equals("Boo"));

            var stubble = new StubbleCompilationRenderer(builder.BuildSettings());

            var obj = new
            {
                Foo = "Bar",
                Bar = "Display Me"
            };

            var func = stubble.Compile("{{#Foo}}{{Bar}}{{/Foo}}", obj);

            Assert.Equal("", func(obj));
            Assert.Equal("", func(new
            {
                Foo = "Boo",
                Bar = "Display Me"
            }));

            Assert.Equal("", func(new
            {
                Foo = "false",
                Bar = "Display Me"
            }));

            Assert.Equal("", func(new
            {
                Foo = (string)null,
                Bar = "Display Me"
            }));
        }