Example #1
0
		public async Task TemplateEngineShouldUseSameNamingConventionForBinding()
		{
			var cacheProvider = new MemoryCacheProvider();
			var handlerFactory = new Mock<IHelperHandlerFactory>();

			handlerFactory.Setup(f => f.Create()).Returns(Enumerable.Empty<IHelperHandler>());

			var namingRule = new NamingRule();
			var schemaProvider = new HandlebarsViewSchemaProvider(null, new MemberLocatorFromNamingRule(namingRule));
			var codeGenerator = new JsonSchemaCodeGenerator(namingRule);
			const string input = "<p>{{name}}</p><p>{{first_name}}</p>";
			var templateInfo = new StringTemplateInfo("views/test", input);

			var schema = await schemaProvider.GetSchemaFromTemplateAsync(templateInfo).ConfigureAwait(false);
			var modelType = codeGenerator.Compile(schema);

			var viewEngine = new VeilViewEngine(cacheProvider, handlerFactory.Object, namingRule);

			var view = await viewEngine.CreateViewAsync(templateInfo, modelType).ConfigureAwait(false);

			var model = Activator.CreateInstance(modelType);

			modelType.GetProperty("Name").SetValue(model, "{{name}}");
			modelType.GetProperty("FirstName").SetValue(model, "{{first_name}}");

			var writer = new StringWriter();
			view.Render(model, new RenderingContext(writer));
			var stringResult = writer.ToString();

			Assert.AreEqual(input, stringResult);
		}
        private async Task<string> ExecuteServerSideStrongModel(string testName, string templateFile, string dataFile)
        {
            var cacheProvider = new NullCacheProvider();
            var namingRule = new NamingRule();
            var handlerFactory = new NullRenderingHelperHandlerFactory();

            var templateInfo = new FileTemplateInfo(testName, PathInfo.Create(templateFile), new FileSystem());

            var schemaProvider = new HandlebarsViewSchemaProvider(handlerFactory, new MemberLocatorFromNamingRule(namingRule));

            var generator = new JsonSchemaCodeGenerator(namingRule);
            var schema = await schemaProvider.GetSchemaFromTemplateAsync(templateInfo).ConfigureAwait(false);
            schema.Title = "Model";
            var modelType = generator.Compile(schema);

            var viewEngine = new VeilViewEngine(cacheProvider, handlerFactory, namingRule);
            var view = await viewEngine.CreateViewAsync(templateInfo, modelType, StaticModelBinder.Create(modelType)).ConfigureAwait(false);
            if (view == null)
                Assert.True(false, string.Format("Could not create view from file '{0}'.", templateFile));

            object model;
            using (var reader = new JsonTextReader(new StreamReader(dataFile)))
            {
                model = JsonSerializer.Create().Deserialize(reader, modelType);
            }

            var builder = new StringBuilder();
            using (var writer = new StringWriterDelayed(builder))
            {
                view.Render(model, new RenderingContext(writer));
            }
            var resultString = builder.ToString();
            return resultString;
        }