view Constraints/ArrayContainsConstraintTest.cs @ 1:6ba8ac49a923

Re #46: Add NUnit helper methods * First import of test project * Add tests for initial "Array contains"
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Aug 2010 14:24:41 +0000
parents
children 664e47280811
line wrap: on
line source

//  This file (ArrayContainsConstraintTest.cs) is a part of the IBBoard.NUnit.Tests project and is copyright 2010 IBBoard
// 
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
using System;
using NUnit.Framework;
using IBBoard.NUnit.Constraints;
using NUnit.Framework.SyntaxHelpers;
namespace IBBoard.NUnit.Tests
{
	[TestFixture()]
	public class ArrayContainsConstraintTest
	{
		[Test()]
		public void TestArrayContainsThrowsExceptionForNonArray()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>(new string[0]);
			
			try
			{
				constraint.Matches(new object());
				Assert.Fail("Exception not thrown");
			}
			catch (ArgumentException ex)
			{
				Assert.That(ex.Message, Is.EqualTo("The actual value must be an array"));
			}
		}
		
		[Test()]
		public void TestArrayContainsReturnsFalseForSimpleNonMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] { "something" }), Is.False);
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
			Assert.That(constraint.Matches(new string[0]), Is.False);
			
			constraint = new ArrayContainsConstraint<string>();
			Assert.That(constraint.Matches(new string[] { "something" }), Is.False);
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
		}
		
		[Test()]
		public void TestArrayContainsReturnsTrueForSimpleMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] { "str" }), Is.True);
			constraint = new ArrayContainsConstraint<string>("");
			Assert.That(constraint.Matches(new string[] { "" }), Is.True);
			
			constraint = new ArrayContainsConstraint<string>();
			Assert.That(constraint.Matches(new string[0]), Is.True);
		}
		
		[Test()]
		public void TestArrayContainsReturnsCorrectErrorForSimpleNonMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] { "something" }), Is.False);
			MessageWriter mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Console.WriteLine(mw.ToString());
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  \"something\"\n  Incorrect value at 0\n"));
			
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Console.WriteLine(mw.ToString());
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  <string.Empty>\n  Incorrect value at 0\n"));
			
			Assert.That(constraint.Matches(new string[0]), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Console.WriteLine(mw.ToString());
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  Empty array\n"));
			
			constraint = new ArrayContainsConstraint<string>();
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Console.WriteLine(mw.ToString());
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: Empty array\n  But was:  <string.Empty>\n"));
		}
	}
}