In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / MessageTest.cs
1 //
2 // MessageTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
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 using System;
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Tasks;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
35
36 namespace MonoTests.Microsoft.Build.Tasks {
37
38         internal class TestMessageLogger : ILogger {
39                 IList messages;
40                 
41                 public TestMessageLogger ()
42                 {
43                         messages = new ArrayList ();
44                 }
45         
46                 public LoggerVerbosity Verbosity { get { return LoggerVerbosity.Normal; } set { } }
47                 
48                 public string Parameters { get { return null; } set { } }
49                 
50                 public void Initialize (IEventSource eventSource)
51                 {
52                         eventSource.MessageRaised += new BuildMessageEventHandler (MessageHandler);
53                 }
54                 
55                 public void Shutdown ()
56                 {
57                 }
58                 
59                 private void MessageHandler (object sender, BuildMessageEventArgs args)
60                 {
61                         if (args.Message.StartsWith ("Using") == false)
62                                 messages.Add (args);
63                 }
64                 
65                 public int CheckHead (string text, MessageImportance importance)
66                 {
67                         BuildMessageEventArgs actual;
68                 
69                         if (messages.Count > 0) {
70                                 actual = (BuildMessageEventArgs) messages [0];
71                                 messages.RemoveAt (0);
72                         } else
73                                 return 1;
74                         
75                         if (text == actual.Message && importance == actual.Importance)
76                                 return 0;
77                         else
78                                 return 2;
79                 }
80         }
81
82         [TestFixture]
83         public class MessageTest {
84         
85                 Engine engine;
86                 Project project;
87                 TestMessageLogger testLogger;
88                 
89                 [Test]
90                 public void TestAssignment ()
91                 {
92                         string importance = "importance";
93                         string text = "text";
94                         
95                         Message message = new Message ();
96                         
97                         message.Importance = importance;
98                         message.Text = text;
99                         
100                         Assert.AreEqual (importance, message.Importance, "A1");
101                         Assert.AreEqual (text, message.Text, "A2");
102                 }
103                 
104                 [Test]
105                 public void TestExecution ()
106                 {
107                         string documentString = @"
108                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
109                                         <Target Name='1'>
110                                                 <Message Text='Text' Importance='Low'/>
111                                                 <Message Text='Text' Importance='Normal'/>
112                                                 <Message Text='Text' Importance='High'/>
113                                                 <Message Text='Text' Importance='low'/>
114                                                 <Message Text='Text' Importance='normal'/>
115                                                 <Message Text='Text' Importance='high'/>
116                                                 <Message Text='Text' />
117                                                 <Message Text='Text' Importance='weird_importance'/>
118                                         </Target>
119                                 </Project>
120                         ";
121                         
122                         engine = new Engine (Consts.BinPath);
123                         testLogger = new TestMessageLogger ();
124                         engine.RegisterLogger (testLogger);
125                         
126                         project = engine.CreateNewProject ();
127                         project.LoadXml (documentString);
128                         project.Build ("1");
129                         
130                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.Low), "A1");
131                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.Normal), "A2");
132                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.High), "A3");
133                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.Low), "A4");
134                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.Normal), "A5");
135                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.High), "A6");
136                         Assert.AreEqual (0, testLogger.CheckHead ("Text", MessageImportance.Normal), "A7");
137                         Assert.AreEqual (1, testLogger.CheckHead ("Text", MessageImportance.Normal), "A8");
138                         
139                 }
140         }
141 }       
142