[xbuild] TaskLoggingHelper: Implement support for correctly using task resources
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / TestMessageLogger.cs
1 using Microsoft.Build.Framework;
2 using System.Collections;
3 using System;
4 using System.Collections.Generic;
5 using NUnit.Framework;
6 using System.Text;
7
8 namespace MonoTests.Microsoft.Build.Tasks
9 {
10         internal class TestMessageLogger : ILogger
11         {
12                 List<BuildMessageEventArgs> messages;
13                 List<BuildEventArgs> all_messages;
14                 int target_started, target_finished;
15                 int task_started, task_finished;
16                 int project_started, project_finished;
17                 int build_started, build_finished;
18
19                 public int BuildStarted
20                 {
21                         get { return build_started; }
22                         set { build_started = value; }
23                 }
24
25                 public int BuildFinished
26                 {
27                         get { return build_finished; }
28                         set { build_finished = value; }
29                 }
30
31                 public TestMessageLogger ()
32                 {
33                         messages = new List<BuildMessageEventArgs> ();
34                         all_messages = new List<BuildEventArgs> ();
35                 }
36
37                 public int ProjectStarted
38                 {
39                         get { return project_started; }
40                         set { project_started = value; }
41                 }
42
43                 public int ProjectFinished
44                 {
45                         get { return project_finished; }
46                         set { project_finished = value; }
47                 }
48
49                 public int TargetFinished
50                 {
51                         get { return target_finished; }
52                         set { target_finished = value; }
53                 }
54
55                 public int TargetStarted
56                 {
57                         get { return target_started; }
58                         set { target_started = value; }
59                 }
60
61                 public int TaskStarted
62                 {
63                         get { return task_started; }
64                         set { task_started = value; }
65                 }
66
67                 public int TaskFinished
68                 {
69                         get { return task_finished; }
70                         set { task_finished = value; }
71                 }
72
73                 public int WarningsCount { get; set; }
74                 public int ErrorsCount { get; set; }
75
76                 public int Count
77                 {
78                         get { return messages.Count; }
79                 }
80
81                 public LoggerVerbosity Verbosity { get { return LoggerVerbosity.Normal; } set { } }
82
83                 public string Parameters { get { return null; } set { } }
84
85                 public void Initialize (IEventSource eventSource)
86                 {
87                         eventSource.MessageRaised += new BuildMessageEventHandler (MessageHandler);
88
89                         eventSource.ErrorRaised += new BuildErrorEventHandler (AllMessagesHandler);
90                         eventSource.ErrorRaised += (e,o) => ErrorsCount ++;
91
92                         eventSource.WarningRaised += new BuildWarningEventHandler(AllMessagesHandler);
93                         eventSource.WarningRaised += (e,o) => WarningsCount ++;
94
95                         eventSource.TargetStarted += delegate { target_started++; };
96                         eventSource.TargetFinished += delegate { target_finished++; };
97                         eventSource.TaskStarted += delegate { task_started++; };
98                         eventSource.TaskFinished += delegate { task_finished++; };
99                         eventSource.ProjectStarted += delegate(object sender, ProjectStartedEventArgs args) { project_started++; };
100                         eventSource.ProjectFinished += delegate (object sender, ProjectFinishedEventArgs args) { project_finished++; };
101                         eventSource.BuildStarted += delegate { build_started ++; };
102                         eventSource.BuildFinished += delegate { build_finished++; };
103                 }
104
105                 public void Shutdown ()
106                 {
107                 }
108
109                 private void MessageHandler (object sender, BuildMessageEventArgs args)
110                 {
111                         if (args.Message.StartsWith ("Using") == false)
112                                 messages.Add (args);
113                         all_messages.Add (args);
114                 }
115
116                 private void AllMessagesHandler (object sender, BuildEventArgs args)
117                 {
118                         all_messages.Add (args);
119                 }
120
121                 public int NormalMessageCount {
122                         get
123                         {
124                                 int count = 0, i = 0;
125                                 while (i ++ < messages.Count)
126                                         if (messages [i - 1].Importance == MessageImportance.Normal)
127                                                 count++;
128                                 return count;
129                         }
130                 }
131
132                 public int WarningMessageCount {
133                         get {
134                                 int count = 0, i = 0;
135                                 while (i++ < messages.Count) {
136                                         var importance = messages [i - 1].Importance;
137                                         if (importance == MessageImportance.High)
138                                                 count++;
139                                 }
140                                 return count;
141                         }
142                 }
143
144                 public int CheckHead (string text, MessageImportance importance)
145                 {
146                         string actual_msg;
147                         return CheckHead (text, importance, out actual_msg);
148                 }
149
150                 public int CheckHead (string text, MessageImportance importance, out string actual_msg)
151                 {
152                         BuildMessageEventArgs actual = null;
153                         actual_msg = String.Empty;
154
155                         if (messages.Count > 0) {
156                                 //find first @importance level message
157                                 int i = -1;
158                                 while (++i < messages.Count && messages [i].Importance != importance)
159                                         ;
160
161                                 if (i < messages.Count) {
162                                         actual = messages [i];
163                                         messages.RemoveAt (i);
164                                 }
165                         } else
166                                 return 1;
167
168                         if (actual != null)
169                                 actual_msg = actual.Message;
170                         if (actual != null && text == actual_msg && importance == actual.Importance)
171                                 return 0;
172                         else
173                                 return 2;
174                 }
175
176                 //return: 0 - found
177                 //              1 - msg not found,
178                 public int CheckAny (string text, MessageImportance importance)
179                 {
180                         if (messages.Count <= 0)
181                                 return 1;
182
183                         int foundAt = -1;
184                         for (int i = 0; i < messages.Count; i ++) {
185                                 BuildMessageEventArgs arg = messages [i];
186                                 if (text == arg.Message && importance == arg.Importance) {
187                                         foundAt = i;
188                                         break;
189                                 }
190                         }
191
192                         if (foundAt < 0)
193                                 return 1;
194
195                         //found
196                         messages.RemoveAt (foundAt);
197                         return 0;
198                 }
199
200                 public int CheckFullLog (string text)
201                 {
202                         for (int i = 0; i < all_messages.Count; i ++) {
203                                 BuildEventArgs arg = all_messages [i];
204                                 if (text == arg.Message) {
205                                         all_messages.RemoveAt (i);
206                                         return 0;
207                                 }
208                         }
209
210                         return 1;
211                 }
212
213                 public void DumpMessages ()
214                 {
215                         foreach (BuildEventArgs arg in all_messages)
216                                 Console.WriteLine ("Msg: {0}", arg.Message);
217                 }
218
219                 public void DumpMessages (StringBuilder sb)
220                 {
221                         foreach (BuildEventArgs arg in all_messages)
222                                 sb.AppendLine (string.Format ("Msg: {0}", arg.Message));
223                 }
224
225                 public void CheckLoggedMessageHead (string expected, string id)
226                 {
227                         string actual;
228                         int result = CheckHead (expected, MessageImportance.Normal, out actual);
229                         if (result == 1)
230                                 Assert.Fail ("{0}: Expected message '{1}' was not emitted.", id, expected);
231                         if (result == 2)
232                                 Assert.AreEqual (expected, actual, id);
233                 }
234
235                 public void CheckLoggedAny (string expected, MessageImportance importance, string id)
236                 {
237                         int res = CheckAny (expected, importance);
238                         if (res != 0)
239                                 Assert.Fail ("{0}: Expected message '{1}' was not emitted.", id, expected);
240                 }
241
242
243         }
244
245 }