1 //
2 // ErrorTest.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 TestErrorLogger : ILogger {
39 		IList errors;
40 
TestErrorLogger()41 		public TestErrorLogger ()
42 		{
43 			errors = new ArrayList ();
44 		}
45 
46 		public LoggerVerbosity Verbosity { get { return LoggerVerbosity.Normal; } set { } }
47 
48 		public string Parameters { get { return null; } set { } }
49 
Initialize(IEventSource eventSource)50 		public void Initialize (IEventSource eventSource)
51 		{
52 			eventSource.ErrorRaised += new BuildErrorEventHandler (ErrorHandler);
53 		}
54 
Shutdown()55 		public void Shutdown ()
56 		{
57 		}
58 
ErrorHandler(object sender, BuildErrorEventArgs args)59 		private void ErrorHandler (object sender, BuildErrorEventArgs args)
60 		{
61 			errors.Add (args);
62 		}
63 
CheckHead(string text, string helpKeyword, string code)64 		public int CheckHead (string text, string helpKeyword, string code)
65 		{
66 			BuildErrorEventArgs actual;
67 
68 			if (errors.Count > 0) {
69 				actual = (BuildErrorEventArgs) errors [0];
70 				errors.RemoveAt (0);
71 			} else
72 				return 1;
73 
74 			if (text == actual.Message && helpKeyword == actual.HelpKeyword && code == actual.Code)
75 				return 0;
76 			else {
77 				return 2;
78 			}
79 		}
80 	}
81 
82 	[TestFixture]
83 	public class ErrorTest {
84 
85 		Engine engine;
86 		Project project;
87 		TestErrorLogger testLogger;
88 
89 		[Test]
TestAssignment()90 		public void TestAssignment ()
91 		{
92 			string code = "code";
93 			string helpKeyword = "helpKeyword";
94 			string text = "text";
95 
96 			Error error = new Error ();
97 
98 			error.Code = code;
99 			error.HelpKeyword = helpKeyword;
100 			error.Text = text;
101 
102 			Assert.AreEqual (code, error.Code, "#1");
103 			Assert.AreEqual (helpKeyword, error.HelpKeyword, "#2");
104 			Assert.AreEqual (text, error.Text, "#3");
105 		}
106 
107 		[Test]
TestExecution1()108 		public void TestExecution1 ()
109 		{
110 			string documentString = @"
111                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
112 					<Target Name='1'>
113 						<Error Text='Text' HelpKeyword='HelpKeyword' Code='Code' />
114 					</Target>
115 				</Project>
116 			";
117 
118 			engine = new Engine (Consts.BinPath);
119 			project = engine.CreateNewProject ();
120 			project.LoadXml (documentString);
121 
122 			bool result = project.Build ("1");
123 
124 			Assert.AreEqual (false, result, "#1");
125 		}
126 
127 		[Test]
TestExecution2()128 		public void TestExecution2 ()
129 		{
130 			string documentString = @"
131                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
132 					<Target Name='1'>
133 						<Error Text='Text' HelpKeyword='HelpKeyword' Code='Code' />
134 					</Target>
135 				</Project>
136 			";
137 
138 			engine = new Engine (Consts.BinPath);
139 			testLogger = new TestErrorLogger ();
140 			engine.RegisterLogger (testLogger);
141 
142 			project = engine.CreateNewProject ();
143 			project.LoadXml (documentString);
144 			project.Build ("1");
145 
146 			Assert.AreEqual (0, testLogger.CheckHead ("Text", "HelpKeyword", "Code"), "A1");
147 		}
148 	}
149 }
150 
151