1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#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 {
/// <summary>
/// Ensures that Assert(true) allows execution to continue.
/// </summary>
[Test, RunAgainstReference]
public void TestAssertTrue ()
{
Contract.Assert (true);
}
/// <summary>
/// Contract.Assert(false) will cause an assert to be triggered with the correct message.
/// </summary>
[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);
}
}
/// <summary>
/// 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.
/// </summary>
[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");
}
/// <summary>
/// Event handler calls SetHandled(), so no assert should be triggered.
/// </summary>
[Test, RunAgainstReference]
public void TestAssertEventHandlerSetHandled ()
{
Contract.ContractFailed += (sender, e) => {
e.SetHandled ();
};
Contract.Assert (false);
}
/// <summary>
/// Event handler calls SetUnwind(), so exception of type ContractException should be thrown.
/// </summary>
[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");
}
}
/// <summary>
/// Event handler calls SetHandled() and SetUnwind(), so exception of type ContractException should be thrown,
/// as SetUnwind overrides SetHandled.
/// </summary>
[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");
}
}
/// <summary>
/// Event handler throws exception.
/// ContractException is thrown by Contract.Assert(), with InnerException set to the thrown exception.
/// </summary>
[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");
}
}
/// <summary>
/// Multiple event handlers are registered. Check that both are called, and that the SetHandled()
/// call in one of them is handled correctly.
/// </summary>
[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");
}
}
}
|