📄 cpp-cli-syntax.cpp
字号:
// ****************************************************************
// This is free software licensed under the NUnit license. You
// may obtain a copy of the license as well as information regarding
// copyright ownership at http://nunit.org/?p=license&r=2.4.
// ****************************************************************
using namespace NUnit::Framework;
using NUnit::Framework::SyntaxHelpers::Text;
using NUnit::Framework::SyntaxHelpers::List;
using NUnit::Framework::SyntaxHelpers::Has;
using System::String;
namespace NUnitSamples
{
[TestFixture]
public ref class AssertSyntaxTests : AssertionHelper
{
public:
[Test]
void IsNull()
{
// Classic syntax
Assert::IsNull(nullptr);
// Helper syntax
Assert::That(nullptr, Is::Null);
// Inherited syntax
Expect(nullptr, Null);
}
[Test]
void IsNotNull()
{
// Classic syntax
Assert::IsNotNull(42);
// Helper syntax
Assert::That(42, Is::Not->Null);
// Inherited syntax
Expect( 42, Not->Null );
}
[Test]
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]
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]
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]
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]
void EmptyCollectionTests()
{
// Classic syntax
Assert::IsEmpty(gcnew array<bool>(0));
Assert::IsNotEmpty(gcnew array<int>(3));
// Helper syntax
Assert::That(gcnew array<bool>(0), Is::Empty);
Assert::That(gcnew array<int>(3), Is::Not->Empty);
// Inherited syntax
Expect(gcnew array<bool>(0), Empty);
Expect(gcnew array<int>(3), Not->Empty);
}
[Test]
void ExactTypeTests()
{
// Classic syntax workarounds)
String^ greeting = "Hello";
Assert::AreEqual(String::typeid, greeting->GetType());
Assert::AreEqual("System.String", greeting->GetType()->FullName);
Assert::AreNotEqual(int::typeid, greeting->GetType());
Assert::AreNotEqual("System.Int32", greeting->GetType()->FullName);
// Helper syntax
Assert::That(greeting, Is::TypeOf(String::typeid));
Assert::That(greeting, Is::Not->TypeOf(int::typeid));
// Inherited syntax
Expect( "Hello", TypeOf(String::typeid));
Expect( "Hello", Not->TypeOf(int::typeid));
}
[Test]
void InstanceOfTypeTests()
{
// Classic syntax
Assert::IsInstanceOfType(String::typeid, "Hello");
Assert::IsNotInstanceOfType(String::typeid, 5);
// Helper syntax
Assert::That("Hello", Is::InstanceOfType(String::typeid));
Assert::That(5, Is::Not->InstanceOfType(String::typeid));
// Inherited syntax
Expect("Hello", InstanceOfType(String::typeid));
Expect(5, Not->InstanceOfType(String::typeid));
}
[Test]
void AssignableFromTypeTests()
{
// Classic syntax
Assert::IsAssignableFrom(String::typeid, "Hello");
Assert::IsNotAssignableFrom(String::typeid, 5);
// Helper syntax
Assert::That( "Hello", Is::AssignableFrom(String::typeid));
Assert::That( 5, Is::Not->AssignableFrom(String::typeid));
// Inherited syntax
Expect( "Hello", AssignableFrom(String::typeid));
Expect( 5, Not->AssignableFrom(String::typeid));
}
[Test]
void SubstringTests()
{
String^ phrase = "Hello World!";
array<String^>^ strings = {"abc", "bad", "dba" };
// Classic Syntax
StringAssert::Contains("World", phrase);
// Helper syntax
Assert::That(phrase, 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(strings, 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(strings, All->Contains("b"));
}
[Test]
void StartsWithTests()
{
String^ phrase = "Hello World!";
array<String^>^ greetings = { "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]
void EndsWithTests()
{
String^ phrase = "Hello World!";
array<String^>^ greetings = { "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]
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(gcnew array<String^> { "Hello", "World" },
Is::EqualTo(gcnew array<Object^> { "HELLO", "WORLD" })->IgnoreCase);
Assert::That(gcnew array<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(gcnew array<String^> { "Hello", "World" },
EqualTo(gcnew array<Object^> { "HELLO", "WORLD" })->IgnoreCase);
Expect(gcnew array<String^> {"HELLO", "Hello", "hello" },
All->EqualTo( "hello" )->IgnoreCase);
}
[Test]
void RegularExpressionTests()
{
String^ phrase = "Now is the time for all good men to come to the aid of their country.";
array<String^>^ quotes = { "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]
void EqualityTests()
{
array<int>^ i3 = { 1, 2, 3 };
array<double>^ d3 = { 1.0, 2.0, 3.0 };
array<int>^ iunequal = { 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -