2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / WarningTest.cs
1 //
2 // WarningTest.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 TestWarningLogger : ILogger {
39                 IList warnings;
40                 
41                 public TestWarningLogger ()
42                 {
43                         warnings = 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.WarningRaised += new BuildWarningEventHandler (WarningHandler);
53                 }
54                 
55                 public void Shutdown ()
56                 {
57                 }
58                 
59                 private void WarningHandler (object sender, BuildWarningEventArgs args)
60                 {
61                         if (args.Message.StartsWith ("The MSBuild engine") == false)
62                                 warnings.Add (args);
63                 }
64                 
65                 public int CheckHead (string text, string helpKeyword, string code)
66                 {
67                         BuildWarningEventArgs actual;
68                 
69                         if (warnings.Count > 0) {
70                                 actual = (BuildWarningEventArgs) warnings [0];
71                                 warnings.RemoveAt (0);
72                         } else
73                                 return 1;
74                         
75                         if (text == actual.Message && helpKeyword == actual.HelpKeyword && code == actual.Code)
76                                 return 0;
77                         else {
78                                 Console.WriteLine (actual.Message);
79                                 return 2;
80                         }
81                 }
82         }
83
84         [TestFixture]
85         public class WarningTest {
86         
87                 Engine engine;
88                 Project project;
89                 TestWarningLogger testLogger;
90                 
91                 [Test]
92                 public void TestAssignment ()
93                 {
94                         string code = "code";
95                         string helpKeyword = "helpKeyword";
96                         string text = "text";
97                         
98                         Warning warning = new Warning ();
99                         
100                         warning.Code = code;
101                         warning.HelpKeyword = helpKeyword;
102                         warning.Text = text;
103
104                         Assert.AreEqual (code, warning.Code, "#1");
105                         Assert.AreEqual (helpKeyword, warning.HelpKeyword, "#2");
106                         Assert.AreEqual (text, warning.Text, "#3");
107                 }
108                 
109                 [Test]
110                 public void TestExecution ()
111                 {
112                         string documentString = @"
113                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
114                                         <Target Name='1'>
115                                                 <Warning Text='Text' HelpKeyword='HelpKeyword' Code='Code' />
116                                         </Target>
117                                 </Project>
118                         ";
119                         
120                         engine = new Engine (Consts.BinPath);
121                         testLogger = new TestWarningLogger ();
122                         engine.RegisterLogger (testLogger);
123                         
124                         project = engine.CreateNewProject ();
125                         project.LoadXml (documentString);
126                         project.Build ("1");
127                         
128                         Assert.AreEqual (0, testLogger.CheckHead ("Text", "HelpKeyword", "Code"), "A1");
129                 }
130         }
131 }       
132