7 using System.Collections.Generic;
10 using NUnit.Framework;
11 using System.Diagnostics.Contracts;
12 using MonoTests.System.Diagnostics.Contracts.Helpers;
14 namespace MonoTests.System.Diagnostics.Contracts {
17 public class ContractMustUseRewriterTest : TestContractBase {
19 private void CheckMustUseRewriter (string expectedMsg, params Action[] fnTests)
21 foreach (var fnTest in fnTests) {
24 Assert.Fail ("CheckMustUseRewriter() exception not thrown");
25 } catch (Exception ex) {
26 Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
30 bool handlerVisited = false;
31 Contract.ContractFailed += (sender, e) => {
32 handlerVisited = true;
35 foreach (var fnTest in fnTests) {
38 Assert.Fail ("CheckMustUseRewriter() exception not thrown");
39 } catch (Exception ex) {
40 Assert.IsInstanceOfType (typeof (NotImplementedException), ex, "CheckMustUseRewriter() wrong exception thrown");
44 Assert.IsFalse (handlerVisited, "CheckMustUseRewriter() handled visited");
48 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
51 [Ignore ("This causes NUnit crash on .NET 4.0")]
52 public void TestRequires ()
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")
63 /// Contract.Requires() ALWAYS triggers an assert, regardless of any other factors.
66 [Ignore ("This causes NUnit crash on .NET 4.0")]
67 public void TestRequiresTException ()
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")
78 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
81 [Ignore ("This causes NUnit crash on .NET 4.0")]
82 public void TestEnsures ()
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")
93 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
96 [Ignore ("This causes NUnit crash on .NET 4.0")]
97 public void TestEnsuresOnThrow ()
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")
108 /// Contract.Ensures() ALWAYS triggers an assert, regardless of any other factors.
111 [Ignore ("This causes NUnit crash on .NET 4.0")]
112 public void TestInvariant ()
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")