5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Specialized;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
36 namespace MonoTests.Microsoft.Build.Utilities {
39 public class TaskItemTest {
41 ITaskItem item,item1,item2;
42 ICollection metadataNames;
47 string[] temp = new string[] {"FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
48 "RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
49 ArrayList al = new ArrayList ();
50 foreach (string s in temp)
55 private bool CompareStringCollections (ICollection compared, ICollection reference)
57 Hashtable comparedHash;
58 comparedHash = CollectionsUtil.CreateCaseInsensitiveHashtable ();
60 foreach (string s in compared)
61 comparedHash.Add (s, null);
63 foreach (string s in reference) {
64 if (comparedHash.ContainsKey (s) == false) {
72 public void TestCloneCustomMetadata ()
74 item = new TaskItem ();
75 item.SetMetadata ("AAA", "111");
76 item.SetMetadata ("aaa", "222");
77 item.SetMetadata ("BBB", "111");
79 string [] metakeys = new string [] { "aaa", "BBB" };
80 IDictionary meta = item.CloneCustomMetadata ();
82 Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A1");
84 Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A2");
85 Assert.AreEqual ("222", meta ["aaa"], "A3");
86 Assert.AreEqual ("222", meta ["AAA"], "A4");
87 Assert.AreEqual ("222", meta ["aAa"], "A5");
88 Assert.AreEqual ("111", meta ["BbB"], "A5");
92 [Ignore ("NRE on .NET 2.0")]
93 public void TestCtor1 ()
95 new TaskItem ((ITaskItem) null);
99 [ExpectedException (typeof (ArgumentNullException))]
100 public void TestCtor2 ()
102 new TaskItem ((string) null);
106 [ExpectedException (typeof (ArgumentNullException))]
107 public void TestCtor3 ()
109 new TaskItem ((string) null, new Hashtable ());
113 [ExpectedException (typeof (ArgumentNullException))]
114 public void TestCtor4 ()
116 new TaskItem ("itemspec", null);
120 public void TestCtor_EscapedSpecialChar ()
122 // If we instantiate with the *escaped* metadata, it's unescaped automatically
123 var metadata = "foo@2x.png";
124 var escapedMetadata = global::Microsoft.Build.BuildEngine.Utilities.Escape ("foo@2x.png");
125 var item = new TaskItem (escapedMetadata);
126 item.SetMetadata ("mine", escapedMetadata);
128 Assert.AreEqual (metadata, item.ItemSpec, "#1");
129 Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
130 Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
131 Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
132 Assert.AreEqual (metadata, item.GetMetadata ("mine"), "#5");
136 public void TestCtor_EscapedSpecialChar_BrokenEscaping ()
138 // This is badly escaped, but MSBuild does not care.
139 var metadata = "foo%4@2x.png";
140 var item = new TaskItem (metadata);
142 Assert.AreEqual (metadata, item.ItemSpec, "#1");
143 Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
144 Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
145 Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
149 public void TestCtor_UnescapedSpecialChar ()
151 // If we instantiate with unescaped metadata, we get the same value back
152 var metadata = "foo@2x.png";
153 var item = new TaskItem (metadata);
154 item.SetMetadata ("mine", metadata);
156 Assert.AreEqual (metadata, item.ItemSpec, "#1");
157 Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
158 Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
159 Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
160 Assert.AreEqual (metadata, item.GetMetadata ("mine"), "#5");
164 public void TestCopyConstructor ()
166 item1 = new TaskItem ("itemSpec");
167 item1.SetMetadata ("meta1", "val1");
168 item2 = new TaskItem (item1);
169 Assert.AreEqual (item1.GetMetadata ("meta1"), item2.GetMetadata ("meta1"), "A1");
170 item1.SetMetadata ("meta1", "val2");
171 Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A2");
172 Assert.AreEqual ("val1", item2.GetMetadata ("meta1"), "A3");
173 item2.SetMetadata ("meta1", "val3");
174 Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A4");
175 Assert.AreEqual ("val3", item2.GetMetadata ("meta1"), "A5");
179 public void TestCopyMetadataTo ()
181 item1 = new TaskItem ("itemSpec");
182 item2 = new TaskItem ("itemSpec");
183 item1.SetMetadata ("A", "1");
184 item1.SetMetadata ("B", "1");
185 item1.SetMetadata ("C", "1");
186 item2.SetMetadata ("B", "2");
187 item1.CopyMetadataTo (item2);
188 Assert.AreEqual ("1", item2.GetMetadata ("A"), "1");
189 Assert.AreEqual ("2", item2.GetMetadata ("B"), "2");
190 Assert.AreEqual ("1", item2.GetMetadata ("C"), "3");
194 public void TestGetMetadata ()
196 item = new TaskItem ("itemSpec");
197 item.SetMetadata ("Metadata", "Value");
198 Assert.AreEqual ("Value", item.GetMetadata ("Metadata"), "A1");
199 Assert.AreEqual (String.Empty, item.GetMetadata ("lala"), "A2");
200 Assert.AreEqual ("itemSpec", item.GetMetadata ("iDentity"), "A3");
201 Assert.AreEqual ("", item.GetMetadata ("extension"), "A4");
202 Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A5");
203 Assert.AreEqual ("", item.GetMetadata ("CreatedTime"), "A6");
204 Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A7");
205 Assert.AreEqual ("", item.GetMetadata ("AccessedTime"), "A8");
209 public void TestMetadataNames ()
211 item = new TaskItem ("itemSpec");
213 Assert.IsTrue (CompareStringCollections (item.MetadataNames, metadataNames), "A1");
215 item.SetMetadata ("a", "b");
217 Assert.AreEqual (12, item.MetadataNames.Count, "A2");
221 public void TestOpExplicit ()
223 TaskItem item = new TaskItem ("itemSpec");
224 item.SetMetadata ("a", "b");
226 Assert.AreEqual ("itemSpec", (string) item, "A1");
230 [ExpectedException (typeof (ArgumentException))]
231 public void TestRemoveMetadata1 ()
233 item = new TaskItem ("lalala");
234 item.RemoveMetadata ("EXTension");
238 [ExpectedException (typeof (ArgumentNullException))]
239 public void TestRemoveMetadata2 ()
241 item = new TaskItem ("lalala");
242 item.RemoveMetadata (null);
246 public void TestRemoveMetadata3 ()
248 item = new TaskItem ("lalala");
249 item.SetMetadata ("a", "b");
250 item.RemoveMetadata ("a");
252 Assert.AreEqual (11, item.MetadataCount, "A1");
256 public void TestSetMetadata1 ()
258 item = new TaskItem ("itemSpec");
259 item.SetMetadata ("Metadata", "Value1");
260 item.SetMetadata ("Metadata", "Value2");
261 Assert.AreEqual (item.MetadataCount, 12, "MetadataCount");
262 Assert.AreEqual ("Value2", item.GetMetadata ("Metadata"));
266 [ExpectedException (typeof (ArgumentNullException))]
267 public void TestSetMetadata2 ()
269 item = new TaskItem ("itemSpec");
270 item.SetMetadata (null, "value");
274 [ExpectedException (typeof (ArgumentNullException))]
275 public void TestSetMetadata3 ()
277 item = new TaskItem ("itemSpec");
278 item.SetMetadata ("name", null);
282 [ExpectedException (typeof (ArgumentException))]
283 public void TestSetReservedMetadata ()
285 item = new TaskItem ("lalala");
286 item.SetMetadata ("Identity", "some value");
290 public void TestSetItemSpec ()
292 var itemSpec = "foo@2x.png";
293 var escapedItemSpec = global::Microsoft.Build.BuildEngine.Utilities.Escape (itemSpec);
295 var item = new TaskItem ("foo");
296 item.ItemSpec = itemSpec;
297 Assert.AreEqual (itemSpec, item.ItemSpec, "#1");
299 item.ItemSpec = escapedItemSpec;
300 Assert.AreEqual (itemSpec, item.ItemSpec, "#2");