Permalink
Browse files

Renamed VariableMarker. Added tests.

  • Loading branch information...
1 parent 2bff365 commit 4238dd68ebd50c86f76e5f8d3b3167571e08ddeb @jdiamond jdiamond committed Oct 29, 2010
@@ -60,7 +60,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TemplateInclude.cs" />
<Compile Include="ValueGetter.cs" />
- <Compile Include="VariableMarker.cs" />
+ <Compile Include="VariableReference.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
@@ -41,7 +41,7 @@ public IEnumerable<Part> Scan(string template)
}
else if (marker[0] != '!')
{
- yield return new VariableMarker(m.Groups[1].Value);
+ yield return new VariableReference(m.Groups[1].Value);
}
i = m.Index + m.Length;
@@ -1,11 +1,18 @@
+using System;
+
namespace Nustache.Core
{
- public class VariableMarker : Part
+ public class VariableReference : Part
{
private readonly string _name;
- public VariableMarker(string name)
+ public VariableReference(string name)
{
+ if (name == null)
+ {
+ throw new ArgumentNullException("name");
+ }
+
_name = name;
}
@@ -21,29 +28,29 @@ public override void Render(RenderContext context)
#region Boring stuff
- public bool Equals(VariableMarker other)
- {
- if (ReferenceEquals(null, other)) return false;
- if (ReferenceEquals(this, other)) return true;
- return Equals(other._name, _name);
- }
-
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
- if (obj.GetType() != typeof(VariableMarker)) return false;
- return Equals((VariableMarker)obj);
+ if (obj.GetType() != typeof(VariableReference)) return false;
+ return Equals((VariableReference)obj);
+ }
+
+ public bool Equals(VariableReference other)
+ {
+ if (ReferenceEquals(null, other)) return false;
+ if (ReferenceEquals(this, other)) return true;
+ return Equals(other._name, _name);
}
public override int GetHashCode()
{
- return (_name != null ? _name.GetHashCode() : 0);
+ return _name.GetHashCode();
}
public override string ToString()
{
- return string.Format("VariableMarker(\"{0}\")", _name);
+ return string.Format("VariableReference(\"{0}\")", _name);
}
#endregion
@@ -28,8 +28,8 @@ public void It_combines_sections()
{
new LiteralText("before"),
new Block("foo",
- new LiteralText("inside"),
- new EndSection("foo")),
+ new LiteralText("inside"),
+ new EndSection("foo")),
new LiteralText("after")
},
template.Parts.ToArray());
@@ -60,12 +60,12 @@ public void It_handles_nested_sections()
{
new LiteralText("before foo"),
new Block("foo",
- new LiteralText("before bar"),
- new Block("bar",
- new LiteralText("inside bar"),
- new EndSection("bar")),
- new LiteralText("after bar"),
- new EndSection("foo")),
+ new LiteralText("before bar"),
+ new Block("bar",
+ new LiteralText("inside bar"),
+ new EndSection("bar")),
+ new LiteralText("after bar"),
+ new EndSection("foo")),
new LiteralText("after foo")
},
template.Parts.ToArray());
@@ -96,12 +96,12 @@ public void It_handles_nested_sections_with_the_same_name()
{
new LiteralText("before foo 1"),
new Block("foo",
- new LiteralText("before foo 2"),
- new Block("foo",
- new LiteralText("inside foo 2"),
- new EndSection("foo")),
- new LiteralText("after foo 2"),
- new EndSection("foo")),
+ new LiteralText("before foo 2"),
+ new Block("foo",
+ new LiteralText("inside foo 2"),
+ new EndSection("foo")),
+ new LiteralText("after foo 2"),
+ new EndSection("foo")),
new LiteralText("after foo 1")
},
template.Parts.ToArray());
@@ -113,15 +113,32 @@ public void It_throws_when_the_end_section_does_not_match_the_start_section()
var parser = new Parser();
var template = new Template();
- Assert.Catch<NustacheException>(() => parser.Parse(template,
- new Part[]
- {
- new LiteralText("before"),
- new Block("foo"),
- new LiteralText("inside"),
- new EndSection("bar"),
- new LiteralText("after")
- }));
+ Assert.Catch<NustacheException>(
+ () => parser.Parse(template,
+ new Part[]
+ {
+ new LiteralText("before"),
+ new Block("foo"),
+ new LiteralText("inside"),
+ new EndSection("bar"),
+ new LiteralText("after")
+ }));
+ }
+
+ [Test]
+ public void It_throws_when_the_end_section_does_not_match_any_start_section()
+ {
+ var parser = new Parser();
+ var template = new Template();
+
+ Assert.Catch<NustacheException>(
+ () => parser.Parse(template,
+ new Part[]
+ {
+ new LiteralText("before"),
+ new EndSection("foo"),
+ new LiteralText("after")
+ }));
}
}
}
@@ -44,7 +44,7 @@ public void It_scans_variable_markers()
CollectionAssert.AreEqual(new Part[]
{
new LiteralText("before"),
- new VariableMarker("foo"),
+ new VariableReference("foo"),
new LiteralText("after"),
},
parts.ToArray());
@@ -0,0 +1,75 @@
+using System;
+using System.IO;
+using NUnit.Framework;
+using Nustache.Core;
+
+namespace Nustache.Tests
+{
+ [TestFixture]
+ public class Describe_VariableReference
+ {
+ [Test]
+ public void It_cant_be_constructed_with_a_null_name()
+ {
+ Assert.Throws<ArgumentNullException>(() => new VariableReference(null));
+ }
+
+ [Test]
+ public void It_renders_the_named_value_from_the_context()
+ {
+ var a = new VariableReference("a");
+ var writer = new StringWriter();
+ var context = new RenderContext(null, new { a = "b" }, writer, null);
+
+ a.Render(context);
+
+ Assert.AreEqual("b", writer.GetStringBuilder().ToString());
+ }
+
+ [Test]
+ public void It_has_a_useful_Equals_method()
+ {
+ object a = new VariableReference("a");
+ object a2 = new VariableReference("a");
+ object b = new VariableReference("b");
+
+ Assert.IsTrue(a.Equals(a));
+ Assert.IsTrue(a.Equals(a2));
+ Assert.IsTrue(a2.Equals(a));
+ Assert.IsFalse(a.Equals(b));
+ Assert.IsFalse(a.Equals(null));
+ Assert.IsFalse(a.Equals("a"));
+ }
+
+ [Test]
+ public void It_has_an_Equals_overload_for_other_VariableReference_objects()
+ {
+ var a = new VariableReference("a");
+ var a2 = new VariableReference("a");
+ var b = new VariableReference("b");
+
+ Assert.IsTrue(a.Equals(a));
+ Assert.IsTrue(a.Equals(a2));
+ Assert.IsTrue(a2.Equals(a));
+ Assert.IsFalse(a.Equals(b));
+ Assert.IsFalse(b.Equals(a));
+ Assert.IsFalse(a.Equals(null));
+ }
+
+ [Test]
+ public void It_has_a_useful_GetHashCode_method()
+ {
+ var a = new VariableReference("a");
+
+ Assert.AreNotEqual(0, a.GetHashCode());
+ }
+
+ [Test]
+ public void It_has_a_useful_ToString_method()
+ {
+ var a = new VariableReference("a");
+
+ Assert.AreEqual("VariableReference(\"a\")", a.ToString());
+ }
+ }
+}
@@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Describe_VariableReference.cs" />
<Compile Include="Describe_Render.cs" />
<Compile Include="Describe_Section.cs" />
<Compile Include="Describe_Parser_Parse.cs" />

0 comments on commit 4238dd6

Please sign in to comment.