Merge pull request #2721 from ludovic-henry/fix-mono_ms_ticks
[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                 Engine engine;
46
47                 [SetUp]
48                 public void SetUp ()
49                 {
50                         if ('/' == DSC) {
51                                 OS = OsType.Unix;
52                         } else if ('\\' == DSC) {
53                                 OS = OsType.Windows;
54                         } else {
55                                 OS = OsType.Mac;
56                                 //FIXME: For Mac. figure this out when we need it
57                         }
58
59                         files = new string [] {
60                                 //resx files
61                                 ".\\foo.resx", @"bar\foo.resx", 
62                                 "foo.fr.resx", @"dir\abc.en.resx", "foo.bar.resx",
63                                 //non-resx
64                                 "sample.txt", @"bar\sample.txt",
65                                 "sample.it.png", @"dir\sample.en.png", "sample.inv.txt"};
66
67                         engine = new Engine (Consts.BinPath);
68                         project = engine.CreateNewProject ();
69                 }
70
71                 [Test]
72                 public void TestAssignedFiles ()
73                 {
74                         LoadAndBuildProject (files);
75
76                         //AssignedFiles
77                         if (OS == OsType.Unix) {
78                                 CheckItems (new string [] {"./foo.resx", "bar/foo.resx", "foo.fr.resx", "dir/abc.en.resx", "foo.bar.resx",
79                                         "sample.txt", "bar/sample.txt", "sample.it.png", "dir/sample.en.png", "sample.inv.txt"},
80                                         new string [] {null, null, "fr", "en", null, null, null, "it", "en", null},
81                                         "AssignedFiles", "A2");
82                         } else if (OS == OsType.Windows) {
83                                 CheckItems (new string [] {".\\foo.resx", @"bar\foo.resx", "foo.fr.resx", @"dir\abc.en.resx", "foo.bar.resx",
84                                         "sample.txt", @"bar\sample.txt", "sample.it.png", @"dir\sample.en.png", "sample.inv.txt"},
85                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
86                                         "AssignedFiles", "A2");
87                         }
88                 }
89
90                 [Test]
91                 public void TestAssignedFilesWithCulture ()
92                 {
93                         LoadAndBuildProject (files);
94
95                         //AssignedFilesWithCulture
96                         if (OS == OsType.Unix) {
97                                 CheckItems (new string [] { "foo.fr.resx", "dir/abc.en.resx", "sample.it.png", "dir/sample.en.png" },
98                                         new string [] {"fr", "en", "it", "en"},
99                                         "AssignedFilesWithCulture", "A2");
100                         } else if (OS == OsType.Windows) {
101                                 CheckItems (new string [] { "foo.fr.resx", @"dir\abc.en.resx", "sample.it.png", @"dir\sample.en.png" },
102                                         new string [] { "fr", "en", "it", "en" },
103                                         "AssignedFilesWithCulture", "A2");
104                         }
105                 }
106
107                 [Test]
108                 public void TestAssignedFilesWithNoCulture ()
109                 {
110                         LoadAndBuildProject (files);
111
112                         //AssignedFilesWithNoCulture
113                         if (OS == OsType.Unix) {
114                                 CheckItems (new string [] { "./foo.resx", "bar/foo.resx", "foo.bar.resx", "sample.txt", "bar/sample.txt", "sample.inv.txt"},
115                                         null, "AssignedFilesWithNoCulture", "A2");
116                         } else if (OS == OsType.Windows) {
117                                 CheckItems (new string [] { ".\\foo.resx", @"bar\foo.resx", "foo.bar.resx", "sample.txt", @"bar\sample.txt", "sample.inv.txt"},
118                                         null, "AssignedFilesWithNoCulture", "A2");
119                         }
120                 }
121
122                 [Test]
123                 public void TestCultureNeutralAssignedFiles ()
124                 {
125                         LoadAndBuildProject (files);
126
127                         //CultureNeutralAssignedFiles
128                         if (OS == OsType.Unix) {
129                                 CheckItems (new string [] { "./foo.resx", "bar/foo.resx", "foo.resx", "dir/abc.resx", "foo.bar.resx",
130                                         "sample.txt", "bar/sample.txt", "sample.png", "dir/sample.png", "sample.inv.txt"},
131                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
132                                         "CultureNeutralAssignedFiles", "A2");
133                         } else if (OS == OsType.Windows) {
134                                 CheckItems (new string [] { ".\\foo.resx", @"bar\foo.resx", "foo.resx", @"dir\abc.resx", "foo.bar.resx",
135                                         "sample.txt", @"bar\sample.txt", "sample.png", @"dir\sample.png", "sample.inv.txt"},
136                                         new string [] { null, null, "fr", "en", null, null, null, "it", "en", null },
137                                         "CultureNeutralAssignedFiles", "A2");
138                         }
139                 }
140
141                 void LoadAndBuildProject (string [] files_list)
142                 {
143                         string projectText = CreateProjectString (files_list);
144                         project.LoadXml (projectText);
145
146                         TestMessageLogger testLogger = new TestMessageLogger ();
147                         engine.RegisterLogger (testLogger);
148
149                         if (!project.Build ("1")) {
150                                 testLogger.DumpMessages ();
151                                 Assert.Fail ("A1 : Error in building");
152                         }
153                 }
154
155                 void CheckItems (string [] values, string [] cultures, string itemlist_name, string prefix)
156                 {
157                         BuildItemGroup group = project.GetEvaluatedItemsByName (itemlist_name);
158                         Assert.AreEqual (values.Length, group.Count, prefix + "#1");
159                         for (int i = 0; i < values.Length; i++) {
160                                 Assert.AreEqual (values [i], group [i].FinalItemSpec, prefix + "#2");
161                                 Assert.IsTrue (group [i].HasMetadata ("Child"), prefix + "#3");
162                                 Assert.AreEqual ("ChildValue", group [i].GetMetadata ("Child"), prefix + "#4");
163                                 Assert.AreEqual (cultures != null && cultures [i] != null, group [i].HasMetadata ("Culture"), prefix + "#5");
164                                 if (cultures != null && cultures [i] != null)
165                                         Assert.AreEqual (cultures [i], group [i].GetMetadata ("Culture"), prefix + "#6");
166                         }
167                 }
168
169                 string CreateProjectString (string [] files)
170                 {
171                         StringBuilder sb = new StringBuilder ();
172                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""><ItemGroup>");
173                         foreach (string file in files)
174                                 sb.AppendFormat ("<Files Include=\"{0}\"><Child>ChildValue</Child></Files>\n", file);
175
176                         sb.Append (@"</ItemGroup>
177                         <Target Name=""1"">
178                                 <AssignCulture Files=""@(Files)"" >
179                                         <Output TaskParameter=""AssignedFiles"" ItemName=""AssignedFiles"" />
180                                         <Output TaskParameter=""AssignedFilesWithCulture"" ItemName=""AssignedFilesWithCulture"" />
181                                         <Output TaskParameter=""AssignedFilesWithNoCulture"" ItemName=""AssignedFilesWithNoCulture"" />
182                                         <Output TaskParameter=""CultureNeutralAssignedFiles"" ItemName=""CultureNeutralAssignedFiles"" />
183                                 </AssignCulture>
184                         </Target>
185                 </Project>");
186
187                         return sb.ToString ();
188                 }
189         }
190 }