2 // AssignCultureTest.cs
5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2008 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
32 using NUnit.Framework;
33 using Microsoft.Build.BuildEngine;
35 namespace MonoTests.Microsoft.Build.Tasks
38 public class AssignCultureTest
41 static char DSC = Path.DirectorySeparatorChar;
52 } else if ('\\' == DSC) {
56 //FIXME: For Mac. figure this out when we need it
59 files = new string [] {
61 ".\\foo.resx", @"bar\foo.resx",
62 "foo.fr.resx", @"dir\abc.en.resx", "foo.bar.resx",
64 "sample.txt", @"bar\sample.txt",
65 "sample.it.png", @"dir\sample.en.png", "sample.inv.txt"};
67 engine = new Engine (Consts.BinPath);
68 project = engine.CreateNewProject ();
72 public void TestAssignedFiles ()
74 LoadAndBuildProject (files);
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");
91 public void TestAssignedFilesWithCulture ()
93 LoadAndBuildProject (files);
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");
108 public void TestAssignedFilesWithNoCulture ()
110 LoadAndBuildProject (files);
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");
123 public void TestCultureNeutralAssignedFiles ()
125 LoadAndBuildProject (files);
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");
141 void LoadAndBuildProject (string [] files_list)
143 string projectText = CreateProjectString (files_list);
144 project.LoadXml (projectText);
146 TestMessageLogger testLogger = new TestMessageLogger ();
147 engine.RegisterLogger (testLogger);
149 if (!project.Build ("1")) {
150 testLogger.DumpMessages ();
151 Assert.Fail ("A1 : Error in building");
155 void CheckItems (string [] values, string [] cultures, string itemlist_name, string prefix)
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");
169 string CreateProjectString (string [] files)
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);
176 sb.Append (@"</ItemGroup>
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"" />
187 return sb.ToString ();