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