2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / CreateCSharpManifestResourceNameTest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text;
5
6 using NUnit.Framework;
7 using Microsoft.Build.BuildEngine;
8
9 namespace MonoTests.Microsoft.Build.Tasks
10 {
11         [TestFixture]
12         public class CreateCSharpManifestResourceNameTest
13         {
14
15                 string [,] resx_no_culture_files, resx_with_culture_files;
16                 string [,] non_resx_no_culture_files, non_resx_with_culture_files;
17
18                 public CreateCSharpManifestResourceNameTest ()
19                 {
20                         string sample_cs_path = Path.Combine ("Test", Path.Combine ("resources", "Sample.cs"));
21                         string junk_file = Path.Combine ("Test", Path.Combine ("resources", "junk.txt"));
22                         string curdir = Path.GetDirectoryName (Environment.CurrentDirectory);
23
24                         /* {Include, LogicalName, DependentUpon, TargetPath} */
25                         resx_no_culture_files = new string [,] {
26                                 // With dependent file
27                                 { "foo with space.resx", null, sample_cs_path, null },
28                                 { "foo with space.resx", "RandomName", sample_cs_path, null },
29                                 { "foo with space.resx", "RandomName", sample_cs_path, "bar with space.resx" },
30
31                                 // can't find a C# class in the .vb file
32                                 { "foo with space.resx", "RandomName", junk_file, "bar with space.resx" },
33
34                                 { "Test/resources/foo with space.resx", null, "Sample.cs", null },
35                                 { "Test/resources/foo with space.resx", "RandomName", "Sample.cs", null },
36                                 { "Test/resources/foo with space.resx", "RandomName", "Sample.cs", "bar with space.resx"},
37
38                                 // W/o dependent file
39                                 { "foo with space.resx", null, null, null },
40                                 { "foo with space.resx", "RandomName", null, null },
41
42                                 { "Test/resources folder/foo with space.resx", null, null, null },
43                                 { "Test/resources folder/foo with space.resx", "RandomName", null, null },
44                         };
45
46                         resx_with_culture_files = new string [,] {
47                                 // With dependent file
48                                 { "foo with space.de.resx", null, sample_cs_path, null },
49                                 { "foo with space.de.resx", "RandomName", sample_cs_path, null },
50                                 { "foo with space.de.resx", "RandomName", sample_cs_path, "bar with space.fr.resx" },
51
52                                 // can't find a C# class in the .vb file
53                                 { "foo with space.de.resx", "RandomName", junk_file, "bar with space.fr.resx" },
54
55                                 { "Test/resources/foo with space.de.resx", null, "Sample.cs", null },
56                                 { "Test/resources/foo with space.de.resx", "RandomName", "Sample.cs", null},
57
58                                 // W/o dependent file
59                                 { "foo with space.de.resx", null, null, null },
60                                 { "foo with space.de.resx", "RandomName", null, null },
61
62                                 { "Test/resources folder/foo with space.de.resx", null, null, null },
63                                 { "Test/resources folder/foo with space.de.resx", "RandomName", null, null }
64                         };
65
66                         non_resx_no_culture_files = new string [,] {
67                                 { "foo with space.txt", null, null },
68                                 { "foo with space.txt", "RandomName", null },
69
70                                 { "Test/resources folder/foo with space.txt", null, null },
71                                 { "Test/resources folder/foo with space.txt", "RandomName", null }
72                         };
73
74                         non_resx_with_culture_files = new string [,] {
75                                 { "foo with space.de.txt", null, null },
76                                 { "foo with space.de.txt", "RandomName", null },
77
78                                 { "Test/resources folder/foo with space.de.txt", null, null },
79                                 { "Test/resources folder/foo with space.de.txt", "RandomName", null }
80                         };
81
82                 }
83
84                 [Test]
85                 public void TestNoRootNamespaceNoCulture ()
86                 {
87                         CheckResourceNames (resx_no_culture_files, new string [] {
88                                 // w/ dependent file
89                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
90                                 "Mono.Tests.Sample", "bar with space",
91                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
92
93                                 // W/o dependent file
94                                 "Mono.Tests.Sample", "foo with space" ,
95                                 "foo with space", "Test.resources_folder.foo with space",
96                                 "Test.resources_folder.foo with space",
97                         }, null);
98                 }
99
100                 [Test]
101                 public void TestWithRootNamespaceNoCulture ()
102                 {
103                         //FIXME: How does LogicalName affect things??
104                         CheckResourceNames (resx_no_culture_files, new string [] {
105                                 // With dependent file
106                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
107                                 "Mono.Tests.Sample", "RN1.RN2.bar with space",
108                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
109
110                                 // W/o dependent file
111                                 "Mono.Tests.Sample", "RN1.RN2.foo with space",
112                                 "RN1.RN2.foo with space", "RN1.RN2.Test.resources_folder.foo with space",
113                                 "RN1.RN2.Test.resources_folder.foo with space"},
114                                 "RN1.RN2");
115                 }
116
117                 [Test]
118                 public void TestNoRootNamespaceWithCulture ()
119                 {
120                         CheckResourceNames (resx_with_culture_files, new string [] {
121                                 // With dependent file
122                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
123                                  "Mono.Tests.Sample.fr", "bar with space.fr",
124
125                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
126
127                                 // W/o dependent file
128                                  "foo with space.de", "foo with space.de",
129                                  "Test.resources_folder.foo with space.de", "Test.resources_folder.foo with space.de" }, null);
130                 }
131
132                 [Test]
133                 public void TestWithRootNamespaceWithCulture ()
134                 {
135                         CheckResourceNames (resx_with_culture_files, new string [] {
136                                 // With dependent file
137                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
138                                  "Mono.Tests.Sample.fr", "RN1.RN2.bar with space.fr",
139                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
140                                 // W/o dependent file
141                                  "RN1.RN2.foo with space.de", "RN1.RN2.foo with space.de",
142                                  "RN1.RN2.Test.resources_folder.foo with space.de", "RN1.RN2.Test.resources_folder.foo with space.de"},
143                                  "RN1.RN2");
144                 }
145
146                 [Test]
147                 public void TestNonResxNoRootNamespaceWithCulture ()
148                 {
149                         CheckResourceNames (non_resx_with_culture_files, new string [] {
150                                 Path.Combine ("de", "foo with space.txt"), Path.Combine ("de", "foo with space.txt"),
151                                 Path.Combine ("de", "Test.resources_folder.foo with space.txt"), Path.Combine ("de", "Test.resources_folder.foo with space.txt")}, null);
152
153                 }
154
155                 [Test]
156                 public void TestNonResxWithRootNamespaceWithCulture ()
157                 {
158                         CheckResourceNames (non_resx_with_culture_files, new string [] {
159                                 Path.Combine ("de", "RN1.RN2.foo with space.txt"), Path.Combine ("de", "RN1.RN2.foo with space.txt"),
160                                 Path.Combine ("de", "RN1.RN2.Test.resources_folder.foo with space.txt"), Path.Combine ("de", "RN1.RN2.Test.resources_folder.foo with space.txt")},
161                                 "RN1.RN2");
162                 }
163
164                 [Test]
165                 public void TestNonResxNoRootNamespaceNoCulture ()
166                 {
167                         CheckResourceNames (non_resx_no_culture_files, new string [] {
168                                 "foo with space.txt", "foo with space.txt",
169                                 "Test.resources_folder.foo with space.txt", "Test.resources_folder.foo with space.txt"}, null);
170                 }
171
172                 [Test]
173                 public void TestNonResxWithRootNamespaceNoCulture ()
174                 {
175                         CheckResourceNames (non_resx_no_culture_files, new string [] {
176                                 // With dependent file
177                                 "RN1.RN2.foo with space.txt", "RN1.RN2.foo with space.txt",
178                                 "RN1.RN2.Test.resources_folder.foo with space.txt", "RN1.RN2.Test.resources_folder.foo with space.txt"},
179                                 "RN1.RN2");
180                 }
181
182                 [Test]
183                 public void TestExternalResourcesNoRootNamespaceWithTargetPath ()
184                 {
185                         CheckResourceNames (new string[,] {
186                                         {"../folder/foo.txt", null, null, "abc.txt"},
187                                         {"../folder/foo.de.txt", null, null, "xyz.txt"}},
188                                         new string[] { "abc.txt", "xyz.txt" }, null);
189                 }
190
191                 [Test]
192                 public void TestExternalResourcesWithRootNamespaceWithTargetPath ()
193                 {
194                         CheckResourceNames (new string[,] {
195                                         {"../folder/foo.txt", null, null, "abc.txt"},
196                                         {"../folder/foo.de.txt", null, null, "xyz.txt"}},
197                                         new string[] { "RN.abc.txt", "RN.xyz.txt" }, "RN");
198                 }
199
200                 [Test]
201                 public void TestExternalResourcesNoRootNamespaceNoTargetPath ()
202                 {
203                         CheckResourceNames (new string[,] {
204                                         {"../folder/foo.txt", null, null},
205                                         {"../folder/foo.de.txt", null, null}},
206                                         new string[] { "...folder.foo.txt", Path.Combine ("de", "...folder.foo.txt") }, null);
207                 }
208
209                 [Test]
210                 public void TestExternalResourcesWithRootNamespaceNoTargetPath ()
211                 {
212                         CheckResourceNames (new string[,] {
213                                         {"../folder/foo.txt", null, null},
214                                         {"../folder/foo.de.txt", null, null}},
215                                         new string[] { "RN....folder.foo.txt", Path.Combine ("de", "RN....folder.foo.txt") }, "RN");
216                 }
217
218                 [Test]
219                 public void TestInvalidCulture ()
220                 {
221                         string [,] files = new string [,] {
222                                 { "Foo.invalid.txt", null, null },
223                                 { "Foo.invalid.resx", null, null }
224                         };
225                         CheckResourceNames (files, new string [] {"RN1.RN2.Foo.invalid.txt", "RN1.RN2.Foo.invalid"},
226                                 "RN1.RN2");
227                 }
228
229                 void CheckResourceNames (string [,] files, string [] names, string rootNamespace)
230                 {
231                         Assert.AreEqual (files.GetUpperBound (0) + 1, names.Length, "Number of files and names must match");
232                         string projectText = CreateProject (files, rootNamespace);
233
234                         Engine engine = new Engine (Consts.BinPath);
235                         Project project = engine.CreateNewProject ();
236                         TestMessageLogger logger = new TestMessageLogger ();
237                         engine.RegisterLogger (logger);
238                         Console.WriteLine (projectText);
239                         project.LoadXml (projectText);
240                         if (!project.Build ("1")) {
241                                 logger.DumpMessages ();
242                                 Assert.Fail ("Build failed");
243                         }
244
245                         bool has_targetpaths = files.GetUpperBound (1) == 3;
246                         BuildItemGroup group = project.GetEvaluatedItemsByName ("ResourceNames");
247                         Assert.AreEqual (names.Length, group.Count, "A2");
248                         for (int i = 0; i <= files.GetUpperBound (0); i++) {
249                                 Assert.AreEqual (names [i], group [i].FinalItemSpec, "A3 #" + (i + 1));
250                                 Assert.AreEqual (files [i, 1] != null, group [i].HasMetadata ("LogicalName"), "A4 #" + (i + 1));
251                                 if (files [i, 1] != null)
252                                         Assert.AreEqual (files [i, 1], group [i].GetMetadata ("LogicalName"), "A5 #" + (i + 1));
253                                 Assert.AreEqual (files [i, 2] != null, group [i].HasMetadata ("DependentUpon"), "A6 #" + (i + 1));
254                                 if (files [i, 2] != null)
255                                         Assert.AreEqual (files [i, 2], group [i].GetMetadata ("DependentUpon"), "A7 #" + (i + 1));
256                                 if (has_targetpaths && files [i, 3] != null)
257                                         Assert.AreEqual (files [i, 3], group [i].GetMetadata ("TargetPath"), "A8 #" + (i + 1));
258                         }
259                 }
260
261                 string CreateProject (string [,] files, string rootNamespace)
262                 {
263                         StringBuilder sb = new StringBuilder ();
264                         sb.Append ("<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
265
266                         bool has_targetpaths = files.GetUpperBound (1) == 3;
267                         sb.Append ("\t<ItemGroup>\n");
268                         for (int i = 0; i <= files.GetUpperBound (0); i ++) {
269                                 sb.AppendFormat ("\t\t<ResourceFiles Include = \"{0}\">\n", files [i, 0]);
270                                 if (has_targetpaths && files [i, 3] != null)
271                                         sb.AppendFormat ("\t\t\t<TargetPath>{0}</TargetPath>\n", files [i, 3]);
272                                 if (files [i, 1] != null)
273                                         sb.AppendFormat ("\t\t\t<LogicalName>{0}</LogicalName>\n", files [i, 1]);
274                                 if (files [i, 2] != null)
275                                         sb.AppendFormat ("\t\t\t<DependentUpon>{0}</DependentUpon>\n", files [i, 2]);
276                                 sb.AppendFormat ("\t\t</ResourceFiles>\n");
277                         }
278                         sb.Append ("\t</ItemGroup>\n");
279
280                         sb.Append ("\t<Target Name=\"1\">\n");
281                         sb.Append ("\t\t<CreateCSharpManifestResourceName ResourceFiles=\"@(ResourceFiles)\" ");
282                         if (rootNamespace != null)
283                                 sb.AppendFormat (" RootNamespace = \"{0}\"", rootNamespace);
284                         sb.Append (">\n \t\t\t<Output TaskParameter=\"ManifestResourceNames\" ItemName=\"ResourceNames\" />\n");
285                         sb.Append ("\t\t</CreateCSharpManifestResourceName>\n\t</Target>\n");
286                         sb.Append ("\t<UsingTask TaskName=\"Microsoft.Build.Tasks.CreateCSharpManifestResourceName\" " +
287                                 "AssemblyName=\"Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"/>\n");
288                         sb.Append ("</Project>");
289
290                         return sb.ToString ();
291                 }
292         }
293 }