7 using System.Collections.Generic;
10 using NUnit.Framework;
11 using System.Diagnostics.Contracts;
12 using System.Diagnostics;
13 using MonoTests.System.Diagnostics.Contracts.Helpers;
15 namespace MonoTests.System.Diagnostics.Contracts {
18 public class ContractAssertTest : TestContractBase {
21 /// Ensures that Assert(true) allows execution to continue.
23 [Test, RunAgainstReference]
24 public void TestAssertTrue ()
26 Contract.Assert (true);
30 /// Contract.Assert(false) will cause an assert to be triggered with the correct message.
33 // [Ignore ("This causes NUnit crash on .NET 4.0")]
34 public void TestAssertNoEventHandler ()
37 Contract.Assert (false);
38 Assert.Fail ("TestAssertNoEventHandler() exception not thrown #1");
39 } catch (Exception ex) {
40 Assert.AreEqual ("Assertion failed.", ex.Message);
44 Contract.Assert (false, "Message");
45 Assert.Fail ("TestAssertNoEventHandler() exception not thrown #2");
46 } catch (Exception ex) {
47 Assert.AreEqual ("Assertion failed. Message", ex.Message);
52 /// Contract.Assert(true) will not call the ContractFailed event handler.
53 /// Contract.Assert(false) will call the ContractFailed event handler.
54 /// Because nothing is done in the event handler, an assert should be triggered.
57 // [Ignore ("This causes NUnit crash on .NET 4.0")]
58 public void TestAssertEventHandlerNoAction ()
60 bool visitedEventHandler = false;
61 Contract.ContractFailed += (sender, e) => {
62 visitedEventHandler = true;
65 Contract.Assert (true);
67 Assert.IsFalse (visitedEventHandler, "TestAssertEventHandlerNoAction() handler visited");
70 Contract.Assert (false);
71 Assert.Fail ("TestAssertEventHandlerNoAction() exception not thrown");
72 } catch (Exception ex) {
73 Assert.AreEqual ("Assertion failed.", ex.Message);
76 Assert.IsTrue (visitedEventHandler, "TestAssertEventHandlerNoAction() handler not visited");
80 /// Event handler calls SetHandled(), so no assert should be triggered.
82 [Test, RunAgainstReference]
83 public void TestAssertEventHandlerSetHandled ()
85 Contract.ContractFailed += (sender, e) => {
89 Contract.Assert (false);
93 /// Event handler calls SetUnwind(), so exception of type ContractException should be thrown.
95 [Test, RunAgainstReference]
96 public void TestAssertEventHandlerSetUnwind ()
98 Contract.ContractFailed += (sender, e) => {
103 Contract.Assert (false);
104 } catch (Exception ex) {
105 Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwind() wrong exception type");
106 Assert.IsNull (ex.InnerException, "TestAssertEventHandlerSetUnwind() inner exception not null");
111 /// Event handler calls SetHandled() and SetUnwind(), so exception of type ContractException should be thrown,
112 /// as SetUnwind overrides SetHandled.
114 [Test, RunAgainstReference]
115 public void TestAssertEventHandlerSetUnwindHandled ()
117 Contract.ContractFailed += (sender, e) => {
123 Contract.Assert (false);
124 } catch (Exception ex) {
125 Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwindHandled() wrong exception type");
126 Assert.IsNull (ex.InnerException, "TestAssertEventHandlerSetUnwindHandled() inner exception not null");
131 /// Event handler throws exception.
132 /// ContractException is thrown by Contract.Assert(), with InnerException set to the thrown exception.
134 [Test, RunAgainstReference]
135 public void TestAssertEventHandlerThrows ()
137 Contract.ContractFailed += (sender, e) => {
138 throw new ArgumentNullException ();
142 Contract.Assert (false);
143 } catch (Exception ex) {
144 Assert.IsInstanceOfType (base.ContractExceptionType, ex, "TestAssertEventHandlerSetUnwindHandled() wrong exception type");
145 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex.InnerException, "TestAssertEventHandlerSetUnwindHandled() wrong inner exception type");
150 /// Multiple event handlers are registered. Check that both are called, and that the SetHandled()
151 /// call in one of them is handled correctly.
153 [Test, RunAgainstReference]
154 public void TestAssertMultipleHandlers ()
156 bool visited1 = false, visited2 = false;
158 Contract.ContractFailed += (sender, e) => {
160 Assert.IsFalse (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #1");
163 Contract.ContractFailed += (sender, e) => {
165 Assert.IsTrue (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #2");
168 Contract.Assert (false);
170 Assert.IsTrue (visited1, "TestAssertMultipleHandlers() visited1 incorrect");
171 Assert.IsTrue (visited2, "TestAssertMultipleHandlers() visited2 incorrect");