Requires gmcs
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / CreateVisualBasicManifestResourceNameTest.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 CreateVisualBasicManifestResourceNameTest
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 CreateVisualBasicManifestResourceNameTest ()
19                 {
20                         string sample_vb_path = Path.Combine ("Test", Path.Combine ("resources", "Sample.vb"));
21                         resx_no_culture_files = new string [,] {
22                                 // With dependent file
23                                 { "foo with space.resx", null, sample_vb_path },
24                                 { "foo with space.resx", "RandomName", sample_vb_path },
25
26                                 { "Test/resources/foo with space.resx", null, "Sample.vb" },
27                                 { "Test/resources/foo with space.resx", "RandomName", "Sample.vb" },
28
29                                 // W/o dependent file
30                                 { "foo with space.resx", null, null },
31                                 { "foo with space.resx", "RandomName", null },
32
33                                 { "Test/resources/foo with space.resx", null, null },
34                                 { "Test/resources/foo with space.resx", "RandomName", null },
35                         };
36
37                         resx_with_culture_files = new string [,] {
38                                 // With dependent file
39                                 { "foo with space.de.resx", null, sample_vb_path },
40                                 { "foo with space.de.resx", "RandomName", sample_vb_path },
41
42                                 { "Test/resources/foo with space.de.resx", null, "Sample.vb" },
43                                 { "Test/resources/foo with space.de.resx", "RandomName", "Sample.vb" },
44
45                                 // W/o dependent file
46                                 { "foo with space.de.resx", null, null },
47                                 { "foo with space.de.resx", "RandomName", null },
48
49                                 { "Test/resources folder/foo with space.de.resx", null, null },
50                                 { "Test/resources folder/foo with space.de.resx", "RandomName", null }
51                         };
52
53                         non_resx_no_culture_files = new string [,] {
54                                 { "foo with space.txt", null, null },
55                                 { "foo with space.txt", "RandomName", null },
56
57                                 { "Test/resources folder/foo with space.txt", null, null },
58                                 { "Test/resources folder/foo with space.txt", "RandomName", null }
59                         };
60
61                         non_resx_with_culture_files = new string [,] {
62                                 { "foo with space.de.txt", null, null },
63                                 { "foo with space.de.txt", "RandomName", null },
64
65                                 { "Test/resources folder/foo with space.de.txt", null, null },
66                                 { "Test/resources folder/foo with space.de.txt", "RandomName", null }
67                         };
68
69                 }
70
71                 [Test]
72                 public void TestNoRootNamespaceNoCulture ()
73                 {
74                         CheckResourceNames (resx_no_culture_files, new string [] {
75                                 // w/ dependent file
76                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
77                                 "Mono.Tests.Sample", "Mono.Tests.Sample",
78
79                                 // W/o dependent file
80                                 "foo with space", "foo with space" ,
81                                 "foo with space", "foo with space"}, null);
82                 }
83
84                 [Test]
85                 public void TestWithRootNamespaceNoCulture ()
86                 {
87                         //FIXME: How does LogicalName affect things??
88                         CheckResourceNames (resx_no_culture_files, new string [] {
89                                 // With dependent file
90                                 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.Mono.Tests.Sample",
91                                 "RN1.RN2.Mono.Tests.Sample", "RN1.RN2.Mono.Tests.Sample",
92                                 // W/o dependent file
93                                 "RN1.RN2.foo with space", "RN1.RN2.foo with space",
94                                 "RN1.RN2.foo with space", "RN1.RN2.foo with space"},
95                                 "RN1.RN2");
96                 }
97
98                 [Test]
99                 public void TestNoRootNamespaceWithCulture ()
100                 {
101                         CheckResourceNames (resx_with_culture_files, new string [] {
102                                 // With dependent file
103                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
104                                  "Mono.Tests.Sample.de", "Mono.Tests.Sample.de",
105                                 // W/o dependent file
106                                  "foo with space.de", "foo with space.de",
107                                  "foo with space.de", "foo with space.de" }, null);
108                 }
109
110                 [Test]
111                 public void TestWithRootNamespaceWithCulture ()
112                 {
113                         CheckResourceNames (resx_with_culture_files, new string [] {
114                                 // With dependent file
115                                  "RN1.RN2.Mono.Tests.Sample.de", "RN1.RN2.Mono.Tests.Sample.de",
116                                  "RN1.RN2.Mono.Tests.Sample.de", "RN1.RN2.Mono.Tests.Sample.de",
117                                 // W/o dependent file
118                                  "RN1.RN2.foo with space.de", "RN1.RN2.foo with space.de",
119                                  "RN1.RN2.foo with space.de", "RN1.RN2.foo with space.de"},
120                                  "RN1.RN2");
121                 }
122
123                 [Test]
124                 public void TestNonResxNoRootNamespaceWithCulture ()
125                 {
126                         CheckResourceNames (non_resx_with_culture_files, new string [] {
127                                 Path.Combine ("de", "foo with space.txt"), Path.Combine ("de", "foo with space.txt"),
128                                 Path.Combine ("de", "foo with space.txt"), Path.Combine ("de", "foo with space.txt")}, null);
129                 }
130
131                 [Test]
132                 public void TestNonResxWithRootNamespaceWithCulture ()
133                 {
134                         CheckResourceNames (non_resx_with_culture_files, new string [] {
135                                 Path.Combine ("de", "RN1.RN2.foo with space.txt"), Path.Combine ("de", "RN1.RN2.foo with space.txt"),
136                                 Path.Combine ("de", "RN1.RN2.foo with space.txt"), Path.Combine ("de", "RN1.RN2.foo with space.txt")},
137                                 "RN1.RN2");
138                 }
139
140                 [Test]
141                 public void TestNonResxNoRootNamespaceNoCulture ()
142                 {
143                         CheckResourceNames (non_resx_no_culture_files, new string [] {
144                                 "foo with space.txt", "foo with space.txt",
145                                 "foo with space.txt", "foo with space.txt"}, null);
146                 }
147
148                 [Test]
149                 public void TestNonResxWithRootNamespaceNoCulture ()
150                 {
151                         CheckResourceNames (non_resx_no_culture_files, new string [] {
152                                 // With dependent file
153                                 "RN1.RN2.foo with space.txt", "RN1.RN2.foo with space.txt",
154                                 "RN1.RN2.foo with space.txt", "RN1.RN2.foo with space.txt"},
155                                 "RN1.RN2");
156                 }
157
158                 void CheckResourceNames (string [,] files, string [] names, string rootNamespace)
159                 {
160                         Assert.AreEqual (files.GetUpperBound (0) + 1, names.Length, "Number of files and names must match");
161                         string projectText = CreateProject (files, rootNamespace);
162
163                         Engine engine = new Engine (Consts.BinPath);
164                         Project project = engine.CreateNewProject ();
165                         TestMessageLogger logger = new TestMessageLogger ();
166                         engine.RegisterLogger (logger);
167                         Console.WriteLine (projectText);
168                         project.LoadXml (projectText);
169                         if (!project.Build ("1")) {
170                                 logger.DumpMessages ();
171                                 Assert.Fail ("Build failed");
172                         }
173
174                         BuildItemGroup group = project.GetEvaluatedItemsByName ("ResourceNames");
175                         Assert.AreEqual (names.Length, group.Count, "A2");
176                         for (int i = 0; i <= files.GetUpperBound (0); i++) {
177                                 Assert.AreEqual (names [i], group [i].FinalItemSpec, "A3 #" + (i + 1));
178                                 Assert.AreEqual (files [i, 1] != null, group [i].HasMetadata ("LogicalName"), "A4 #" + (i + 1));
179                                 if (files [i, 1] != null)
180                                         Assert.AreEqual (files [i, 1], group [i].GetMetadata ("LogicalName"), "A5 #" + (i + 1));
181                                 Assert.AreEqual (files [i, 2] != null, group [i].HasMetadata ("DependentUpon"), "A6 #" + (i + 1));
182                                 if (files [i, 2] != null)
183                                         Assert.AreEqual (files [i, 2], group [i].GetMetadata ("DependentUpon"), "A7 #" + (i + 1));
184                         }
185                 }
186
187                 string CreateProject (string [,] files, string rootNamespace)
188                 {
189                         StringBuilder sb = new StringBuilder ();
190                         sb.Append ("<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
191
192                         sb.Append ("\t<ItemGroup>\n");
193                         for (int i = 0; i <= files.GetUpperBound (0); i ++) {
194                                 sb.AppendFormat ("\t\t<ResourceFiles Include = \"{0}\">\n", files [i, 0]);
195                                 if (files [i, 1] != null)
196                                         sb.AppendFormat ("\t\t\t<LogicalName>{0}</LogicalName>\n", files [i, 1]);
197                                 if (files [i, 2] != null)
198                                         sb.AppendFormat ("\t\t\t<DependentUpon>{0}</DependentUpon>\n", files [i, 2]);
199                                 sb.AppendFormat ("\t\t</ResourceFiles>\n");
200                         }
201                         sb.Append ("\t</ItemGroup>\n");
202
203                         sb.Append ("\t<Target Name=\"1\">\n");
204                         sb.Append ("\t\t<CreateVisualBasicManifestResourceName ResourceFiles=\"@(ResourceFiles)\" ");
205                         if (rootNamespace != null)
206                                 sb.AppendFormat (" RootNamespace = \"{0}\"", rootNamespace);
207                         sb.Append (">\n \t\t\t<Output TaskParameter=\"ManifestResourceNames\" ItemName=\"ResourceNames\" />\n");
208                         sb.Append ("\t\t</CreateVisualBasicManifestResourceName>\n\t</Target>\n");
209                         sb.Append ("\t<UsingTask TaskName=\"Microsoft.Build.Tasks.CreateVisualBasicManifestResourceName\" " +
210                                 "AssemblyName=\"Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"/>\n");
211                         sb.Append ("</Project>");
212
213                         return sb.ToString ();
214                 }
215         }
216 }