PageRenderTime 46ms CodeModel.GetById 37ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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