Пример #1
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));
        }
Пример #2
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 }));
        }