6 using System.Collections.Generic;
10 using System.Diagnostics.Contracts;
11 using MonoTests.System.Diagnostics.Contracts.Helpers;
13 namespace MonoTests.System.Diagnostics.Contracts {
16 public class ContractMustUseRewriterTest : TestContractBase {
18 private void CheckMustUseRewriter (string expectedMsg, params Action[] fnTests)
20 foreach (var fnTest in fnTests) {
23 Assert.Fail ("CheckMustUseRewriter() exception not thrown");
24 } catch (Exception ex) {
25 Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
29 bool handlerVisited = false;
30 Contract.ContractFailed += (sender, e) => {
31 handlerVisited = true;
34 foreach (var fnTest in fnTests) {
37 Assert.Fail ("CheckMustUseRewriter() exception not thrown");
38 } catch (Exception ex) {
39 Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
43 Assert.IsFalse (handlerVisited, "CheckMustUseRewriter() handled visited");
47 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
50 [Ignore ("This causes NUnit crash on .NET 4.0")]
51 public void TestRequires ()
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")
62 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
65 [Ignore ("This causes NUnit crash on .NET 4.0")]
66 public void TestRequiresTException ()
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")
77 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
80 [Ignore ("This causes NUnit crash on .NET 4.0")]
81 public void TestEnsures ()
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")
92 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
95 [Ignore ("This causes NUnit crash on .NET 4.0")]
96 public void TestEnsuresOnThrow ()
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")
107 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
110 [Ignore ("This causes NUnit crash on .NET 4.0")]
111 public void TestInvariant ()
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")