Inheritance: INamingRule
Esempio n. 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);
		}
		public void TestClassNameNormalization()
		{
			var inputs = new List<Tuple<string, string>>
			{
				new Tuple<string, string>(null, null),
				new Tuple<string, string>("class", "Class"),
				new Tuple<string, string>("person info", "PersonInfo"),
				new Tuple<string, string>("person", "Person"),
				new Tuple<string, string>("Person", "Person"),
                new Tuple<string, string>("person_info", "PersonInfo"),
				new Tuple<string, string>("SimpleClass", "SimpleClass"),
			};

			foreach (var input in inputs)
			{
				var result = new NamingRule().GetPropertyName(input.Item1);
				Assert.AreEqual(input.Item2, result);
			}
		}
        private string ExecuteClientSide(string testName, string templateFile, string dataFile)
        {
            var namingRule = new NamingRule();
            var handlerFactory = new NullRenderingHelperHandlerFactory();

            var clientGenerator = new ClientTemplateGenerator(handlerFactory, new MemberLocatorFromNamingRule(namingRule));
            var generator = new JavascriptClientTemplateGenerator("repo", clientGenerator);

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

            Dictionary<string, object> model;
            using (var reader = new StreamReader(dataFile))
            {
                model = (Dictionary<string, object>) new JavaScriptSerializer().Deserialize(reader.ReadToEnd(), typeof(Dictionary<string, object>));
            }

            CleanupModel(model);

            var result = JavascriptClientTest.ExecuteJavascript(view, model, testName);
            return result;
        }
Esempio n. 4
0
		public async Task EscapeNullStringLateBinding()
		{
			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(object)).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);
		}
        private async Task<string> ExecuteServerSide(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 viewEngine = new VeilViewEngine(cacheProvider, handlerFactory, namingRule);
            IView view = await viewEngine.CreateViewAsync(templateInfo).ConfigureAwait(false);
            if (view == null)
                Assert.True(false, string.Format("Could not create view from file'{0}'.", templateFile));

            object model;
            using (var reader = new StreamReader(dataFile))
            {
                model = new JsonSerializer().Deserialize(reader, typeof(Dictionary<string, object>));
            }

            var builder = new StringBuilder();
            using (var writer = new StringWriterDelayed(builder))
            {
                view.Render(model, new RenderingContext(writer));
            }
            var resultString = builder.ToString();
            return resultString;
        }
        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;
        }
	    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();
	    }