PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpTAL.Tests/TALTests/TALContentTests.cs

https://bitbucket.org/rlacko/sharptal
C# | 137 lines | 121 code | 16 blank | 0 comment | 0 complexity | 92c332f238cc408a6940914279eee2b3 MD5 | raw file
Possible License(s): Apache-2.0
  1. namespace SharpTAL.SharpTALTests.TALTests
  2. {
  3. using System;
  4. using System.Text;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.IO;
  8. using System.Reflection;
  9. using NUnit.Framework;
  10. using SharpTAL.TemplateCache;
  11. public static class Extensions
  12. {
  13. public static string ToUpperExtension(this string s)
  14. {
  15. return s.ToUpper();
  16. }
  17. }
  18. [TestFixture]
  19. public class TALContentTests
  20. {
  21. public static Dictionary<string, object> globals;
  22. [TestFixtureSetUp]
  23. public void SetUpClass()
  24. {
  25. }
  26. [TestFixtureTearDown]
  27. public void CleanupClass()
  28. {
  29. }
  30. [SetUp]
  31. public void SetUp()
  32. {
  33. string entry = @"Some structure: <b tal:content=""weblog/subject""></b>";
  34. Dictionary<string, string> weblog = new Dictionary<string, string>
  35. {
  36. { "subject", "Test subject" },
  37. { "entry", entry }
  38. };
  39. globals = new Dictionary<string, object>();
  40. globals.Add("test", "testing");
  41. globals.Add("one", new List<object>() { 1 });
  42. globals.Add("two", new List<object>() { "one", "two" });
  43. globals.Add("three", new List<object>() { 1, "Two", 3 });
  44. globals.Add("weblog", weblog);
  45. }
  46. public static void RunTest(string template, string expected, string errMsg)
  47. {
  48. List<Assembly> referencedAssemblies = new List<Assembly>()
  49. {
  50. typeof(TALContentTests).Assembly
  51. };
  52. string actual = new Template(template, referencedAssemblies).Render(globals);
  53. actual = actual.Replace("{", "{{").Replace("}", "}}");
  54. Assert.AreEqual(expected, actual, "{1} - {0}template: {2}{0}actual: {3}{0}expected: {4}",
  55. Environment.NewLine, errMsg, template, actual, expected);
  56. }
  57. [Test]
  58. public void TestContentExtensionMethods()
  59. {
  60. RunTest(@"<html><p tal:content=""test.ToUpperExtension()"">Original</p></html>"
  61. , "<html><p>TESTING</p></html>"
  62. , "Content of string did not evaluate to contain string");
  63. }
  64. [Test]
  65. public void TestContentNull()
  66. {
  67. RunTest(@"<html><p tal:content=""null""></p></html>"
  68. , "<html><p></p></html>"
  69. , "Content of nothing did not evaluate to empty tag.");
  70. }
  71. [Test]
  72. public void TestContentDefault()
  73. {
  74. RunTest(@"<html><p tal:content=""default"">Original</p></html>"
  75. , "<html><p>Original</p></html>"
  76. , "Content of default did not evaluate to existing content");
  77. }
  78. [Test]
  79. public void TestContentString()
  80. {
  81. RunTest(@"<html><p tal:content=""test"">Original</p></html>"
  82. , "<html><p>testing</p></html>"
  83. , "Content of string did not evaluate to contain string");
  84. }
  85. [Test]
  86. public void TestContentStructure()
  87. {
  88. Dictionary<string, object> weblog = new Dictionary<string, object>
  89. {
  90. { "subject", "Test subject" },
  91. };
  92. globals["weblog"] = weblog;
  93. string macros = @"<p metal:define-macro=""entry"">Some structure: <b tal:content='weblog[""subject""]'></b></p>";
  94. RunTest(macros + @"<html><p metal:use-macro='macros[""entry""]'>Original</p></html>"
  95. , "<html><p>Some structure: <b>Test subject</b></p></html>"
  96. , "Content of Structure did not evaluate to expected result");
  97. }
  98. [Test]
  99. public void TestTALDisabledContentStructure()
  100. {
  101. RunTest(@"<html><p tal:content='structure weblog[""entry""]'>Original</p></html>"
  102. , @"<html><p>Some structure: <b tal:content=""weblog/subject""></b></p></html>"
  103. , "Content of Structure did not evaluate to expected result");
  104. }
  105. [Test]
  106. public void TestTALContentEscaping()
  107. {
  108. RunTest(@"<html><p tal:content='string:=&=&amp;=&nbsp;=&lt;=&gt;=&quot;=""='>Original</p>=&=&amp;=&nbsp;=&lt;=&gt;=&quot;=""=</html>"
  109. , @"<html><p>=&amp;=&amp;=&nbsp;=&lt;=&gt;=""=""=</p>=&=&amp;=&nbsp;=&lt;=&gt;=&quot;=""=</html>"
  110. , "Escaped content did not evaluate to expected result");
  111. }
  112. [Test]
  113. public void TestMultilineTag()
  114. {
  115. RunTest(@"<html><p
  116. tal:content='string:str'>Original</p></html>"
  117. , @"<html><p>str</p></html>"
  118. , "Multiline tag did not evaluate to expected result");
  119. }
  120. }
  121. }