Skip to content

Commit

Permalink
Added LiteralText tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed Oct 29, 2010
1 parent 4238dd6 commit fb646ce
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Nustache.Core/LiteralText.cs
@@ -1,3 +1,5 @@
using System;

namespace Nustache.Core
{
public class LiteralText : Part
Expand All @@ -6,6 +8,11 @@ public class LiteralText : Part

public LiteralText(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}

_text = text;
}

Expand All @@ -16,13 +23,6 @@ public override void Render(RenderContext context)

#region Boring stuff

public bool Equals(LiteralText other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._text, _text);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
Expand All @@ -31,9 +31,16 @@ public override bool Equals(object obj)
return Equals((LiteralText)obj);
}

public bool Equals(LiteralText other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._text, _text);
}

public override int GetHashCode()
{
return (_text != null ? _text.GetHashCode() : 0);
return _text.GetHashCode();
}

public override string ToString()
Expand Down
75 changes: 75 additions & 0 deletions Nustache.Tests/Describe_LiteralText.cs
@@ -0,0 +1,75 @@
using System;
using System.IO;
using NUnit.Framework;
using Nustache.Core;

namespace Nustache.Tests
{
[TestFixture]
public class Describe_LiteralText
{
[Test]
public void It_cant_be_constructed_with_null_text()
{
Assert.Throws<ArgumentNullException>(() => new LiteralText(null));
}

[Test]
public void It_renders_its_text()
{
var a = new LiteralText("a");
var writer = new StringWriter();
var context = new RenderContext(null, null, writer, null);

a.Render(context);

Assert.AreEqual("a", writer.GetStringBuilder().ToString());
}

[Test]
public void It_has_a_useful_Equals_method()
{
object a = new LiteralText("a");
object a2 = new LiteralText("a");
object b = new LiteralText("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_LiteralText_objects()
{
var a = new LiteralText("a");
var a2 = new LiteralText("a");
var b = new LiteralText("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 LiteralText("a");

Assert.AreNotEqual(0, a.GetHashCode());
}

[Test]
public void It_has_a_useful_ToString_method()
{
var a = new LiteralText("a");

Assert.AreEqual("LiteralText(\"a\")", a.ToString());
}
}
}
1 change: 1 addition & 0 deletions Nustache.Tests/Nustache.Tests.csproj
Expand Up @@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Describe_LiteralText.cs" />
<Compile Include="Describe_VariableReference.cs" />
<Compile Include="Describe_Render.cs" />
<Compile Include="Describe_Section.cs" />
Expand Down

0 comments on commit fb646ce

Please sign in to comment.