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 static void CompileToAssembly(JsonSchemaCodeGenerator codeGenerator, IEnumerable<JSchema> schemas, string outputAssembly, string rootNamespace = null) { using (var stream = new FileStream(outputAssembly, FileMode.Create)) { codeGenerator.CompileTo(schemas, stream, rootNamespace); } }
private static void CompileToAssembly(JsonSchemaCodeGenerator codeGenerator, IEnumerable <JSchema> schemas, string outputAssembly, string rootNamespace = null) { using (var stream = new FileStream(outputAssembly, FileMode.Create)) { codeGenerator.CompileTo(schemas, stream, rootNamespace); } }
private static void WriteToFile(JsonSchemaCodeGenerator codeGenerator, IEnumerable<JSchema> schemas, string fileName, string rootNamespace = null) { using (var stream = new FileStream(fileName, FileMode.Create)) { codeGenerator.WriteTo(schemas, stream, rootNamespace); } }
private static void WriteToFile(JsonSchemaCodeGenerator codeGenerator, IEnumerable <JSchema> schemas, string fileName, string rootNamespace = null) { using (var stream = new FileStream(fileName, FileMode.Create)) { codeGenerator.WriteTo(schemas, stream, rootNamespace); } }
private static async Task ExecuteInternal(string sourcePath, Action<JsonSchemaCodeGenerator, IEnumerable<JSchema>> executeAction) { var fileSystem = new FileSystem(sourcePath); var config = ConfigurationLoader.LoadTerrificConfiguration(string.Empty, fileSystem); var schemaProvider = new SchemaMergeProvider(new HandlebarsViewSchemaProvider(null, null), new FileSystemSchemaProvider(config, fileSystem)); var repo = new TerrificTemplateRepository(fileSystem); var codeGenerator = new JsonSchemaCodeGenerator(); var schemas = repo.GetAll().Select(schemaProvider.GetSchemaFromTemplateAsync).ToList(); var res = await Task.WhenAll(schemas).ConfigureAwait(false); executeAction(codeGenerator, res); }
private static async Task ExecuteInternal(string sourcePath, Action <JsonSchemaCodeGenerator, IEnumerable <JSchema> > executeAction) { var fileSystem = new FileSystem(sourcePath); var config = ConfigurationLoader.LoadTerrificConfiguration(string.Empty, fileSystem); var schemaProvider = new SchemaMergeProvider(new HandlebarsViewSchemaProvider(null, null), new PhysicalSchemaProvider(config, fileSystem)); var repo = new TerrificTemplateRepository(fileSystem, config); var codeGenerator = new JsonSchemaCodeGenerator(); var schemas = repo.GetAll().Select(schemaProvider.GetSchemaFromTemplateAsync).ToList(); var res = await Task.WhenAll(schemas).ConfigureAwait(false); executeAction(codeGenerator, res); }
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; }