📄 assertsyntaxtests.cs
字号:
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************
using System;
using NUnit.Framework.Constraints;
using NUnit.Framework.SyntaxHelpers;
namespace NUnit.Framework.Tests
{
/// <summary>
/// This test fixture attempts to exercise all the syntactic
/// variations of Assert without getting into failures, errors
/// or corner cases. Thus, some of the tests may be duplicated
/// in other fixtures.
///
/// Each test performs the same operations using the classic
/// syntax (if available) and the new syntax in both the
/// helper-based and inherited forms.
///
/// This Fixture will eventually be duplicated in other
/// supported languages.
/// </summary>
[TestFixture]
public class AssertSyntaxTests : AssertionHelper
{
[Test]
public void IsNull()
{
// Classic syntax
Assert.IsNull(null);
// Helper syntax
Assert.That(null, Is.Null);
// Inherited syntax
Expect(null, Null);
}
[Test]
public void IsNotNull()
{
// Classic syntax
Assert.IsNotNull(42);
// Helper syntax
Assert.That(42, Is.Not.Null);
// Inherited syntax
Expect( 42, Not.Null );
}
[Test]
public void IsTrue()
{
// Classic syntax
Assert.IsTrue(2+2==4);
// Helper syntax
Assert.That(2+2==4, Is.True);
Assert.That(2+2==4);
// Inherited syntax
Expect(2+2==4, True);
Expect(2+2==4);
}
[Test]
public void IsFalse()
{
// Classic syntax
Assert.IsFalse(2+2==5);
// Helper syntax
Assert.That(2+2== 5, Is.False);
// Inherited syntax
Expect(2+2==5, False);
}
[Test]
public void IsNaN()
{
double d = double.NaN;
float f = float.NaN;
// Classic syntax
Assert.IsNaN(d);
Assert.IsNaN(f);
// Helper syntax
Assert.That(d, Is.NaN);
Assert.That(f, Is.NaN);
// Inherited syntax
Expect(d, NaN);
Expect(f, NaN);
}
[Test]
public void EmptyStringTests()
{
// Classic syntax
Assert.IsEmpty("");
Assert.IsNotEmpty("Hello!");
// Helper syntax
Assert.That("", Is.Empty);
Assert.That("Hello!", Is.Not.Empty);
// Inherited syntax
Expect("", Empty);
Expect("Hello!", Not.Empty);
}
[Test]
public void EmptyCollectionTests()
{
// Classic syntax
Assert.IsEmpty(new bool[0]);
Assert.IsNotEmpty(new int[] { 1, 2, 3 });
// Helper syntax
Assert.That(new bool[0], Is.Empty);
Assert.That(new int[] { 1, 2, 3 }, Is.Not.Empty);
// Inherited syntax
Expect(new bool[0], Empty);
Expect(new int[] { 1, 2, 3 }, Not.Empty);
}
[Test]
public void ExactTypeTests()
{
// Classic syntax workarounds
Assert.AreEqual(typeof(string), "Hello".GetType());
Assert.AreEqual("System.String", "Hello".GetType().FullName);
Assert.AreNotEqual(typeof(int), "Hello".GetType());
Assert.AreNotEqual("System.Int32", "Hello".GetType().FullName);
// Helper syntax
Assert.That("Hello", Is.TypeOf(typeof(string)));
Assert.That("Hello", Is.Not.TypeOf(typeof(int)));
// Inherited syntax
Expect( "Hello", TypeOf(typeof(string)));
Expect( "Hello", Not.TypeOf(typeof(int)));
}
[Test]
public void InstanceOfTypeTests()
{
// Classic syntax
Assert.IsInstanceOfType(typeof(string), "Hello");
Assert.IsNotInstanceOfType(typeof(string), 5);
// Helper syntax
Assert.That("Hello", Is.InstanceOfType(typeof(string)));
Assert.That(5, Is.Not.InstanceOfType(typeof(string)));
// Inherited syntax
Expect("Hello", InstanceOfType(typeof(string)));
Expect(5, Not.InstanceOfType(typeof(string)));
}
[Test]
public void AssignableFromTypeTests()
{
// Classic syntax
Assert.IsAssignableFrom(typeof(string), "Hello");
Assert.IsNotAssignableFrom(typeof(string), 5);
// Helper syntax
Assert.That( "Hello", Is.AssignableFrom(typeof(string)));
Assert.That( 5, Is.Not.AssignableFrom(typeof(string)));
// Inherited syntax
Expect( "Hello", AssignableFrom(typeof(string)));
Expect( 5, Not.AssignableFrom(typeof(string)));
}
[Test]
public void SubstringTests()
{
string phrase = "Hello World!";
string[] array = new string[] { "abc", "bad", "dba" };
// Classic Syntax
StringAssert.Contains("World", phrase);
// Helper syntax
Assert.That(phrase, Text.Contains("World"));
// Only available using new syntax
Assert.That(phrase, Text.DoesNotContain("goodbye"));
Assert.That(phrase, Text.Contains("WORLD").IgnoreCase);
Assert.That(phrase, Text.DoesNotContain("BYE").IgnoreCase);
Assert.That(array, Text.All.Contains( "b" ) );
// Inherited syntax
Expect(phrase, Contains("World"));
// Only available using new syntax
Expect(phrase, Not.Contains("goodbye"));
Expect(phrase, Contains("WORLD").IgnoreCase);
Expect(phrase, Not.Contains("BYE").IgnoreCase);
Expect(array, All.Contains("b"));
}
[Test]
public void StartsWithTests()
{
string phrase = "Hello World!";
string[] greetings = new string[] { "Hello!", "Hi!", "Hola!" };
// Classic syntax
StringAssert.StartsWith("Hello", phrase);
// Helper syntax
Assert.That(phrase, Text.StartsWith("Hello"));
// Only available using new syntax
Assert.That(phrase, Text.DoesNotStartWith("Hi!"));
Assert.That(phrase, Text.StartsWith("HeLLo").IgnoreCase);
Assert.That(phrase, Text.DoesNotStartWith("HI").IgnoreCase);
Assert.That(greetings, Text.All.StartsWith("h").IgnoreCase);
// Inherited syntax
Expect(phrase, StartsWith("Hello"));
// Only available using new syntax
Expect(phrase, Not.StartsWith("Hi!"));
Expect(phrase, StartsWith("HeLLo").IgnoreCase);
Expect(phrase, Not.StartsWith("HI").IgnoreCase);
Expect(greetings, All.StartsWith("h").IgnoreCase);
}
[Test]
public void EndsWithTests()
{
string phrase = "Hello World!";
string[] greetings = new string[] { "Hello!", "Hi!", "Hola!" };
// Classic Syntax
StringAssert.EndsWith("!", phrase);
// Helper syntax
Assert.That(phrase, Text.EndsWith("!"));
// Only available using new syntax
Assert.That(phrase, Text.DoesNotEndWith("?"));
Assert.That(phrase, Text.EndsWith("WORLD!").IgnoreCase);
Assert.That(greetings, Text.All.EndsWith("!"));
// Inherited syntax
Expect(phrase, EndsWith("!"));
// Only available using new syntax
Expect(phrase, Not.EndsWith("?"));
Expect(phrase, EndsWith("WORLD!").IgnoreCase);
Expect(greetings, All.EndsWith("!") );
}
[Test]
public void EqualIgnoringCaseTests()
{
string phrase = "Hello World!";
// Classic syntax
StringAssert.AreEqualIgnoringCase("hello world!",phrase);
// Helper syntax
Assert.That(phrase, Is.EqualTo("hello world!").IgnoreCase);
//Only available using new syntax
Assert.That(phrase, Is.Not.EqualTo("goodbye world!").IgnoreCase);
Assert.That(new string[] { "Hello", "World" },
Is.EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase);
Assert.That(new string[] {"HELLO", "Hello", "hello" },
Is.All.EqualTo( "hello" ).IgnoreCase);
// Inherited syntax
Expect(phrase, EqualTo("hello world!").IgnoreCase);
//Only available using new syntax
Expect(phrase, Not.EqualTo("goodbye world!").IgnoreCase);
Expect(new string[] { "Hello", "World" },
EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase);
Expect(new string[] {"HELLO", "Hello", "hello" },
All.EqualTo( "hello" ).IgnoreCase);
}
[Test]
public void RegularExpressionTests()
{
string phrase = "Now is the time for all good men to come to the aid of their country.";
string[] quotes = new string[] { "Never say never", "It's never too late", "Nevermore!" };
// Classic syntax
StringAssert.IsMatch( "all good men", phrase );
StringAssert.IsMatch( "Now.*come", phrase );
// Helper syntax
Assert.That( phrase, Text.Matches( "all good men" ) );
Assert.That( phrase, Text.Matches( "Now.*come" ) );
// Only available using new syntax
Assert.That(phrase, Text.DoesNotMatch("all.*men.*good"));
Assert.That(phrase, Text.Matches("ALL").IgnoreCase);
Assert.That(quotes, Text.All.Matches("never").IgnoreCase);
// Inherited syntax
Expect( phrase, Matches( "all good men" ) );
Expect( phrase, Matches( "Now.*come" ) );
// Only available using new syntax
Expect(phrase, Not.Matches("all.*men.*good"));
Expect(phrase, Matches("ALL").IgnoreCase);
Expect(quotes, All.Matches("never").IgnoreCase);
}
[Test]
public void EqualityTests()
{
int[] i3 = new int[] { 1, 2, 3 };
double[] d3 = new double[] { 1.0, 2.0, 3.0 };
int[] iunequal = new int[] { 1, 3, 2 };
// Classic Syntax
Assert.AreEqual(4, 2 + 2);
Assert.AreEqual(i3, d3);
Assert.AreNotEqual(5, 2 + 2);
Assert.AreNotEqual(i3, iunequal);
// Helper syntax
Assert.That(2 + 2, Is.EqualTo(4));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -