Merge pull request #1510 from BrzVlad/fix-file-map
[mono.git] / mcs / class / System.Design / Test / System.Resources.Tools / StronglyTypedResourceBuilderResourceNameTests.cs
1 //
2 // StronglyTypedResourceBuilderResourceNameTests.cs - tests validation 
3 // of the Resource Names passed in the resourceList param of the main 
4 // Create overload and the unmatatchables returned
5 // 
6 // Author:
7 //      Gary Barnett (gary.barnett.mono@gmail.com)
8 // 
9 // Copyright (C) Gary Barnett (2012)
10 //
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.Resources.Tools;
35 using System.CodeDom;
36 using Microsoft.CSharp;
37 using System.Collections.Generic;
38
39 namespace MonoTests.System.Resources.Tools {
40         [TestFixture]
41         public class StronglyTypedResourceBuilderResourceNameTests      {
42                 CSharpCodeProvider provider = new CSharpCodeProvider ();
43                 
44                 [Test, ExpectedException (typeof (ArgumentException))]
45                 public void ResourceNamesCaseSensitiveDupes ()
46                 {
47                         // passing in case insensitive dupes of resourcenames throws exception in .NET framework
48                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
49                         string [] unmatchables;
50
51                         testResources.Add ("FortyTwo", String.Empty);
52                         testResources.Add ("fortytwo", String.Empty);                    
53                         
54                         StronglyTypedResourceBuilder.Create (testResources,
55                                                                 "TestClass",
56                                                                 "TestNamespace",
57                                                                 "TestResourcesNameSpace",
58                                                                 provider,
59                                                                 true,
60                                                                 out unmatchables);
61                 }
62
63                 [Test, ExpectedException (typeof (ArgumentException))]
64                 public void ResourceNamesCaseSensitiveDupesIgnored ()
65                 {
66                         // passing in case insensitive dupes of resourcenames to be ignored still raises exception
67                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
68                         string [] unmatchables;
69
70                         testResources.Add (">>FortyTwo", String.Empty);
71                         testResources.Add (">>fortytwo", String.Empty);      
72
73                         StronglyTypedResourceBuilder.Create (testResources,
74                                                                 "TestClass",
75                                                                 "TestNamespace",
76                                                                 "TestResourcesNameSpace",
77                                                                 provider,
78                                                                 true,
79                                                                 out unmatchables);
80                 }
81
82                 [Test, ExpectedException (typeof (ArgumentException))]
83                 public void ResourceNamesCaseSensitiveDupesInvalid ()
84                 {
85                         // passing in case insensitive dupes of invalid resourcenames still raises exception
86                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
87                         string [] unmatchables;
88
89                         testResources.Add ("Fo$rtyTwo", String.Empty);
90                         testResources.Add ("fo$rtytwo", String.Empty);                   
91                         
92                         StronglyTypedResourceBuilder.Create (testResources,
93                                                                 "TestClass",
94                                                                 "TestNamespace",
95                                                                 "TestResourcesNameSpace",
96                                                                 provider,
97                                                                 true,
98                                                                 out unmatchables);
99                 }
100
101                 [Test]
102                 public void ResourceNamesCaseSensitiveDupesWithSpecialChars ()
103                 {
104                         // resourcenames with special chars that would result in case insentive dupes go to unmatchables
105
106                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
107                         string [] unmatchables;
108
109                         testResources.Add ("Fo&rtyTwo", String.Empty);
110                         testResources.Add ("fo_rtytwo", String.Empty);                   
111                         
112                         StronglyTypedResourceBuilder.Create (testResources,
113                                                                 "TestClass",
114                                                                 "TestNamespace",
115                                                                 "TestResourcesNameSpace",
116                                                                 provider,
117                                                                 true,
118                                                                 out unmatchables);
119                         
120                         Assert.AreEqual (2,unmatchables.Length);
121                 }
122
123                 [Test]
124                 public void ResourceNamesDuplicate_NETBUG ()
125                 {
126                         /*      
127                          * DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
128                          *   language keywords have _ appended to start
129                          *   string.emtpy converted to _
130                          *   various chars replaced
131                          * .net inconsistency:
132                          * if keys contain multiple single char names made up of invalid chars
133                          * which are converted to _, sometimes none are used and all are returned 
134                          * in unmatchables, while other times one is used, and other times 
135                          * none are used but one is still missing from unmatchables like in this case
136                         */
137                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
138                         string [] unmatchables;
139                         
140                         testResources.Add ("for", "1");
141                         testResources.Add ("_for", "2"); 
142                         testResources.Add (String.Empty, String.Empty);
143                         testResources.Add ("*", String.Empty);
144                         testResources.Add ("_", String.Empty);
145                         testResources.Add (".", String.Empty);  
146                         testResources.Add ("/", String.Empty);  
147                         testResources.Add ("\\", String.Empty); 
148                         testResources.Add ("imok", "2");
149                         
150                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
151                                                                                 "TestRes",
152                                                                                 "TestNamespace",
153                                                                                 "TestResourcesNameSpace",
154                                                                                 provider,
155                                                                                 true,
156                                                                                 out unmatchables);
157                                                         
158                         int matchedResources = testResources.Count - unmatchables.Length;
159                         int membersExpected = matchedResources + 5; // 5 standard members
160                         
161                         Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);   
162                 }
163                 
164                 [Test]
165                 public void ResourceNamesDuplicate ()
166                 {
167                         /*      
168                          * DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
169                          *   language keywords have _ appended to start
170                          *   string.emtpy converted to _
171                          *   various chars replaced
172                         */
173                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
174                         string [] unmatchables;
175                         
176                         testResources.Add ("for", "1");
177                         testResources.Add ("_for", "2"); 
178                         testResources.Add ("&", String.Empty);                  
179                         testResources.Add ("_", String.Empty);
180                         testResources.Add ("imok", "2");
181                         
182                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
183                                                                                 "TestRes",
184                                                                                 "TestNamespace",
185                                                                                 "TestResourcesNameSpace",
186                                                                                 provider,
187                                                                                 true,
188                                                                                 out unmatchables);
189                         
190                         int matchedResources = testResources.Count - unmatchables.Length;
191                         int membersExpected = matchedResources + 5; // 5 standard members
192                         Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);   
193                 }
194                 
195                 [Test]
196                 public void ResourceNamesIgnored ()
197                 {
198                         // names beginning with the chars "$" and ">>" ignored and not put in unmatchables
199                         Dictionary<string, object> testResources = new Dictionary<string, object>();
200                         string [] unmatchables;
201                         
202                         testResources.Add ("$test1", String.Empty);
203                         testResources.Add ("$test2", String.Empty); 
204                         testResources.Add (">>test1", String.Empty);                    
205                         testResources.Add (">>test2", String.Empty);                     
206                         testResources.Add ("$", String.Empty);
207                         testResources.Add (">>", String.Empty); 
208                         testResources.Add (">", String.Empty);  
209                         testResources.Add (">test1", String.Empty);
210                         testResources.Add ("test>>", String.Empty);
211                         // resource name with $ somwhere else goes into unmatchables as invalid with csharpprovider
212                         
213                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
214                                                                                         "TestRes",
215                                                                                         "TestNamespace",
216                                                                                         "TestResourcesNameSpace",
217                                                                                         provider,
218                                                                                         true,
219                                                                                         out unmatchables);
220                                 
221                         Assert.AreEqual(0,unmatchables.Length);
222                         
223                         Assert.AreEqual (8,ccu.Namespaces [0].Types [0].Members.Count); // 3 valid + 5 standard
224                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
225                                                                                                         "_", ccu));
226                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
227                                                                                                         "_test1", ccu));
228                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
229                                                                                                         "test__", ccu));
230                 }
231                 
232                 [Test]
233                 public void ResourceNamesInvalid ()
234                 {
235                         // resources named Culture and ResourceManager go into unmatchables (case sensitive)
236                         // if there is a $ in resource name after char 0 it goes into unmatchables with csharpprovider
237                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
238                         string [] unmatchables;
239                         
240                         testResources.Add ("ResourceManager", String.Empty);
241                         testResources.Add ("Culture", String.Empty);
242                         testResources.Add ("t$est", String.Empty);
243
244                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
245                                                                                 "TestRes",
246                                                                                 "TestNamespace",
247                                                                                 "TestResourcesNameSpace",
248                                                                                 provider,
249                                                                                 true,
250                                                                                 out unmatchables);
251                         
252                         Assert.AreEqual (3,unmatchables.Length);
253                         Assert.AreEqual (5,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard
254                 }
255
256                 [Test]
257                 public void ResourceNamesInvalidDifferentCase ()
258                 {
259                         // resources named Culture and ResourceManager go into unmatchables (case sensitive)
260                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
261                         string [] unmatchables;
262                         
263                         testResources.Add ("resourceManager", String.Empty);
264                         testResources.Add ("culture", String.Empty);
265                         testResources.Add ("t$est", String.Empty);
266
267                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
268                                                                                 "TestRes",
269                                                                                 "TestNamespace",
270                                                                                 "TestResourcesNameSpace",
271                                                                                 provider,
272                                                                                 true,
273                                                                                 out unmatchables);
274                         
275                         Assert.AreEqual (1,unmatchables.Length);
276                         Assert.AreEqual (7,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard +2
277                 }
278
279                 [Test, ExpectedException (typeof (InvalidCastException))]
280                 public void ResourceNamesNotString ()
281                 {
282                         // throws InvalidCastException in .net framework
283                         Dictionary<object, object> testResources = new Dictionary<object, object> ();
284                         string [] unmatchables;
285                         
286                         testResources.Add (DateTime.MinValue, "1");
287                         
288                         StronglyTypedResourceBuilder.Create (testResources,
289                                                                 "TestRes",
290                                                                 "TestNamespace",
291                                                                 "TestResourcesNameSpace",
292                                                                 provider,
293                                                                 true,
294                                                                 out unmatchables);
295                         
296                 }
297                 
298         }
299 }
300