ln.templates/ln.templates.test/HtmlTests.cs

73 lines
2.9 KiB
C#

// /**
// * File: HtmlTests.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using NUnit.Framework;
using System;
using ln.templates.html;
using System.IO;
namespace ln.templates.test
{
[TestFixture()]
public class HtmlTests
{
[Test()]
public void TestCase()
{
html.TemplateReader templateReader = new html.TemplateReader();
templateReader.Read("<!DOCTYPE HTML>\n<html>\n<head>\n <title>Hello</title>\n</head>\n<body>\n <p>Welcome to this example.</p>\n <p>{{ 'Ich bin ein ScriptText!' }}</p>\n <p>DateTime: {{ Date }}</p>\n <p>Ein bisschen JavaScript: {{ 5 + ' mal ' + 5 + ' = ' + (5*5) }}</p>\n</body>\n</html>");
Console.WriteLine("Source rendered:\n{0}", templateReader.Document.ToString());
StringWriter stringWriter = new StringWriter();
RenderContext renderContext = new RenderContext(stringWriter);
renderContext.SetScriptObject("Date", DateTime.Now);
templateReader.TemplateDocument.RenderTemplate(renderContext);
Console.WriteLine("Template rendered:\n{0}", stringWriter.ToString());
}
[Test()]
public void TestFileSystemTemplateSource()
{
if (File.Exists("templates.tst/frame.html"))
File.Delete("templates.tst/frame.html");
if (File.Exists("templates.tst/head.html"))
File.Delete("templates.tst/head.html");
Directory.CreateDirectory("templates.tst");
using (FileStream fs = new FileStream("templates.tst/frame.html", FileMode.CreateNew))
using (TextWriter tw = new StreamWriter(fs))
{
tw.Write("<!DOCTYPE html><html><head v-include=\"'head.html'\"/></html>");
tw.Flush();
}
using (FileStream fs = new FileStream("templates.tst/head.html", FileMode.CreateNew))
using (TextWriter tw = new StreamWriter(fs))
{
tw.Write("<head><title v-if=\"title != 'Schwurbel'\" v-for:title=\"['One HTML Template Test','Second Title','Schwurbel','Last Title']\">{{ title }}</title></head>");
tw.Flush();
}
FileSystemTemplateSource fileSystemTemplateSource = new FileSystemTemplateSource("templates.tst");
TemplateDocument templateDocument = fileSystemTemplateSource.GetTemplateByPath("frame.html");
StringWriter stringWriter = new StringWriter();
RenderContext renderContext = new RenderContext(stringWriter,fileSystemTemplateSource);
templateDocument.RenderTemplate(renderContext);
Console.WriteLine("Document rendered to: {0}", stringWriter.ToString());
}
}
}