f8f3236a7627044f8db852694639d4b14787aa1c
[mono.git] / mcs / class / corlib / Test / System.Diagnostics.Contracts / ContractAssertTest.cs
1 #if NET_4_0
2
3 #define CONTRACTS_FULL
4 #define DEBUG
5
6 using System;
7 using System.Collections.Generic;
8 using System.Linq;
9 using System.Text;
10 using NUnit.Framework;
11 using System.Diagnostics.Contracts;
12 using System.Diagnostics;
13 using MonoTests.System.Diagnostics.Contracts.Helpers;
14
15 namespace MonoTests.System.Diagnostics.Contracts {
16
17         [TestFixture]
18         public class ContractAssertTest : TestContractBase {
19
20                 /// <summary>
21                 /// Ensures that Assert(true) allows execution to continue.
22                 /// </summary>
23                 [Test, RunAgainstReference]
24                 public void TestAssertTrue ()
25                 {
26                         Contract.Assert (true);
27                 }
28
29                 /// <summary>
30                 /// Contract.Assert(false) will cause an assert to be triggered with the correct message.
31                 /// </summary>
32                 [Test]
33 //              [Ignore ("This causes NUnit crash on .NET 4.0")]
34                 public void TestAssertNoEventHandler ()
35                 {
36                         try {
37                                 Contract.Assert (false);
38                                 Assert.Fail ("TestAssertNoEventHandler() exception not thrown #1");
39                         } catch (Exception ex) {
40                                 Assert.AreEqual ("Assertion failed.", ex.Message);
41                         }
42
43                         try {
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);
48                         }
49                 }
50
51                 /// <summary>
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.
55                 /// </summary>
56                 [Test]
57 //              [Ignore ("This causes NUnit crash on .NET 4.0")]
58                 public void TestAssertEventHandlerNoAction ()
59                 {
60                         bool visitedEventHandler = false;
61                         Contract.ContractFailed += (sender, e) => {
62                                 visitedEventHandler = true;
63                         };
64
65                         Contract.Assert (true);
66
67                         Assert.IsFalse (visitedEventHandler, "TestAssertEventHandlerNoAction() handler visited");
68
69                         try {
70                                 Contract.Assert (false);
71                                 Assert.Fail ("TestAssertEventHandlerNoAction() exception not thrown");
72                         } catch (Exception ex) {
73                                 Assert.AreEqual ("Assertion failed.", ex.Message);
74                         }
75
76                         Assert.IsTrue (visitedEventHandler, "TestAssertEventHandlerNoAction() handler not visited");
77                 }
78
79                 /// <summary>
80                 /// Event handler calls SetHandled(), so no assert should be triggered.
81                 /// </summary>
82                 [Test, RunAgainstReference]
83                 public void TestAssertEventHandlerSetHandled ()
84                 {
85                         Contract.ContractFailed += (sender, e) => {
86                                 e.SetHandled ();
87                         };
88
89                         Contract.Assert (false);
90                 }
91
92                 /// <summary>
93                 /// Event handler calls SetUnwind(), so exception of type ContractException should be thrown.
94                 /// </summary>
95                 [Test, RunAgainstReference]
96                 public void TestAssertEventHandlerSetUnwind ()
97                 {
98                         Contract.ContractFailed += (sender, e) => {
99                                 e.SetUnwind ();
100                         };
101
102                         try {
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");
107                         }
108                 }
109
110                 /// <summary>
111                 /// Event handler calls SetHandled() and SetUnwind(), so exception of type ContractException should be thrown,
112                 /// as SetUnwind overrides SetHandled.
113                 /// </summary>
114                 [Test, RunAgainstReference]
115                 public void TestAssertEventHandlerSetUnwindHandled ()
116                 {
117                         Contract.ContractFailed += (sender, e) => {
118                                 e.SetHandled ();
119                                 e.SetUnwind ();
120                         };
121
122                         try {
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");
127                         }
128                 }
129
130                 /// <summary>
131                 /// Event handler throws exception.
132                 /// ContractException is thrown by Contract.Assert(), with InnerException set to the thrown exception.
133                 /// </summary>
134                 [Test, RunAgainstReference]
135                 public void TestAssertEventHandlerThrows ()
136                 {
137                         Contract.ContractFailed += (sender, e) => {
138                                 throw new ArgumentNullException ();
139                         };
140
141                         try {
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");
146                         }
147                 }
148
149                 /// <summary>
150                 /// Multiple event handlers are registered. Check that both are called, and that the SetHandled()
151                 /// call in one of them is handled correctly.
152                 /// </summary>
153                 [Test, RunAgainstReference]
154                 public void TestAssertMultipleHandlers ()
155                 {
156                         bool visited1 = false, visited2 = false;
157
158                         Contract.ContractFailed += (sender, e) => {
159                                 visited1 = true;
160                                 Assert.IsFalse (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #1");
161                                 e.SetHandled ();
162                         };
163                         Contract.ContractFailed += (sender, e) => {
164                                 visited2 = true;
165                                 Assert.IsTrue (e.Handled, "TestAssertMultipleHandlers() Handled incorrect #2");
166                         };
167
168                         Contract.Assert (false);
169
170                         Assert.IsTrue (visited1, "TestAssertMultipleHandlers() visited1 incorrect");
171                         Assert.IsTrue (visited2, "TestAssertMultipleHandlers() visited2 incorrect");
172                 }
173
174         }
175 }
176
177 #endif