view Constraints/ArrayContainsConstraintTest.cs @ 7:c6dc68face9e default tip

* Update to v2.6 of NUnit and new syntax/API changes
author IBBoard <dev@ibboard.co.uk>
date Sun, 28 Apr 2013 19:32:03 +0100
parents 664e47280811
children
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.Constraints;

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 TestArrayContainsReturnsFalseForPartialMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something"
			}), Is.False);
			
			constraint = new ArrayContainsConstraint<string>("str", "something");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something",
				"other thing"
			}), Is.False);
		}

		[Test()]
		public void TestArrayContainsReturnsFalseForSubsetMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str", "something");
			Assert.That(constraint.Matches(new string[] { "str" }), Is.False);
			
			constraint = new ArrayContainsConstraint<string>("str", "something", "other thing");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something"
			}), 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 TestArrayContainsReturnsCorrectMessageForSimpleNonMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] { "something" }), Is.False);
			MessageWriter mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  \"something\"\n  Incorrect value at index 0\n"));
			
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  <string.Empty>\n  Incorrect value at index 0\n"));
			
			Assert.That(constraint.Matches(new string[0]), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"str\"\n  But was:  Empty array\n  Expected value at index 0\n"));
			
			constraint = new ArrayContainsConstraint<string>();
			Assert.That(constraint.Matches(new string[] { "" }), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: Empty array\n  But was:  <string.Empty>\n  Unexpected value at index 0\n"));
		}
			
		[Test()]
		public void TestArrayContainsReturnsCorrectMessageForPartialMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something"
			}), Is.False);
			MessageWriter mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: End of array\n  But was:  \"something\"\n  Unexpected value at index 1\n"));
			
			constraint = new ArrayContainsConstraint<string>("str", "something");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something",
				"other thing"
			}), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: End of array\n  But was:  \"other thing\"\n  Unexpected value at index 2\n"));
		}

		[Test()]
		public void TestArrayContainsReturnsCorrectMessageForSubsetMatch()
		{
			ArrayContainsConstraint<string> constraint = new ArrayContainsConstraint<string>("str", "something");
			Assert.That(constraint.Matches(new string[] { "str" }), Is.False);			
			MessageWriter mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"something\"\n  But was:  End of array\n  Expected value at index 1\n"));
			
			constraint = new ArrayContainsConstraint<string>("str", "something", "other thing");
			Assert.That(constraint.Matches(new string[] {
				"str",
				"something"
			}), Is.False);
			mw = new TextMessageWriter();
			constraint.WriteMessageTo(mw);
			Assert.That(mw.ToString(), Is.EqualTo("  Expected: \"other thing\"\n  But was:  End of array\n  Expected value at index 2\n"));
		}
	}
}