[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / corlib / Test / System.Diagnostics.Contracts / ContractMustUseRewriterTest.cs
1
2 #define CONTRACTS_FULL
3 #define DEBUG
4
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Text;
9 using NUnit.Framework;
10 using System.Diagnostics.Contracts;
11 using MonoTests.System.Diagnostics.Contracts.Helpers;
12
13 namespace MonoTests.System.Diagnostics.Contracts {
14
15         [TestFixture]
16         public class ContractMustUseRewriterTest : TestContractBase {
17
18                 private void CheckMustUseRewriter (string expectedMsg, params Action[] fnTests)
19                 {
20                         foreach (var fnTest in fnTests) {
21                                 try {
22                                         fnTest ();
23                                         Assert.Fail ("CheckMustUseRewriter() exception not thrown");
24                                 } catch (Exception ex) {
25                                         Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
26                                 }
27                         }
28
29                         bool handlerVisited = false;
30                         Contract.ContractFailed += (sender, e) => {
31                                 handlerVisited = true;
32                         };
33
34                         foreach (var fnTest in fnTests) {
35                                 try {
36                                         fnTest ();
37                                         Assert.Fail ("CheckMustUseRewriter() exception not thrown");
38                                 } catch (Exception ex) {
39                                         Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
40                                 }
41                         }
42
43                         Assert.IsFalse (handlerVisited, "CheckMustUseRewriter() handled visited");
44                 }
45
46                 /// <summary>
47                 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
48                 /// </summary>
49                 [Test]
50                 [Ignore ("This causes NUnit crash on .NET 4.0")]
51                 public void TestRequires ()
52                 {
53                         CheckMustUseRewriter ("Description: Must use the rewriter when using Contract.Requires",
54                                 () => Contract.Requires (true),
55                                 () => Contract.Requires (false),
56                                 () => Contract.Requires (true, "Message"),
57                                 () => Contract.Requires (false, "Message")
58                         );
59                 }
60
61                 /// <summary>
62                 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
63                 /// </summary>
64                 [Test]
65                 [Ignore ("This causes NUnit crash on .NET 4.0")]
66                 public void TestRequiresTException ()
67                 {
68                         CheckMustUseRewriter ("Description: Must use the rewriter when using Contract.Requires<TException>",
69                                 () => Contract.Requires<Exception> (true),
70                                 () => Contract.Requires<Exception> (false),
71                                 () => Contract.Requires<Exception> (true, "Message"),
72                                 () => Contract.Requires<Exception> (false, "Message")
73                         );
74                 }
75
76                 /// <summary>
77                 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
78                 /// </summary>
79                 [Test]
80                 [Ignore ("This causes NUnit crash on .NET 4.0")]
81                 public void TestEnsures ()
82                 {
83                         CheckMustUseRewriter ("Description: Must use the rewriter when using Contract.Ensures",
84                                 () => Contract.Ensures (true),
85                                 () => Contract.Ensures (false),
86                                 () => Contract.Ensures (true, "Message"),
87                                 () => Contract.Ensures (false, "Message")
88                         );
89                 }
90
91                 /// <summary>
92                 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
93                 /// </summary>
94                 [Test]
95                 [Ignore ("This causes NUnit crash on .NET 4.0")]
96                 public void TestEnsuresOnThrow ()
97                 {
98                         CheckMustUseRewriter ("Description: Must use the rewriter when using Contract.EnsuresOnThrow",
99                                 () => Contract.EnsuresOnThrow<Exception> (true),
100                                 () => Contract.EnsuresOnThrow<Exception> (false),
101                                 () => Contract.EnsuresOnThrow<Exception> (true, "Message"),
102                                 () => Contract.EnsuresOnThrow<Exception> (false, "Message")
103                         );
104                 }
105
106                 /// <summary>
107                 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
108                 /// </summary>
109                 [Test]
110                 [Ignore ("This causes NUnit crash on .NET 4.0")]
111                 public void TestInvariant ()
112                 {
113                         CheckMustUseRewriter ("Description: Must use the rewriter when using Contract.Invariant",
114                                 () => Contract.Invariant (true),
115                                 () => Contract.Invariant (false),
116                                 () => Contract.Invariant (true, "Message"),
117                                 () => Contract.Invariant (false, "Message")
118                         );
119                 }
120
121         }
122
123 }
124