/source/test/NOS.Registration.Tests/NVelocityEntryFormatterSpecs.cs

http://github.com/agross/netopenspace · C# · 153 lines · 127 code · 26 blank · 0 comment · 0 complexity · e9db0f1653fae6f5388fcad0a8a699b3 MD5 · raw file

  1. using System;
  2. using Machine.Specifications;
  3. using Rhino.Mocks;
  4. namespace NOS.Registration.Tests
  5. {
  6. public abstract class With_entry_formatter
  7. {
  8. protected static string Entry;
  9. protected static IEntryFormatter Formatter;
  10. protected static User User;
  11. protected static ISettings Settings;
  12. Establish context = () =>
  13. {
  14. Formatter = new NVelocityEntryFormatter();
  15. Settings = MockRepository.GenerateStub<ISettings>();
  16. User = new User("user")
  17. {
  18. Data =
  19. {
  20. Twitter = "twitter",
  21. Xing = "xing"
  22. }
  23. };
  24. };
  25. }
  26. [Subject(typeof(NVelocityEntryFormatter))]
  27. public class When_a_simple_entry_with_escaped_newlines_in_the_template_is_formatted
  28. : With_entry_formatter
  29. {
  30. Because of = () => { Entry = Formatter.FormatUserEntry(User, Settings, "some template\\n\\n"); };
  31. It should_convert_escaped_newline_characters_to_newline_characters = () => Entry.ShouldEndWith("\n\n");
  32. }
  33. [Subject(typeof(NVelocityEntryFormatter))]
  34. public class When_a_simple_entry_with_a_placeholder_is_formatted : With_entry_formatter
  35. {
  36. Because of = () => { Entry = Formatter.FormatUserEntry(User, Settings, "$user.UserName"); };
  37. It should_fill_the_template_with_the_user_name = () => Entry.ShouldEqual("user");
  38. }
  39. [Subject(typeof(NVelocityEntryFormatter))]
  40. public class When_an_entry_with_conditional_placeholders_is_formatted : With_entry_formatter
  41. {
  42. static string EntryTemplate;
  43. Establish context = () =>
  44. {
  45. EntryTemplate = "#if($user.Data.Twitter)" +
  46. "$user.Data.Twitter was given" +
  47. "#end" +
  48. "#if($user.Data.Xing)" +
  49. "$user.Data.Xing was given" +
  50. "#end";
  51. User = new User("user")
  52. {
  53. Data =
  54. {
  55. Twitter = "twitter",
  56. Xing = String.Empty
  57. }
  58. };
  59. };
  60. Because of = () => { Entry = Formatter.FormatUserEntry(User, Settings, EntryTemplate); };
  61. It should_fill_the_template_with_satisfied_conditonals = () => Entry.ShouldContain("twitter was given");
  62. It should_turn_empty_strings_into_null_values = () => Entry.ShouldEqual("twitter was given");
  63. }
  64. [Subject(typeof(NVelocityEntryFormatter))]
  65. public class When_an_entry_with_conditional_decimal_placeholders_is_formatted : With_entry_formatter
  66. {
  67. static string EntryTemplate;
  68. Establish context = () =>
  69. {
  70. EntryTemplate = "#if($user.Data.Sponsoring > 0)" +
  71. "$user.Data.Sponsoring was given" +
  72. "#end";
  73. User = new User("user")
  74. {
  75. Data =
  76. {
  77. Sponsoring = 0.01m
  78. }
  79. };
  80. };
  81. Because of = () => { Entry = Formatter.FormatUserEntry(User, Settings, EntryTemplate); };
  82. It should_fill_the_template_with_satisfied_conditonals = () => Entry.ShouldContain("was given");
  83. }
  84. [Subject(typeof(NVelocityEntryFormatter))]
  85. public class When_a_complex_entry_is_formatted : With_entry_formatter
  86. {
  87. static string EntryTemplate;
  88. Establish context = () =>
  89. {
  90. EntryTemplate = @"# $user.Data.Name" +
  91. "#if($user.Data.Email)" +
  92. ", [$user.Data.Email|E-Mail]" +
  93. "#end" +
  94. "#if($user.Data.Blog)" +
  95. ", [$user.Data.Blog|Blog]" +
  96. "#end" +
  97. "#if($user.Data.Twitter)" +
  98. ", [http://twitter.com/$user.Data.Twitter/|Twitter]" +
  99. "#end" +
  100. "#if($user.Data.Xing)" +
  101. ", [http://xing.com/$user.Data.Xing/|XING]" +
  102. "#end" +
  103. "#if($user.Data.Picture)" +
  104. ", [$user.Data.Picture|Bild]" +
  105. "#end\n\n";
  106. User = new User("user")
  107. {
  108. Data =
  109. {
  110. Name = "Peter Pan",
  111. Blog = "blog",
  112. Email = "foo@example.com",
  113. Picture = "picture",
  114. Twitter = "twitter",
  115. Xing = "xing"
  116. }
  117. };
  118. };
  119. Because of = () => { Entry = Formatter.FormatUserEntry(User, Settings, EntryTemplate); };
  120. It should_fill_the_template_with_just_the_satisfied_conditonals =
  121. () =>
  122. Entry.ShouldStartWith(
  123. "# Peter Pan, [foo@example.com|E-Mail], [blog|Blog], [http://twitter.com/twitter/|Twitter], [http://xing.com/xing/|XING], [picture|Bild]");
  124. It should_replace_double_newline_at_the_end_with_a_single_newline =
  125. () => Entry.ShouldEndWith("]\n");
  126. }
  127. }