📄 assertsyntaxtests.cs
字号:
Assert.That(2 + 2 == 4);
Assert.That(i3, Is.EqualTo(d3));
Assert.That(2 + 2, Is.Not.EqualTo(5));
Assert.That(i3, Is.Not.EqualTo(iunequal));
// Inherited syntax
Expect(2 + 2, EqualTo(4));
Expect(2 + 2 == 4);
Expect(i3, EqualTo(d3));
Expect(2 + 2, Not.EqualTo(5));
Expect(i3, Not.EqualTo(iunequal));
}
[Test]
public void EqualityTestsWithTolerance()
{
// CLassic syntax
Assert.AreEqual(5.0d, 4.99d, 0.05d);
Assert.AreEqual(5.0f, 4.99f, 0.05f);
// Helper syntax
Assert.That(4.99d, Is.EqualTo(5.0d).Within(0.05d));
Assert.That(4.99f, Is.EqualTo(5.0f).Within(0.05f));
// Inherited syntax
Expect(4.99d, EqualTo(5.0d).Within(0.05d));
Expect(4.99f, EqualTo(5.0f).Within(0.05f));
}
[Test]
public void ComparisonTests()
{
// Classic Syntax
Assert.Greater(7, 3);
Assert.GreaterOrEqual(7, 3);
Assert.GreaterOrEqual(7, 7);
// Helper syntax
Assert.That(7, Is.GreaterThan(3));
Assert.That(7, Is.GreaterThanOrEqualTo(3));
Assert.That(7, Is.AtLeast(3));
Assert.That(7, Is.GreaterThanOrEqualTo(7));
Assert.That(7, Is.AtLeast(7));
// Inherited syntax
Expect(7, GreaterThan(3));
Expect(7, GreaterThanOrEqualTo(3));
Expect(7, AtLeast(3));
Expect(7, GreaterThanOrEqualTo(7));
Expect(7, AtLeast(7));
// Classic syntax
Assert.Less(3, 7);
Assert.LessOrEqual(3, 7);
Assert.LessOrEqual(3, 3);
// Helper syntax
Assert.That(3, Is.LessThan(7));
Assert.That(3, Is.LessThanOrEqualTo(7));
Assert.That(3, Is.AtMost(7));
Assert.That(3, Is.LessThanOrEqualTo(3));
Assert.That(3, Is.AtMost(3));
// Inherited syntax
Expect(3, LessThan(7));
Expect(3, LessThanOrEqualTo(7));
Expect(3, AtMost(7));
Expect(3, LessThanOrEqualTo(3));
Expect(3, AtMost(3));
}
[Test]
public void AllItemsTests()
{
object[] ints = new object[] { 1, 2, 3, 4 };
object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
// Classic syntax
CollectionAssert.AllItemsAreNotNull(ints);
CollectionAssert.AllItemsAreInstancesOfType(ints, typeof(int));
CollectionAssert.AllItemsAreInstancesOfType(strings, typeof(string));
CollectionAssert.AllItemsAreUnique(ints);
// Helper syntax
Assert.That(ints, Is.All.Not.Null);
Assert.That(ints, Is.All.InstanceOfType(typeof(int)));
Assert.That(strings, Is.All.InstanceOfType(typeof(string)));
Assert.That(ints, Is.Unique);
// Only available using new syntax
Assert.That(strings, Is.Not.Unique);
Assert.That(ints, Is.All.GreaterThan(0));
Assert.That(strings, Text.All.Contains( "a" ) );
Assert.That(strings, List.Some.StartsWith( "ba" ) );
// Inherited syntax
Expect(ints, All.Not.Null);
Expect(ints, All.InstanceOfType(typeof(int)));
Expect(strings, All.InstanceOfType(typeof(string)));
Expect(ints, Unique);
// Only available using new syntax
Expect(strings, Not.Unique);
Expect(ints, All.GreaterThan(0));
Expect(strings, All.Contains( "a" ) );
Expect(strings, Some.StartsWith( "ba" ) );
}
[Test]
public void SomeItemsTests()
{
object[] mixed = new object[] { 1, 2, "3", null, "four", 100 };
object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
// Not available using the classic syntax
// Helper syntax
Assert.That(mixed, Has.Some.Null);
Assert.That(mixed, Has.Some.InstanceOfType(typeof(int)));
Assert.That(mixed, Has.Some.InstanceOfType(typeof(string)));
Assert.That(mixed, Has.Some.GreaterThan(99));
Assert.That(strings, Has.Some.StartsWith( "ba" ) );
Assert.That(strings, Has.Some.Not.StartsWith( "ba" ) );
// Inherited syntax
Expect(mixed, Some.Null);
Expect(mixed, Some.InstanceOfType(typeof(int)));
Expect(mixed, Some.InstanceOfType(typeof(string)));
Expect(mixed, Some.GreaterThan(99));
Expect(strings, Some.StartsWith( "ba" ) );
Expect(strings, Some.Not.StartsWith( "ba" ) );
}
[Test]
public void NoItemsTests()
{
object[] ints = new object[] { 1, 2, 3, 4, 5 };
object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
// Not available using the classic syntax
// Helper syntax
Assert.That(ints, Has.None.Null);
Assert.That(ints, Has.None.InstanceOfType(typeof(string)));
Assert.That(ints, Has.None.GreaterThan(99));
Assert.That(strings, Has.None.StartsWith( "qu" ) );
// Inherited syntax
Expect(ints, None.Null);
Expect(ints, None.InstanceOfType(typeof(string)));
Expect(ints, None.GreaterThan(99));
Expect(strings, None.StartsWith( "qu" ) );
}
[Test]
public void CollectionContainsTests()
{
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
// Classic syntax
Assert.Contains(3, iarray);
Assert.Contains("b", sarray);
CollectionAssert.Contains(iarray, 3);
CollectionAssert.Contains(sarray, "b");
CollectionAssert.DoesNotContain(sarray, "x");
// Helper syntax
Assert.That(iarray, List.Contains(3));
Assert.That(sarray, List.Contains("b"));
Assert.That(sarray, List.Not.Contains("x"));
// Inherited syntax
Expect(iarray, Contains(3));
Expect(sarray, Contains("b"));
Expect(sarray, Not.Contains("x"));
}
[Test]
public void CollectionEquivalenceTests()
{
int[] ints1to5 = new int[] { 1, 2, 3, 4, 5 };
// Classic syntax
CollectionAssert.AreEquivalent(new int[] { 2, 1, 4, 3, 5 }, ints1to5);
CollectionAssert.AreNotEquivalent(new int[] { 2, 2, 4, 3, 5 }, ints1to5);
CollectionAssert.AreNotEquivalent(new int[] { 2, 4, 3, 5 }, ints1to5);
CollectionAssert.AreEquivalent(new int[] { 2, 2, 1, 1, 4, 3, 5 }, ints1to5);
// Helper syntax
Assert.That(new int[] { 2, 1, 4, 3, 5 }, Is.EquivalentTo(ints1to5));
Assert.That(new int[] { 2, 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5));
Assert.That(new int[] { 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5));
Assert.That(new int[] { 2, 2, 1, 1, 4, 3, 5 }, Is.EquivalentTo(ints1to5));
// Inherited syntax
Expect(new int[] { 2, 1, 4, 3, 5 }, EquivalentTo(ints1to5));
Expect(new int[] { 2, 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5));
Expect(new int[] { 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5));
Expect(new int[] { 2, 2, 1, 1, 4, 3, 5 }, EquivalentTo(ints1to5));
}
[Test]
public void SubsetTests()
{
int[] ints1to5 = new int[] { 1, 2, 3, 4, 5 };
// Classic syntax
CollectionAssert.IsSubsetOf(new int[] { 1, 3, 5 }, ints1to5);
CollectionAssert.IsSubsetOf(new int[] { 1, 2, 3, 4, 5 }, ints1to5);
CollectionAssert.IsNotSubsetOf(new int[] { 2, 4, 6 }, ints1to5);
// Helper syntax
Assert.That(new int[] { 1, 3, 5 }, Is.SubsetOf(ints1to5));
Assert.That(new int[] { 1, 2, 3, 4, 5 }, Is.SubsetOf(ints1to5));
Assert.That(new int[] { 2, 4, 6 }, Is.Not.SubsetOf(ints1to5));
// Inherited syntax
Expect(new int[] { 1, 3, 5 }, SubsetOf(ints1to5));
Expect(new int[] { 1, 2, 3, 4, 5 }, SubsetOf(ints1to5));
Expect(new int[] { 2, 4, 6 }, Not.SubsetOf(ints1to5));
}
[Test]
public void PropertyTests()
{
string[] array = new string[] { "abc", "bca", "xyz" };
// Helper syntax
Assert.That( "Hello", Has.Property("Length", 5) );
Assert.That( "Hello", Has.Length( 5 ) );
Assert.That( array , Has.All.Property( "Length", 3 ) );
Assert.That( array, Has.All.Length( 3 ) );
// Inherited syntax
Expect( "Hello", Property("Length", 5) );
Expect( "Hello", Length( 5 ) );
Expect( array, All.Property("Length", 3 ) );
Expect( array, All.Length( 3 ) );
}
[Test]
public void NotTests()
{
// Not available using the classic syntax
// Helper syntax
Assert.That(42, Is.Not.Null);
Assert.That(42, Is.Not.True);
Assert.That(42, Is.Not.False);
Assert.That(2.5, Is.Not.NaN);
Assert.That(2 + 2, Is.Not.EqualTo(3));
Assert.That(2 + 2, Is.Not.Not.EqualTo(4));
Assert.That(2 + 2, Is.Not.Not.Not.EqualTo(5));
// Inherited syntax
Expect(42, Not.Null);
Expect(42, Not.True);
Expect(42, Not.False);
Expect(2.5, Not.NaN);
Expect(2 + 2, Not.EqualTo(3));
Expect(2 + 2, Not.Not.EqualTo(4));
Expect(2 + 2, Not.Not.Not.EqualTo(5));
}
[Test]
public void NotOperator()
{
// The ! operator is only available in the new syntax
Assert.That(42, !Is.Null);
// Inherited syntax
Expect( 42, !Null );
}
[Test]
public void AndOperator()
{
// The & operator is only available in the new syntax
Assert.That(7, Is.GreaterThan(5) & Is.LessThan(10));
// Inherited syntax
Expect( 7, GreaterThan(5) & LessThan(10));
}
[Test]
public void OrOperator()
{
// The | operator is only available in the new syntax
Assert.That(3, Is.LessThan(5) | Is.GreaterThan(10));
Expect( 3, LessThan(5) | GreaterThan(10));
}
[Test]
public void ComplexTests()
{
Assert.That(7, Is.Not.Null & Is.Not.LessThan(5) & Is.Not.GreaterThan(10));
Expect(7, Not.Null & Not.LessThan(5) & Not.GreaterThan(10));
Assert.That(7, !Is.Null & !Is.LessThan(5) & !Is.GreaterThan(10));
Expect(7, !Null & !LessThan(5) & !GreaterThan(10));
// TODO: Remove #if when mono compiler can handle null
#if MONO
Constraint x = null;
Assert.That(7, !x & !Is.LessThan(5) & !Is.GreaterThan(10));
Expect(7, !x & !LessThan(5) & !GreaterThan(10));
#else
Assert.That(7, !(Constraint)null & !Is.LessThan(5) & !Is.GreaterThan(10));
Expect(7, !(Constraint)null & !LessThan(5) & !GreaterThan(10));
#endif
}
// This method contains assertions that should not compile
// You can check by uncommenting it.
//public void WillNotCompile()
//{
// Assert.That(42, Is.Not);
// Assert.That(42, Is.All);
// Assert.That(42, Is.Null.Not);
// Assert.That(42, Is.Not.Null.GreaterThan(10));
// Assert.That(42, Is.GreaterThan(10).LessThan(99));
// object[] c = new object[0];
// Assert.That(c, Is.Null.All);
// Assert.That(c, Is.Not.All);
// Assert.That(c, Is.All.Not);
//}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -