diff --git a/Nustache.Core/LiteralText.cs b/Nustache.Core/LiteralText.cs index 113bcd6..76fcbad 100644 --- a/Nustache.Core/LiteralText.cs +++ b/Nustache.Core/LiteralText.cs @@ -1,3 +1,5 @@ +using System; + namespace Nustache.Core { public class LiteralText : Part @@ -6,6 +8,11 @@ public class LiteralText : Part public LiteralText(string text) { + if (text == null) + { + throw new ArgumentNullException("text"); + } + _text = text; } @@ -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; @@ -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() diff --git a/Nustache.Tests/Describe_LiteralText.cs b/Nustache.Tests/Describe_LiteralText.cs new file mode 100644 index 0000000..7ee4a24 --- /dev/null +++ b/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(() => 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()); + } + } +} \ No newline at end of file diff --git a/Nustache.Tests/Nustache.Tests.csproj b/Nustache.Tests/Nustache.Tests.csproj index 566fd14..4396248 100644 --- a/Nustache.Tests/Nustache.Tests.csproj +++ b/Nustache.Tests/Nustache.Tests.csproj @@ -49,6 +49,7 @@ +