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);
		}
Example #2
0
		public async Task EscapeNullString()
		{
			var cacheProvider = new MemoryCacheProvider();
			var handlerFactory = new Mock<IHelperHandlerFactory>();

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

			var namingRule = new NamingRule();
			const string input = "<p>{{{name}}}</p>";
			var templateInfo = new StringTemplateInfo("views/test", input);

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

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

			var model = new TestModel();

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

			Assert.AreEqual("<p></p>", stringResult);
		}
        protected async Task GetErrorPage(StreamWriter writer, Exception error, SourceLocation location)
        {
            var fileSystem = new EmbeddedResourceFileSystem(typeof(WebInitializer).Assembly);
            string content;
            using (var reader = new StreamReader(fileSystem.OpenRead(PathInfo.Create("Core/error_partial.html"))))
            {
                content = await reader.ReadToEndAsync();
            }

            var templateInfo = new StringTemplateInfo("error", content);

            var view = await ((IViewEngine)Resolver.GetService(typeof(IViewEngine)))
	            .CreateViewAsync(templateInfo, typeof(ErrorViewModel)).ConfigureAwait(false);

            if (location == null)
            {
                var modelWithoutLocation = new ErrorViewModel
                {
                    ErrorMessage = error.Message,
                    Details = error.StackTrace
                };
                view.Render(modelWithoutLocation, new RenderingContext(writer));
                return;
            }

            var templateRepository = (ITemplateRepository)this.Resolver.GetService(typeof(ITemplateRepository));
            var sourceTemplate = await templateRepository.GetTemplateAsync(location.TemplateId).ConfigureAwait(false);
            string sourceTemplateSource;

            using (var reader = new StreamReader(sourceTemplate.Open()))
            {
                sourceTemplateSource = await reader.ReadToEndAsync().ConfigureAwait(false);
            }

            var model = new ErrorViewModel
            {
                TemplateId = location.TemplateId,
                ErrorMessage = error.Message,
                Details = error.StackTrace,
                Before = sourceTemplateSource.Substring(0, location.Index),
                Node = sourceTemplateSource.Substring(location.Index, location.Length),
                After = sourceTemplateSource.Substring(location.Index + location.Length),
                Text = HttpUtility.JavaScriptStringEncode(sourceTemplateSource),
                Range = GetRange(sourceTemplateSource, location)
            };

            view.Render(model, new RenderingContext(writer));
        }
	    private static async Task<string> Execute(string input, string templateId, object model, Type modelType)
	    {
	        var cacheProvider = new MemoryCacheProvider();
	        var handlerFactory = new Mock<IHelperHandlerFactory>();

	        var namingRule = new NamingRule();
	        var templateInfo = new StringTemplateInfo(templateId, input);

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

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

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