public void BuildHamlDocument_UnknownPartial_UsesTemplateContentProvider() { const string partialName = "SubDocument"; var rootViewSource = ViewSourceBuilder.Create("MainFile"); var rootDocument = HamlDocumentBuilder.Create("MainFile", new HamlNodePartial(new HamlLine(partialName, HamlRuleEnum.Partial, "", 0))); _treeParserMock.Setup(x => x.ParseViewSource(rootViewSource)) .Returns(rootDocument); var viewSourceList = new ViewSourceCollection { rootViewSource }; var childViewSource = ViewSourceBuilder.Create("SubDocument", partialName); var childDocument = HamlDocumentBuilder.Create("SubDocument)", new HamlNodeTextContainer(0, "Child Test")); _treeParserMock.Setup(x => x.ParseViewSource(childViewSource)) .Returns(childDocument); _templateContentProviderMock.Setup(x => x.GetViewSource(partialName)) .Returns(childViewSource); var result = _templateFactoryFactory.BuildHamlDocument(viewSourceList); _templateContentProviderMock.Verify(x => x.GetViewSource(partialName)); }
public void BuildHamlDocument_UnnamedPartialReference_UsesFollowingPartial() { var rootViewSource = ViewSourceBuilder.Create("MainFile"); var rootDocument = HamlDocumentBuilder.Create("MainFile", new HamlNodePartial(new HamlLine("", HamlRuleEnum.Partial, "", 0))); _treeParserMock.Setup(x => x.ParseViewSource(rootViewSource)) .Returns(rootDocument); var childViewSource = ViewSourceBuilder.Create("SubDocument", "SubDocument"); var childDocument = HamlDocumentBuilder.Create("SubDocument)", new HamlNodeTextContainer(0, "Child Test")); _treeParserMock.Setup(x => x.ParseViewSource(childViewSource)) .Returns(childDocument); var viewSourceList = new ViewSourceCollection { rootViewSource, childViewSource }; var result = _templateFactoryFactory.BuildHamlDocument(viewSourceList); Assert.That(result, Is.SameAs(rootDocument)); Assert.That(((HamlNodePartial)result.Children.First()).IsResolved, Is.True); Assert.That(result.Children.First().Children.First(), Is.SameAs(childDocument.Children.First())); }
public void ParseViewSource_SingleLineTemplate_ReturnsHamlTree() { var viewSource = ViewSourceBuilder.Create("Test"); var result = _parser.ParseViewSource(viewSource); Assert.IsInstanceOf(typeof(HamlDocument), result); }
private Template CreateTemplate(string hamlTemplate) { var viewSource = ViewSourceBuilder.Create(hamlTemplate); var templateEngine = XmlConfigurator.GetTemplateEngine(new FileTemplateContentProvider(), new List <string>(), new List <string>()); var compiledTemplate = templateEngine.GetCompiledTemplate(viewSource, typeof(Template)); return(compiledTemplate.CreateTemplate()); }
public void GetTemplateEngine_BasicHamlConfig_ContainsExtraUsingStatement() { var viewSource = ViewSourceBuilder.Create("= NHaml.IntegrationTests.XmlConfigurator_IntegrationTests.TestRenderMethod()"); var templateEngine = XmlConfigurator.GetTemplateEngine(); var templateFactory = templateEngine.GetCompiledTemplate(viewSource, typeof(Template)); var template = templateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); Assert.That(textWriter.ToString(), Is.StringMatching("Test")); }
public void GetCompiledTemplate_NormalUse_CallsTemplateFactoryFactoryCompileTemplateFactory() { // Arrange var viewSourceCollection = new ViewSourceCollection { ViewSourceBuilder.Create() }; // Act _templateEngine.GetCompiledTemplate(viewSourceCollection, typeof(object)); // Assert _templateFactoryFactoryMock.Verify(x => x.CompileTemplateFactory(viewSourceCollection.GetClassName(), viewSourceCollection, It.IsAny <Type>())); }
public void GetCompiledTemplate_BaseTemplateSpecified_UsesSpecifiedTemplateBaseType() { // Arrange var viewSourceCollection = new ViewSourceCollection { ViewSourceBuilder.Create() }; // Act _templateEngine.GetCompiledTemplate(viewSourceCollection, typeof(object)); // Assert _templateFactoryFactoryMock.Verify(x => x.CompileTemplateFactory(viewSourceCollection.GetClassName(), viewSourceCollection, typeof(object))); }
public void GetCompiledTemplate_MultipleCalls_OnlyCompilesTemplateOnce() { // Arrange var viewSourceCollection = new ViewSourceCollection { ViewSourceBuilder.Create() }; // Act _templateEngine.GetCompiledTemplate(viewSourceCollection, typeof(DummyTemplate)); _templateEngine.GetCompiledTemplate(viewSourceCollection, typeof(DummyTemplate)); // Assert _templateFactoryFactoryMock.Verify(x => x.CompileTemplateFactory(viewSourceCollection.GetClassName(), viewSourceCollection, typeof(DummyTemplate)), Times.Once()); }
public void CompileTemplateFactory_CallsTreeParser() { // Arrange var fakeHamlSource = ViewSourceBuilder.Create(); // Act var compiledTemplate = new TemplateFactoryFactory(_templateContentProviderMock.Object, _parserMock.Object, _documentWalkerMock.Object, _compilerMock.Object, new List <string>(), new List <string>()); compiledTemplate.CompileTemplateFactory("className", fakeHamlSource); // Assert _parserMock.Verify(x => x.ParseViewSource(fakeHamlSource)); }
public void TemplateWithDynamicModel_RenderNullString_ThrowsArgumentException() { // Arrange const string templateContent = @"=Model.NullValue"; var viewSource = ViewSourceBuilder.Create(templateContent); var templateFactory = _templateEngine.GetCompiledTemplate(viewSource, typeof(CustomTemplateBase)); // Act var template = (CustomTemplateBase)templateFactory.CreateTemplate(); template.Model.NullValue = null; var textWriter = new StringWriter(); Assert.Throws <RuntimeBinderException>(() => template.Render(textWriter)); }
public void GetCompiledTemplate_NormalUse_ReturnsCompiledTemplateFactory() { // Arrange var viewSource = ViewSourceBuilder.Create(); var expectedTemplateFactory = new TemplateFactory(typeof(DummyTemplate)); _templateFactoryFactoryMock.Setup(x => x.CompileTemplateFactory(It.IsAny <string>(), It.IsAny <ViewSourceCollection>(), It.IsAny <Type>())) .Returns(expectedTemplateFactory); // Act var templateFactory = _templateEngine.GetCompiledTemplate(viewSource, typeof(object)); // Assert Assert.That(templateFactory, Is.SameAs(expectedTemplateFactory)); }
public void SimpleIntegrationTest() { // Arrange const string templateContent = @"This is a test"; var viewSource = ViewSourceBuilder.Create(templateContent); var templateFactory = _templateEngine.GetCompiledTemplate(viewSource, typeof(CustomTemplateBase)); // Act Template template = templateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); // Assert Assert.AreEqual("This is a test", textWriter.ToString()); }
public void SimpleIntegrationTest() { // Arrange const string templateContent = @"This is a test"; var viewSource = ViewSourceBuilder.Create(templateContent); // Act var templateFactory = _templateFactoryFactory.CompileTemplateFactory(viewSource.GetClassName(), viewSource); Template template = templateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); // Assert Assert.AreEqual("This is a test", textWriter.ToString()); }
public void GetTemplateEngine_AdditionalUsingAndReferencedAssemblies_ContainsExtraUsingStatement() { var viewSource = ViewSourceBuilder.Create("= new StringBuilder(\"Default\")"); var contentProvider = new FileTemplateContentProvider(); var importsList = new List <string> { "System.Text" }; var referencedAssembliesList = new List <string>(); var templateEngine = XmlConfigurator.GetTemplateEngine(contentProvider, importsList, referencedAssembliesList); var templateFactory = templateEngine.GetCompiledTemplate(viewSource, typeof(Template)); var template = templateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); Assert.That(textWriter.ToString(), Is.StringMatching("Default")); }
public void XmlConfigurator_TypedTemplate_CompilesValidTemplate() { // Arrange string templateContent = @"= Model.Date"; var viewSource = ViewSourceBuilder.Create(templateContent); // Act var templateEngine = XmlConfigurator.GetTemplateEngine(); var compiledTemplateFactory = templateEngine.GetCompiledTemplate(viewSource, typeof(TypedTemplate <DateTime>)); var template = (TypedTemplate <DateTime>)compiledTemplateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter, DateTime.Now); // Assert Assert.AreEqual(DateTime.Today.ToString(), textWriter.ToString()); }
public void TemplateWithDynamicModel_RenderNullWithCastString_RendersEmptyString() { // Arrange const string templateContent = @"=(string)Model.NullValue"; var viewSource = ViewSourceBuilder.Create(templateContent); var templateFactory = _templateEngine.GetCompiledTemplate(viewSource, typeof(CustomTemplateBase)); // Act var template = (CustomTemplateBase)templateFactory.CreateTemplate(); template.Model.NullValue = null; var textWriter = new StringWriter(); template.Render(textWriter); // Assert Assert.AreEqual(string.Empty, textWriter.ToString()); }
public void BuildHamlDocument_NoPartials_ReturnsSameDocument() { var viewSource = ViewSourceBuilder.Create("Test"); var hamlDocument = HamlDocumentBuilder.Create("MainFile", new HamlNodeTextContainer(0, "Test")); _treeParserMock.Setup(x => x.ParseViewSource(viewSource)) .Returns(hamlDocument); var viewSourceCollection = new ViewSourceCollection { viewSource }; var result = _templateFactoryFactory.BuildHamlDocument(viewSourceCollection); Assert.That(result, Is.SameAs(hamlDocument)); Assert.That(result.Children.First(), Is.SameAs(hamlDocument.Children.First())); }
public void XmlConfigurator_DefaultUsage_CompilesValidTemplate() { // Arrange string templateContent = @"This is a test"; var viewSource = ViewSourceBuilder.Create(templateContent); // Act var templateEngine = XmlConfigurator.GetTemplateEngine(); var compiledTemplateFactory = templateEngine.GetCompiledTemplate(viewSource, typeof(Template)); Template template = compiledTemplateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); // Assert Assert.AreEqual("This is a test", textWriter.ToString()); }
public void CompileTemplateFactory_CallsCompile() { // Arrange var fakeTemplateSource = "FakeTemplateSource"; _documentWalkerMock.Setup(x => x.Walk(It.IsAny <HamlDocument>(), It.IsAny <string>(), It.IsAny <Type>(), It.IsAny <IList <string> >())) .Returns(fakeTemplateSource); var viewSource = ViewSourceBuilder.Create(); var assemblies = new List <string>(); // Act var compiledTemplate = new TemplateFactoryFactory(_contentProviderMock.Object, _parserMock.Object, _documentWalkerMock.Object, _compilerMock.Object, new List <string>(), assemblies); compiledTemplate.CompileTemplateFactory(viewSource.GetClassName(), viewSource); // Assert _compilerMock.Verify(x => x.Compile(fakeTemplateSource, viewSource.GetClassName(), assemblies)); }
public void TemplateWithStringModelProperty_ModelPropertyReferencedAsExpression_GeneratesCorrectOutput() { // Arrange const string templateContent = "This is a test of a #{Model.StringValue}\n" + "%a(href=#{Model.StringValue})"; var viewSource = ViewSourceBuilder.Create(templateContent); var templateFactory = _templateEngine.GetCompiledTemplate(viewSource, typeof(CustomTemplateBase)); // Act var template = (CustomTemplateBase)templateFactory.CreateTemplate(); template.Model.StringValue = "Model String Property"; var textWriter = new StringWriter(); template.Render(textWriter); // Assert string expected = string.Format("This is a test of a {0}\n<a href='{0}'></a>", template.Model.StringValue); Assert.AreEqual(expected, textWriter.ToString()); }
public void PartialsIntegrationTest() { // Arrange const string template1Content = "This is a test\n_File2"; const string template2Content = "Of a partial"; var viewSourceList = new ViewSourceCollection { ViewSourceBuilder.Create(template1Content), ViewSourceBuilder.Create(template2Content, "File2") }; // Act var templateFactory = _templateFactoryFactory.CompileTemplateFactory(viewSourceList.GetClassName(), viewSourceList); Template template = templateFactory.CreateTemplate(); var textWriter = new StringWriter(); template.Render(textWriter); // Assert Assert.AreEqual("This is a test\nOf a partial", textWriter.ToString()); }
public void CompileTemplateFactory_CallsDocumentWalker() { // Arrange const string className = "className"; var baseType = typeof(Template); var fakeHamlDocument = HamlDocumentBuilder.Create(""); _parserMock.Setup(x => x.ParseViewSource(It.IsAny <ViewSource>())) .Returns(fakeHamlDocument); var viewSource = ViewSourceBuilder.Create(); var imports = new List <string>(); // Act var compiledTemplate = new TemplateFactoryFactory(_contentProviderMock.Object, _parserMock.Object, _documentWalkerMock.Object, _compilerMock.Object, new List <string>(), imports); compiledTemplate.CompileTemplateFactory(className, new ViewSourceCollection { viewSource }, baseType); // Assert _documentWalkerMock.Verify(x => x.Walk(fakeHamlDocument, className, baseType, imports)); }
public void GetCompiledTemplate_NullBaseType_ThrowsArgumentNullException() { // Act Assert.Throws <ArgumentNullException>( () => _templateEngine.GetCompiledTemplate(ViewSourceBuilder.Create(), null)); }