PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpTAL.Tests/TALESTests/TALESCSharpPathTests.cs

https://bitbucket.org/rlacko/sharptal
C# | 125 lines | 107 code | 18 blank | 0 comment | 0 complexity | 934004d4c00b157efc8fd4ca896c4f16 MD5 | raw file
Possible License(s): Apache-2.0
  1. namespace SharpTAL.SharpTALTests.TALESTests
  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 TALESCSharpPathTests
  13. {
  14. public static Dictionary<string, object> globals;
  15. public delegate object TestFuncDelegate(string param);
  16. public delegate object TestFuncDelegate2();
  17. private static object simpleFunction(object param)
  18. {
  19. return string.Format("Hello {0}", param);
  20. }
  21. private static object helloFunction()
  22. {
  23. return "Hello";
  24. }
  25. public class TestingException : Exception
  26. {
  27. public TestingException()
  28. : base()
  29. {
  30. }
  31. public TestingException(string message)
  32. : base(message)
  33. {
  34. }
  35. }
  36. [TestFixtureSetUp]
  37. public void SetUpClass()
  38. {
  39. }
  40. [TestFixtureTearDown]
  41. public void CleanupClass()
  42. {
  43. }
  44. [SetUp]
  45. public void SetUp()
  46. {
  47. }
  48. public static void RunTest(string template, string expected, string errMsg)
  49. {
  50. globals = new Dictionary<string, object>();
  51. globals.Add("top", "Hello from the top");
  52. globals.Add("helloFunc", new TestFuncDelegate(simpleFunction));
  53. globals.Add("helloFunction", new TestFuncDelegate2(helloFunction));
  54. globals.Add("myList", new List<int>() { 1, 2, 3, 4, 5, 6 });
  55. globals.Add("testing", "testing");
  56. globals.Add("map", new Dictionary<string, object>() { { "test", "maptest" } });
  57. globals.Add("data", new Dictionary<string, object>() { { "one", 1 }, { "zero", 0 } });
  58. string actual = new Template(template).Render(globals);
  59. actual = actual.Replace("{", "{{").Replace("}", "}}");
  60. Assert.AreEqual(expected, actual, "{1} - {0}template: {2}{0}actual: {3}{0}expected: {4}",
  61. Environment.NewLine, errMsg, template, actual, expected);
  62. }
  63. [Test]
  64. public void TestCSharpPathFuncSuccess()
  65. {
  66. RunTest(@"<html tal:content='csharp:helloFunc (""Colin!"")'>Exists</html>",
  67. "<html>Hello Colin!</html>",
  68. "CSharp path with function failed.");
  69. }
  70. [Test]
  71. public void TestCSharpPathSliceSuccess()
  72. {
  73. RunTest(@"<html tal:repeat=""num csharp:myList.GetRange(2, 2)"" tal:content=""num"">Exists</html>",
  74. "<html>3</html><html>4</html>",
  75. "CSharp path with slice failed.");
  76. }
  77. [Test]
  78. public void TestCSharpPathArrayCreate()
  79. {
  80. RunTest(@"<html tal:repeat=""i csharp:from i in myList.GetRange(0, myList.Count - 1) where i &lt; 3 select i"" tal:content=""i"">Exists</html>",
  81. "<html>1</html><html>2</html>",
  82. "CSharp path with array create with slice failed.");
  83. }
  84. [Test]
  85. public void TestCSharpStringCompare()
  86. {
  87. RunTest(@"<html tal:condition='csharp: testing==""testing""'>Passed.</html>",
  88. "<html>Passed.</html>",
  89. "CSharp string compare failed.");
  90. }
  91. [Test]
  92. public void TestSystemString()
  93. {
  94. RunTest(@"<html tal:condition='csharp: ""abc"" == string.Format(""{0}{1}{2}"", ""a"", ""b"", ""c"")'>Passed.</html>",
  95. "<html>Passed.</html>",
  96. "CSharp string compare failed.");
  97. }
  98. [Test]
  99. public void TestSystemDateTime()
  100. {
  101. string year = System.DateTime.Now.ToString("yyyy");
  102. RunTest(@"<html tal:content='csharp: DateTime.Now.ToString(""yyyy"")'></html>",
  103. "<html>" + year + "</html>",
  104. "CSharp string compare failed.");
  105. }
  106. }
  107. }