5aa8a5350200736af6547608b62b8f9bdbbb199b
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / AssignCultureTest.cs
1 //
2 // AssignCultureTest.cs
3 //
4 // Author:
5 //   Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2008 Novell, Inc (http://www.novell.com)
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.Generic;
30 using System.IO;
31 using System.Text;
32 using NUnit.Framework;
33 using Microsoft.Build.BuildEngine;
34
35 namespace MonoTests.Microsoft.Build.Tasks
36 {
37         [TestFixture]
38         public class AssignCultureTest
39         {
40                 static OsType OS;
41                 static char DSC = Path.DirectorySeparatorChar;
42
43                 string [] files;
44                 Project project;
45
46                 [SetUp]
47                 public void SetUp ()
48                 {
49                         if ('/' == DSC) {
50                                 OS = OsType.Unix;
51                         } else if ('\\' == DSC) {
52                                 OS = OsType.Windows;
53                         } else {
54                                 OS = OsType.Mac;
55                                 //FIXME: For Mac. figure this out when we need it
56                         }
57
58                         files = new string [] {
59                                 //resx files
60                                 ".\\foo.resx", @"bar\foo.resx", 
61                                 "foo.fr.resx", @"dir\abc.en.resx", "foo.bar.resx",
62                                 //non-resx
63                                 "sample.txt", @"bar\sample.txt",
64                                 "sample.it.png", @"dir\sample.en.png", "sample.inv.txt"};
65
66                         Engine engine = new Engine (Consts.BinPath);
67                         project = engine.CreateNewProject ();
68                 }
69
70                 [Test]
71                 public void TestAssignedFiles ()
72                 {
73                         LoadAndBuildProject (files);
74
75                         //AssignedFiles
76                         if (OS == OsType.Unix) {
77                                 CheckItems (new string [] {"./foo.resx", "bar/foo.resx", "foo.fr.resx", "dir/abc.en.resx", "foo.bar.resx",
78                                         "sample.txt", "bar/sample.txt", "sample.it.png", "dir/sample.en.png", "sample.inv.txt"},
79                                         new string [] {null, null, "fr", "en", null, null, null, "it", "en", null},
80                                         "AssignedFiles", "A2");
81                         } else if (OS == OsType.Windows) {
82                                 CheckItems (new string [] {".\\foo.resx", @"bar\foo.resx", "foo.fr.resx", @"dir\abc.en.resx", "foo.bar.resx",
83                                         "sample.txt", @"bar\sample.txt", "sample.it.png", @"dir\sample.en.png", "sample.inv.txt"},
84                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
85                                         "AssignedFiles", "A2");
86                         }
87                 }
88
89                 [Test]
90                 public void TestAssignedFilesWithCulture ()
91                 {
92                         LoadAndBuildProject (files);
93
94                         //AssignedFilesWithCulture
95                         if (OS == OsType.Unix) {
96                                 CheckItems (new string [] { "foo.fr.resx", "dir/abc.en.resx", "sample.it.png", "dir/sample.en.png" },
97                                         new string [] {"fr", "en", "it", "en"},
98                                         "AssignedFilesWithCulture", "A2");
99                         } else if (OS == OsType.Windows) {
100                                 CheckItems (new string [] { "foo.fr.resx", @"dir\abc.en.resx", "sample.it.png", @"dir\sample.en.png" },
101                                         new string [] { "fr", "en", "it", "en" },
102                                         "AssignedFilesWithCulture", "A2");
103                         }
104                 }
105
106                 [Test]
107                 public void TestAssignedFilesWithNoCulture ()
108                 {
109                         LoadAndBuildProject (files);
110
111                         //AssignedFilesWithNoCulture
112                         if (OS == OsType.Unix) {
113                                 CheckItems (new string [] { "./foo.resx", "bar/foo.resx", "foo.bar.resx", "sample.txt", "bar/sample.txt", "sample.inv.txt"},
114                                         null, "AssignedFilesWithNoCulture", "A2");
115                         } else if (OS == OsType.Windows) {
116                                 CheckItems (new string [] { ".\\foo.resx", @"bar\foo.resx", "foo.bar.resx", "sample.txt", @"bar\sample.txt", "sample.inv.txt"},
117                                         null, "AssignedFilesWithNoCulture", "A2");
118                         }
119                 }
120
121                 [Test]
122                 public void TestCultureNeutralAssignedFiles ()
123                 {
124                         LoadAndBuildProject (files);
125
126                         //CultureNeutralAssignedFiles
127                         if (OS == OsType.Unix) {
128                                 CheckItems (new string [] { "./foo.resx", "bar/foo.resx", "foo.resx", "dir/abc.resx", "foo.bar.resx",
129                                         "sample.txt", "bar/sample.txt", "sample.png", "dir/sample.png", "sample.inv.txt"},
130                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
131                                         "CultureNeutralAssignedFiles", "A2");
132                         } else if (OS == OsType.Windows) {
133                                 CheckItems (new string [] { ".\\foo.resx", @"bar\foo.resx", "foo.resx", @"dir\abc.resx", "foo.bar.resx",
134                                         "sample.txt", @"bar\sample.txt", "sample.png", @"dir\sample.png", "sample.inv.txt"},
135                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
136                                         "CultureNeutralAssignedFiles", "A2");
137                         }
138                 }
139
140                 void LoadAndBuildProject (string [] files_list)
141                 {
142                         string projectText = CreateProjectString (files_list);
143                         project.LoadXml (projectText);
144                         Assert.IsTrue (project.Build ("1"), "A1 : Error in building");
145                 }
146
147                 void CheckItems (string [] values, string [] cultures, string itemlist_name, string prefix)
148                 {
149                         BuildItemGroup group = project.GetEvaluatedItemsByName (itemlist_name);
150                         Assert.AreEqual (values.Length, group.Count, prefix + "#1");
151                         for (int i = 0; i < values.Length; i++) {
152                                 Assert.AreEqual (values [i], group [i].FinalItemSpec, prefix + "#2");
153                                 Assert.IsTrue (group [i].HasMetadata ("Child"), prefix + "#3");
154                                 Assert.AreEqual ("ChildValue", group [i].GetMetadata ("Child"), prefix + "#4");
155                                 Assert.AreEqual (cultures != null && cultures [i] != null, group [i].HasMetadata ("Culture"), prefix + "#5");
156                                 if (cultures != null && cultures [i] != null)
157                                         Assert.AreEqual (cultures [i], group [i].GetMetadata ("Culture"), prefix + "#6");
158                         }
159                 }
160
161                 string CreateProjectString (string [] files)
162                 {
163                         StringBuilder sb = new StringBuilder ();
164                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
165                         foreach (string file in files)
166                                 sb.AppendFormat ("<Files Include=\"{0}\"><Child>ChildValue</Child></Files>\n", file);
167
168                         sb.Append (@"</ItemGroup>
169                         <Target Name=""1"">
170                                 <AssignCulture Files=""@(Files)"" >
171                                         <Output TaskParameter=""AssignedFiles"" ItemName=""AssignedFiles"" />
172                                         <Output TaskParameter=""AssignedFilesWithCulture"" ItemName=""AssignedFilesWithCulture"" />
173                                         <Output TaskParameter=""AssignedFilesWithNoCulture"" ItemName=""AssignedFilesWithNoCulture"" />
174                                         <Output TaskParameter=""CultureNeutralAssignedFiles"" ItemName=""CultureNeutralAssignedFiles"" />
175                                 </AssignCulture>
176                         </Target>
177                 </Project>");
178
179                         return sb.ToString ();
180                 }
181         }
182 }