PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/SharpTAL.Tests/TALTests/TALDefineTests.cs

https://bitbucket.org/rlacko/sharptal
C# | 123 lines | 106 code | 17 blank | 0 comment | 0 complexity | a2849f7344e2ff32e6b09f0c570739ab 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. [TestFixture]
  12. public class TALDefineTests
  13. {
  14. public static Dictionary<string, object> globals;
  15. [TestFixtureSetUp]
  16. public void SetUpClass()
  17. {
  18. }
  19. [TestFixtureTearDown]
  20. public void CleanupClass()
  21. {
  22. }
  23. [SetUp]
  24. public void SetUp()
  25. {
  26. globals = new Dictionary<string, object>();
  27. globals.Add("test", "testing"); ;
  28. globals.Add("one", new List<object>() { 1 });
  29. globals.Add("two", new List<object>() { "one", "two" });
  30. globals.Add("three", new List<object>() { 1, "Two", 3 });
  31. }
  32. public static void RunTest(string template, string expected, string errMsg)
  33. {
  34. try
  35. {
  36. string actual = new Template(template).Render(globals);
  37. actual = actual.Replace("{", "{{").Replace("}", "}}");
  38. Assert.AreEqual(expected, actual, "{1} - {0}template: {2}{0}actual: {3}{0}expected: {4}",
  39. Environment.NewLine, errMsg, template, actual, expected);
  40. }
  41. finally
  42. {
  43. }
  44. }
  45. [Test]
  46. public void TestDefineString()
  47. {
  48. RunTest(@"<html tal:define=""def1 test""><p tal:content=""def1""></p></html>",
  49. "<html><p>testing</p></html>", "Simple string define failed.");
  50. }
  51. [Test]
  52. public void TestDefineList()
  53. {
  54. RunTest(@"<html tal:define=""def1 two""><p tal:repeat=""var def1"">Hello <b tal:content=""var""></b></p></html>",
  55. "<html><p>Hello <b>one</b></p><p>Hello <b>two</b></p></html>", "List define failed.");
  56. }
  57. [Test]
  58. public void TestDefineGlobal()
  59. {
  60. RunTest(@"<html><p tal:define=""global def1 test""></p><p tal:content=""def1""></p></html>"
  61. , "<html><p></p><p>testing</p></html>", "Global did not set globally");
  62. }
  63. [Test]
  64. public void TestDefineGlobalMultiple()
  65. {
  66. RunTest(@"<html><p tal:define='global def1 ""a""; global def1 ""b""; global def1 test'></p><p tal:content=""def1""></p></html>"
  67. , "<html><p></p><p>testing</p></html>", "Global did not set globally");
  68. }
  69. [Test]
  70. public void TestDefineNonLocal()
  71. {
  72. RunTest(@"<html><p tal:define='def1 ""a""'><tal:def define='nonlocal def1 test'></tal:def><p tal:content=""def1""></p></p></html>"
  73. , "<html><p><p>testing</p></p></html>", "Local did not set");
  74. }
  75. [Test]
  76. public void TestDefineDefault()
  77. {
  78. RunTest(@"<html><p tal:define=""global test default""></p><p tal:content=""test"">Can you see me?</p></html>"
  79. , "<html><p></p><p>Can you see me?</p></html>", "Default variable did not define proplerly.");
  80. }
  81. [Test]
  82. public void TestDefineNothing()
  83. {
  84. RunTest(@"<html><p tal:define=""global test null""></p><p tal:content=""test"">Can you see me?</p></html>"
  85. , "<html><p></p><p></p></html>", "Nothing variable did not define proplerly.");
  86. }
  87. [Test]
  88. public void TestDefineMultipleLocal()
  89. {
  90. RunTest(@"<html><div tal:define=""firstVar test;secondVar string:This is a semi;;colon;thirdVar string:Test""><p tal:content=""test"">Testing</p><p tal:content=""secondVar""></p><p tal:content=""thirdVar""></p></div></html>"
  91. , "<html><div><p>testing</p><p>This is a semi;colon</p><p>Test</p></div></html>", "Multiple defines failed.");
  92. }
  93. [Test]
  94. public void TestDefineMultipleMixed()
  95. {
  96. RunTest(@"<html><div tal:define=""firstVar test;global secondVar string:This is a semi;;colon;thirdVar string:Test""><p tal:content=""test"">Testing</p><p tal:content=""secondVar""></p><p tal:content=""thirdVar""></p></div><b tal:content=""secondVar""></b></html>"
  97. , "<html><div><p>testing</p><p>This is a semi;colon</p><p>Test</p></div><b>This is a semi;colon</b></html>", "Multiple defines failed.");
  98. }
  99. [Test]
  100. public void TestDefineMultipleLocalRef()
  101. {
  102. RunTest(@"<html><div tal:define=""firstVar test;secondVar firstVar""><p tal:content=""test"">Testing</p><p tal:content=""secondVar""></p></div></html>"
  103. , "<html><div><p>testing</p><p>testing</p></div></html>", "Multiple local defines with references to earlier define failed.");
  104. }
  105. }
  106. }