예제 #1
0
        static ContextObject()
        {
            DefaultFormatter = new MorestachioFormatterService();
            DefaultFormatter.AddFromType(typeof(ObjectStringFormatter));
            DefaultFormatter.AddFromType(typeof(Number));
            DefaultFormatter.AddFromType(typeof(BooleanFormatter));
            DefaultFormatter.AddFromType(typeof(DateFormatter));
            DefaultFormatter.AddFromType(typeof(EqualityFormatter));
            DefaultFormatter.AddFromType(typeof(LinqFormatter));
            DefaultFormatter.AddFromType(typeof(ListExtensions));
            DefaultFormatter.AddFromType(typeof(RegexFormatter));
            DefaultFormatter.AddFromType(typeof(TimeSpanFormatter));
            DefaultFormatter.AddFromType(typeof(StringFormatter));
            DefaultFormatter.AddFromType(typeof(RandomFormatter));
            DefaultFormatter.AddFromType(typeof(Worktime));
            DefaultFormatter.AddFromType(typeof(Money));

            DefaultDefinitionOfFalse = value => value != null &&
                                       value as bool? != false &&
                                       // ReSharper disable once CompareOfFloatsByEqualityOperator
                                       value as double? != 0 &&
                                       value as int? != 0 &&
                                       value as string != string.Empty &&
                                       // We've gotten this far, if it is an object that does NOT cast as enumberable, it exists
                                       // OR if it IS an enumerable and .Any() returns true, then it exists as well
                                       (!(value is IEnumerable) || ((IEnumerable)value).Cast <object>().Any()
                                       );
            DefinitionOfFalse = DefaultDefinitionOfFalse;
        }
예제 #2
0
 private void CallWithResult(string methodName, IDictionary <string, object> values, object expected)
 {
     Assert.That(Result, Is.Null);
     MorestachioFormatterService.PrepareMakeGenericMethodInfoByValues(GetType()
                                                                      .GetMethod(methodName), values.Select(e => e.Value).ToArray())
     .Invoke(this, values.Values.ToArray());
     Assert.That(Result, Is.EqualTo(expected));
     Result = null;
 }
예제 #3
0
 static ContextObject()
 {
     DefaultFormatter = new MorestachioFormatterService();
     DefaultFormatter.AddFromType(typeof(DefaultFormatterClassImpl));
     DefaultDefinitionOfFalse = (value) => value != null &&
                                value as bool? != false &&
                                // ReSharper disable once CompareOfFloatsByEqualityOperator
                                value as double? != 0 &&
                                value as int? != 0 &&
                                value as string != string.Empty &&
                                // We've gotten this far, if it is an object that does NOT cast as enumberable, it exists
                                // OR if it IS an enumerable and .Any() returns true, then it exists as well
                                (!(value is IEnumerable) || ((IEnumerable)value).Cast <object>().Any()
                                );
     DefinitionOfFalse = DefaultDefinitionOfFalse;
 }
예제 #4
0
        public void GenericsTest()
        {
            var methodInfo = typeof(PlaygroundTests).GetMethod(nameof(Generic));

            Array value  = new int[1];
            var   values = new Dictionary <string, object>()
            {
                { "value", value },
            };
            var endValues = values.Values;

            methodInfo = MorestachioFormatterService.MakeGenericMethodInfoByValues(methodInfo, values);

            var parameters = endValues.ToArray();

            methodInfo.Invoke(this, parameters);
        }
예제 #5
0
        public void TestNamed()
        {
            var formatterService = new MorestachioFormatterService();

            formatterService.AddFromType(typeof(StringFormatter));

            var options = new ParserOptions("{{data([Name]reverse-arg, TEST)}}", null, DefaultEncoding);

            formatterService.AddFormatterToMorestachio(options);
            var template = Parser.ParseWithOptions(options);

            var andStringify = template.CreateAndStringify(new Dictionary <string, object>()
            {
                { "data", "Test" }
            });

            Assert.That(andStringify, Is.EqualTo("TEST"));
        }
예제 #6
0
        public void GenericsTest()
        {
            var formatterService = new MorestachioFormatterService();

            formatterService.AddFromType(typeof(StringFormatter));
            formatterService.AddFromType(typeof(ListFormatter));

            var options = new ParserOptions("{{data([Name]fod)}}");

            formatterService.AddFormatterToMorestachio(options);
            var template = Parser.ParseWithOptions(options);

            var andStringify = template.CreateAndStringify(new Dictionary <string, object>()
            {
                { "data", new[] { "TEST", "test" } }
            });

            Assert.That(andStringify, Is.EqualTo("TEST"));
        }
 public MorestachioTemplateBase()
 {
     MorestachioFormatterService = new MorestachioFormatterService();
     Timeout = TimeSpan.FromMinutes(1);
 }
예제 #8
0
        public async Task <GeneratedTemplateInfos> GenerateTemplateExecute(string template, IDataSourceProvider dataSourceProvider)
        {
            if (template == null)
            {
                return(null);
            }

            var parsingOptions = new ParserOptions(template,
                                                   () => new MemoryStream(),
                                                   Encoding.Default, false, true);

            parsingOptions.ValueResolver = new JObjectResolver();

            var formatterService = new MorestachioFormatterService();
            var formatters       = _templateServiceProvider.ObtainFormatters();

            foreach (var formatter in formatters)
            {
                formatterService.GlobalFormatterModels.Add(formatter.CreateFormatter());
            }

            formatterService.AddFormatterToMorestachio(parsingOptions);
            MorestachioDocumentInfo extendedParseInformation;

            try
            {
                extendedParseInformation = Parser.ParseWithOptions(parsingOptions);
            }
            catch (Exception e)
            {
                return(new GeneratedTemplateInfos()
                {
                    Errors = new IMorestachioError[]
                    {
                        new MorestachioSyntaxError(new Tokenizer.CharacterLocation(), "Error ", "", "", e.Message),
                    }
                });
            }

            if (extendedParseInformation.Errors.Any())
            {
                return(new GeneratedTemplateInfos()
                {
                    InferredTemplateModel = extendedParseInformation.Document,
                    Errors = extendedParseInformation.Errors.ToArray()
                });
            }

            var result = await dataSourceProvider.Fetch();

            if (result == null)
            {
                return(null);
            }

            return(new GeneratedTemplateInfos()
            {
                Result = extendedParseInformation.Create(result).Stringify(true, Encoding.Default),
                InferredTemplateModel = extendedParseInformation.Document
            });
        }