3 using System.Collections.Generic;
8 using Microsoft.Build.BuildEngine;
10 namespace MonoTests.Microsoft.Build.Tasks
13 public class CreateVisualBasicManifestResourceNameTest
16 string [,] resx_no_culture_files, resx_with_culture_files;
17 string [,] non_resx_no_culture_files, non_resx_with_culture_files;
18 public CreateVisualBasicManifestResourceNameTest ()
20 string junk_file = Path.Combine ("Test", Path.Combine ("resources", "junk.txt"));
21 string sample_vb_path = Path.Combine ("Test", Path.Combine ("resources", "Sample.vb"));
22 string curdir = Path.GetDirectoryName (Environment.CurrentDirectory);
24 /* {Include, LogicalName, DependentUpon, TargetPath} */
25 resx_no_culture_files = new string [,] {
26 // With dependent file
27 { "foo with space.resx", null, sample_vb_path, null },
28 { "foo with space.resx", "RandomName", sample_vb_path, null },
29 { "foo with space.resx", "RandomName", sample_vb_path, "bar with space.resx" },
31 // can't find a C# class in the .vb file
32 { "foo with space.resx", "RandomName", junk_file, "bar with space.resx" },
34 { "Test/resources/foo with space.resx", null, "Sample.vb", null },
35 { "Test/resources/foo with space.resx", "RandomName", "Sample.vb", null },
36 { "Test/resources/foo with space.resx", "RandomName", "Sample.vb", "bar with space.resx"},
39 { "foo with space.resx", null, null, null },
40 { "foo with space.resx", "RandomName", null, null },
42 { "Test/resources folder/foo with space.resx", null, null, null },
43 { "Test/resources folder/foo with space.resx", "RandomName", null, null },
46 resx_with_culture_files = new string [,] {
47 // With dependent file
48 { "foo with space.de.resx", null, sample_vb_path, null },
49 { "foo with space.de.resx", "RandomName", sample_vb_path, null },
50 { "foo with space.de.resx", "RandomName", sample_vb_path, "bar with space.fr.resx" },
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" },
55 { "Test/resources/foo with space.de.resx", null, "Sample.vb", null },
56 { "Test/resources/foo with space.de.resx", "RandomName", "Sample.vb", null},
59 { "foo with space.de.resx", null, null, null },
60 { "foo with space.de.resx", "RandomName", null, null },
62 { "Test/resources folder/foo with space.de.resx", null, null, null },
63 { "Test/resources folder/foo with space.de.resx", "RandomName", null, null }
66 non_resx_no_culture_files = new string [,] {
67 { "foo with space.txt", null, null },
68 { "foo with space.txt", "RandomName", null },
70 { "Test/resources folder/foo with space.txt", null, null },
71 { "Test/resources folder/foo with space.txt", "RandomName", null }
74 non_resx_with_culture_files = new string [,] {
75 { "foo with space.de.txt", null, null },
76 { "foo with space.de.txt", "RandomName", null },
78 { "Test/resources folder/foo with space.de.txt", null, null },
79 { "Test/resources folder/foo with space.de.txt", "RandomName", null }
85 public void TestNoRootNamespaceNoCulture ()
87 CheckResourceNames (resx_no_culture_files, new string [] {
89 "Mono.Tests.Sample", "Mono.Tests.Sample",
90 "Mono.Tests.Sample", "bar with space",
91 "Mono.Tests.Sample", "Mono.Tests.Sample",
94 "Mono.Tests.Sample", "foo with space" ,
95 "foo with space", "foo with space",
101 public void TestWithRootNamespaceNoCulture ()
103 //FIXME: How does LogicalName affect things??
104 CheckResourceNames (resx_no_culture_files, new string [] {
105 // With dependent file
106 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.Mono.Tests.Sample",
107 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.bar with space",
108 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.Mono.Tests.Sample",
110 // W/o dependent file
111 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.foo with space",
112 "RN1.RN2.foo with space", "RN1.RN2.foo with space",
113 "RN1.RN2.foo with space"},
118 public void TestNoRootNamespaceWithCulture ()
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",
125 "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
127 // W/o dependent file
128 "foo with space.de", "foo with space.de",
129 "foo with space.de", "foo with space.de" }, null);
133 public void TestWithRootNamespaceWithCulture ()
135 CheckResourceNames (resx_with_culture_files, new string [] {
136 // With dependent file
137 "RN1.RN2.Mono.Tests.Sample.de", "RN1.RN2.Mono.Tests.Sample.de",
138 "RN1.RN2.Mono.Tests.Sample.fr", "RN1.RN2.bar with space.fr",
139 "RN1.RN2.Mono.Tests.Sample.de", "RN1.RN2.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.foo with space.de", "RN1.RN2.foo with space.de"},
147 public void TestNonResxNoRootNamespaceWithCulture ()
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", "foo with space.txt"), Path.Combine ("de", "foo with space.txt")}, null);
156 public void TestNonResxWithRootNamespaceWithCulture ()
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.foo with space.txt"), Path.Combine ("de", "RN1.RN2.foo with space.txt")},
165 public void TestNonResxNoRootNamespaceNoCulture ()
167 CheckResourceNames (non_resx_no_culture_files, new string [] {
168 "foo with space.txt", "foo with space.txt",
169 "foo with space.txt", "foo with space.txt"}, null);
173 public void TestNonResxWithRootNamespaceNoCulture ()
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.foo with space.txt", "RN1.RN2.foo with space.txt"},
183 public void TestExternalResourcesNoRootNamespaceWithTargetPath ()
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);
192 public void TestExternalResourcesWithRootNamespaceWithTargetPath ()
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");
201 public void TestExternalResourcesNoRootNamespaceNoTargetPath ()
203 CheckResourceNames (new string[,] {
204 {"../folder/foo.txt", null, null},
205 {"../folder/foo.de.txt", null, null}},
206 new string[] { "foo.txt", Path.Combine ("de", "foo.txt") }, null);
210 public void TestExternalResourcesWithRootNamespaceNoTargetPath ()
212 CheckResourceNames (new string[,] {
213 {"../folder/foo.txt", null, null},
214 {"../folder/foo.de.txt", null, null}},
215 new string[] { "RN.foo.txt", Path.Combine ("de", "RN.foo.txt") }, "RN");
219 public void TestInvalidCulture ()
221 string [,] files = new string [,] {
222 { "Foo.invalid.txt", null, null },
223 { "Foo.invalid.resx", null, null }
225 CheckResourceNames (files, new string [] {"RN1.RN2.Foo.invalid.txt", "RN1.RN2.Foo.invalid"},
229 void CheckResourceNames (string [,] files, string [] names, string rootNamespace)
231 Assert.AreEqual (files.GetUpperBound (0) + 1, names.Length, "Number of files and names must match");
232 string projectText = CreateProject (files, rootNamespace);
234 Engine engine = new Engine (Consts.BinPath);
235 Project project = engine.CreateNewProject ();
236 TestMessageLogger logger = new TestMessageLogger ();
237 engine.RegisterLogger (logger);
238 project.LoadXml (projectText);
239 if (!project.Build ("1")) {
240 Console.WriteLine (projectText);
241 logger.DumpMessages ();
242 Assert.Fail ("Build failed");
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));
261 string CreateProject (string [,] files, string rootNamespace)
263 StringBuilder sb = new StringBuilder ();
264 sb.Append ("<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
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");
278 sb.Append ("\t</ItemGroup>\n");
280 sb.Append ("\t<Target Name=\"1\">\n");
281 sb.Append ("\t\t<CreateVisualBasicManifestResourceName 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</CreateVisualBasicManifestResourceName>\n\t</Target>\n");
286 sb.Append ("\t" + GetUsingTask ("CreateVisualBasicManifestResourceName"));
287 sb.Append ("</Project>");
289 return sb.ToString ();
292 string GetUsingTask (string taskName)
294 return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";