Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / System.Diagnostics.Contracts.Internal / ContractHelper.cs
1 //
2 // System.Diagnostics.Contracts.Internal.ContractHelper.cs
3 //
4 // Authors:
5 //    Chris Bacon (chrisbacon76@gmail.com)
6 //
7 // Copyright 2010 Novell (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_0 || MOONLIGHT
30
31 using System;
32 using System.Text;
33 using System.Runtime.ConstrainedExecution;
34
35 namespace System.Diagnostics.Contracts.Internal
36 {
37         public static class ContractHelper
38         {
39                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
40                 [DebuggerNonUserCode]
41                 public static string RaiseContractFailedEvent (ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
42                 {
43
44                         StringBuilder msg = new StringBuilder (60);
45                         switch (failureKind) {
46                         case ContractFailureKind.Assert:
47                                 msg.Append ("Assertion failed");
48                                 break;
49                         case ContractFailureKind.Assume:
50                                 msg.Append ("Assumption failed");
51                                 break;
52                         case ContractFailureKind.Invariant:
53                                 msg.Append ("Invariant failed");
54                                 break;
55                         case ContractFailureKind.Postcondition:
56                                 msg.Append ("Postcondition failed");
57                                 break;
58                         case ContractFailureKind.PostconditionOnException:
59                                 msg.Append ("Postcondition failed after throwing an exception");
60                                 break;
61                         case ContractFailureKind.Precondition:
62                                 msg.Append ("Precondition failed");
63                                 break;
64                         default:
65                                 throw new NotSupportedException ("Not supported: " + failureKind);
66                         }
67                         if (conditionText != null) {
68                                 msg.Append (": ");
69                                 msg.Append (conditionText);
70                         } else {
71                                 msg.Append ('.');
72                         }
73                         if (userMessage != null) {
74                                 msg.Append ("  ");
75                                 msg.Append (userMessage);
76                         }
77                         string msgString = msg.ToString ();
78
79                         Exception handlerException = null;
80                         bool unwind = false, handled = false;
81
82                         var contractFailed = Contract.InternalContractFailedEvent;
83                         if (contractFailed != null) {
84                                 // Execute all event handlers
85                                 var handlers = contractFailed.GetInvocationList ();
86                                 var e = new ContractFailedEventArgs (failureKind, msgString, conditionText, innerException);
87                                 foreach (var handler in handlers) {
88                                         try {
89                                                 handler.DynamicInvoke (null, e);
90                                         } catch (Exception ex) {
91                                                 e.SetUnwind ();
92                                                 // If multiple handlers throw an exception then the specification states that it
93                                                 // is undetermined which one becomes the InnerException.
94                                                 handlerException = ex.InnerException;
95                                         }
96                                 }
97                                 unwind = e.Unwind;
98                                 handled = e.Handled;
99                         }
100
101                         if (unwind) {
102                                 Exception ex = innerException ?? handlerException;
103                                 throw new ContractException (msgString, failureKind, conditionText, userMessage, ex);
104                         }
105
106                         return handled ? null : msgString;
107                 }
108
109                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
110                 [DebuggerNonUserCode]
111                 public static void TriggerFailure (ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
112                 {
113                         StringBuilder msg = new StringBuilder (50);
114
115                         if (conditionText != null) {
116                                 msg.Append ("Expression: ");
117                                 msg.AppendLine (conditionText);
118                         }
119                         msg.Append ("Description: ");
120                         if (displayMessage != null) {
121                                 msg.Append (displayMessage);
122                         }
123
124                         if (Environment.UserInteractive) {
125                                 // FIXME: This should trigger an assertion.
126                                 // But code will never get here at the moment, as Environment.UserInteractive currently
127                                 // always returns false.
128                                 throw new ContractShouldAssertException (msg.ToString ());
129                         } else {
130                                 // Note that FailFast() currently throws a NotImplementedException()
131                                 Environment.FailFast(msg.ToString()/*, new ExecutionEngineException()*/);
132                         }
133
134                 }
135
136         }
137
138 }
139
140 #endif