#define CONTRACTS_FULL
#define DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Diagnostics.Contracts;
using System.Diagnostics;
using MonoTests.System.Diagnostics.Contracts.Helpers;
namespace MonoTests.System.Diagnostics.Contracts {
[TestFixture]
public class ContractAssertTest : TestContractBase {
///
/// Ensures that Assert(true) allows execution to continue.
///
[Test, RunAgainstReference]
public void TestAssertTrue ()
{
Contract.Assert (true);
}
///
/// Contract.Assert(false) will cause an assert to be triggered with the correct message.
///
[Test]
// [Ignore ("This causes NUnit crash on .NET 4.0")]
public void TestAssertNoEventHandler ()
{
try {
Contract.Assert (false);
Assert.Fail ("TestAssertNoEventHandler() exception not thrown #1");
} catch (Exception ex) {
Assert.AreEqual ("Assertion failed.", ex.Message);
}
try {
Contract.Assert (false, "Message");
Assert.Fail ("TestAssertNoEventHandler() exception not thrown #2");
} catch (Exception ex) {
Assert.AreEqual ("Assertion failed. Message", ex.Message);
}
}
///
/// Contract.Assert(true) will not call the ContractFailed event handler.
/// Contract.Assert(false) will call the ContractFailed event handler.
/// Because nothing is done in the event handler, an assert should be triggered.
///
[Test]
// [Ignore ("This causes NUnit crash on .NET 4.0")]
public void TestAssertEventHandlerNoAction ()
{
bool visitedEventHandler = false;
Contract.ContractFailed += (sender, e) => {
visitedEventHandler = true;
};
Contract.Assert (true);
Assert.IsFalse (visitedEventHandler, "TestAssertEventHandlerNoAction() handler visited");
try {
Contract.Assert (false);
Assert.Fail ("TestAssertEventHandlerNoAction() exception not thrown");
} catch (Exception ex) {
Assert.AreEqual ("Assertion failed.", ex.Message);
}
Assert.IsTrue (visitedEventHandler, "TestAssertEventHandlerNoAction() handler not visited");
}
///
/// Event handler calls SetHandled(), so no assert should be triggered.
///
[Test, RunAgainstReference]
public void TestAssertEventHandlerSetHandled ()
{
Contract.ContractFailed += (sender, e) => {
e.SetHandled ();
};
Contract.Assert (false);
}
///
/// Event handler calls SetUnwind(), so exception of type ContractException should be thrown.
///
[Test, RunAgainstReference]
public void TestAssertEventHandlerSetUnwind ()
{
Contract.ContractFailed += (sender, e) => {
e.SetUnwind ();
};
try {
Contract.Assert (false);
} catch (Exception ex) {
Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwind() wrong exception type");
Assert.IsNull (ex.InnerException, "TestAssertEventHandlerSetUnwind() inner exception not null");
}
}
///
/// Event handler calls SetHandled() and SetUnwind(), so exception of type ContractException should be thrown,
/// as SetUnwind overrides SetHandled.
///
[Test, RunAgainstReference]
public void TestAssertEventHandlerSetUnwindHandled ()
{
Contract.ContractFailed += (sender, e) => {
e.SetHandled ();
e.SetUnwind ();
};
try {
Contract.Assert (false);
} catch (Exception ex) {
Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwindHandled() wrong exception type");
Assert.IsNull (ex.InnerException, "TestAssertEventHandlerSetUnwindHandled() inner exception not null");
}
}
///
/// Event handler throws exception.
/// ContractException is thrown by Contract.Assert(), with InnerException set to the thrown exception.
///
[Test, RunAgainstReference]
public void TestAssertEventHandlerThrows ()
{
Contract.ContractFailed += (sender, e) => {
throw new ArgumentNullException ();
};
try {
Contract.Assert (false);
} catch (Exception ex) {
Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwindHandled() wrong exception type");
Assert.IsInstanceOfType (typeof (ArgumentNullException), ex.InnerException, "TestAssertEventHandlerSetUnwindHandled() wrong inner exception type");
}
}
///
/// Multiple event handlers are registered. Check that both are called, and that the SetHandled()
/// call in one of them is handled correctly.
///
[Test, RunAgainstReference]
public void TestAssertMultipleHandlers ()
{
bool visited1 = false, visited2 = false;
Contract.ContractFailed += (sender, e) => {
visited1 = true;
Assert.IsFalse (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #1");
e.SetHandled ();
};
Contract.ContractFailed += (sender, e) => {
visited2 = true;
Assert.IsTrue (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #2");
};
Contract.Assert (false);
Assert.IsTrue (visited1, "TestAssertMultipleHandlers() visited1 incorrect");
Assert.IsTrue (visited2, "TestAssertMultipleHandlers() visited2 incorrect");
}
}
}