Merge pull request #409 from Alkarex/patch-1
[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 #if NET_2_0
32
33 using NUnit.Framework;
34 using System;
35 using System.Resources.Tools;
36 using System.CodeDom;
37 using Microsoft.CSharp;
38 using System.Collections.Generic;
39
40 namespace MonoTests.System.Resources.Tools {
41         [TestFixture]
42         public class StronglyTypedResourceBuilderResourceNameTests      {
43                 CSharpCodeProvider provider = new CSharpCodeProvider ();
44                 
45                 [Test, ExpectedException (typeof (ArgumentException))]
46                 public void ResourceNamesCaseSensitiveDupes ()
47                 {
48                         // passing in case insensitive dupes of resourcenames throws exception in .NET framework
49                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
50                         string [] unmatchables;
51
52                         testResources.Add ("FortyTwo", String.Empty);
53                         testResources.Add ("fortytwo", String.Empty);                    
54                         
55                         StronglyTypedResourceBuilder.Create (testResources,
56                                                                 "TestClass",
57                                                                 "TestNamespace",
58                                                                 "TestResourcesNameSpace",
59                                                                 provider,
60                                                                 true,
61                                                                 out unmatchables);
62                 }
63
64                 [Test, ExpectedException (typeof (ArgumentException))]
65                 public void ResourceNamesCaseSensitiveDupesIgnored ()
66                 {
67                         // passing in case insensitive dupes of resourcenames to be ignored still raises exception
68                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
69                         string [] unmatchables;
70
71                         testResources.Add (">>FortyTwo", String.Empty);
72                         testResources.Add (">>fortytwo", String.Empty);      
73
74                         StronglyTypedResourceBuilder.Create (testResources,
75                                                                 "TestClass",
76                                                                 "TestNamespace",
77                                                                 "TestResourcesNameSpace",
78                                                                 provider,
79                                                                 true,
80                                                                 out unmatchables);
81                 }
82
83                 [Test, ExpectedException (typeof (ArgumentException))]
84                 public void ResourceNamesCaseSensitiveDupesInvalid ()
85                 {
86                         // passing in case insensitive dupes of invalid resourcenames still raises exception
87                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
88                         string [] unmatchables;
89
90                         testResources.Add ("Fo$rtyTwo", String.Empty);
91                         testResources.Add ("fo$rtytwo", String.Empty);                   
92                         
93                         StronglyTypedResourceBuilder.Create (testResources,
94                                                                 "TestClass",
95                                                                 "TestNamespace",
96                                                                 "TestResourcesNameSpace",
97                                                                 provider,
98                                                                 true,
99                                                                 out unmatchables);
100                 }
101
102                 [Test]
103                 public void ResourceNamesCaseSensitiveDupesWithSpecialChars ()
104                 {
105                         // resourcenames with special chars that would result in case insentive dupes go to unmatchables
106
107                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
108                         string [] unmatchables;
109
110                         testResources.Add ("Fo&rtyTwo", String.Empty);
111                         testResources.Add ("fo_rtytwo", String.Empty);                   
112                         
113                         StronglyTypedResourceBuilder.Create (testResources,
114                                                                 "TestClass",
115                                                                 "TestNamespace",
116                                                                 "TestResourcesNameSpace",
117                                                                 provider,
118                                                                 true,
119                                                                 out unmatchables);
120                         
121                         Assert.AreEqual (2,unmatchables.Length);
122                 }
123
124                 [Test]
125                 public void ResourceNamesDuplicate_NETBUG ()
126                 {
127                         /*      
128                          * DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
129                          *   language keywords have _ appended to start
130                          *   string.emtpy converted to _
131                          *   various chars replaced
132                          * .net inconsistency:
133                          * if keys contain multiple single char names made up of invalid chars
134                          * which are converted to _, sometimes none are used and all are returned 
135                          * in unmatchables, while other times one is used, and other times 
136                          * none are used but one is still missing from unmatchables like in this case
137                         */
138                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
139                         string [] unmatchables;
140                         
141                         testResources.Add ("for", "1");
142                         testResources.Add ("_for", "2"); 
143                         testResources.Add (String.Empty, 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 ("\\", String.Empty); 
149                         testResources.Add ("imok", "2");
150                         
151                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
152                                                                                 "TestRes",
153                                                                                 "TestNamespace",
154                                                                                 "TestResourcesNameSpace",
155                                                                                 provider,
156                                                                                 true,
157                                                                                 out unmatchables);
158                                                         
159                         int matchedResources = testResources.Count - unmatchables.Length;
160                         int membersExpected = matchedResources + 5; // 5 standard members
161                         
162                         Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);   
163                 }
164                 
165                 [Test]
166                 public void ResourceNamesDuplicate ()
167                 {
168                         /*      
169                          * DUPES CHECK HAPPENS AFTER VerifyResourceName called which changed eg.
170                          *   language keywords have _ appended to start
171                          *   string.emtpy converted to _
172                          *   various chars replaced
173                         */
174                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
175                         string [] unmatchables;
176                         
177                         testResources.Add ("for", "1");
178                         testResources.Add ("_for", "2"); 
179                         testResources.Add ("&", String.Empty);                  
180                         testResources.Add ("_", String.Empty);
181                         testResources.Add ("imok", "2");
182                         
183                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
184                                                                                 "TestRes",
185                                                                                 "TestNamespace",
186                                                                                 "TestResourcesNameSpace",
187                                                                                 provider,
188                                                                                 true,
189                                                                                 out unmatchables);
190                         
191                         int matchedResources = testResources.Count - unmatchables.Length;
192                         int membersExpected = matchedResources + 5; // 5 standard members
193                         Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);   
194                 }
195                 
196                 [Test]
197                 public void ResourceNamesIgnored ()
198                 {
199                         // names beginning with the chars "$" and ">>" ignored and not put in unmatchables
200                         Dictionary<string, object> testResources = new Dictionary<string, object>();
201                         string [] unmatchables;
202                         
203                         testResources.Add ("$test1", String.Empty);
204                         testResources.Add ("$test2", String.Empty); 
205                         testResources.Add (">>test1", String.Empty);                    
206                         testResources.Add (">>test2", String.Empty);                     
207                         testResources.Add ("$", String.Empty);
208                         testResources.Add (">>", String.Empty); 
209                         testResources.Add (">", String.Empty);  
210                         testResources.Add (">test1", String.Empty);
211                         testResources.Add ("test>>", String.Empty);
212                         // resource name with $ somwhere else goes into unmatchables as invalid with csharpprovider
213                         
214                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
215                                                                                         "TestRes",
216                                                                                         "TestNamespace",
217                                                                                         "TestResourcesNameSpace",
218                                                                                         provider,
219                                                                                         true,
220                                                                                         out unmatchables);
221                                 
222                         Assert.AreEqual(0,unmatchables.Length);
223                         
224                         Assert.AreEqual (8,ccu.Namespaces [0].Types [0].Members.Count); // 3 valid + 5 standard
225                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
226                                                                                                         "_", ccu));
227                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
228                                                                                                         "_test1", ccu));
229                         Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
230                                                                                                         "test__", ccu));
231                 }
232                 
233                 [Test]
234                 public void ResourceNamesInvalid ()
235                 {
236                         // resources named Culture and ResourceManager go into unmatchables (case sensitive)
237                         // if there is a $ in resource name after char 0 it goes into unmatchables with csharpprovider
238                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
239                         string [] unmatchables;
240                         
241                         testResources.Add ("ResourceManager", String.Empty);
242                         testResources.Add ("Culture", String.Empty);
243                         testResources.Add ("t$est", String.Empty);
244
245                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
246                                                                                 "TestRes",
247                                                                                 "TestNamespace",
248                                                                                 "TestResourcesNameSpace",
249                                                                                 provider,
250                                                                                 true,
251                                                                                 out unmatchables);
252                         
253                         Assert.AreEqual (3,unmatchables.Length);
254                         Assert.AreEqual (5,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard
255                 }
256
257                 [Test]
258                 public void ResourceNamesInvalidDifferentCase ()
259                 {
260                         // resources named Culture and ResourceManager go into unmatchables (case sensitive)
261                         Dictionary<string, object> testResources = new Dictionary<string, object> ();
262                         string [] unmatchables;
263                         
264                         testResources.Add ("resourceManager", String.Empty);
265                         testResources.Add ("culture", String.Empty);
266                         testResources.Add ("t$est", String.Empty);
267
268                         CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
269                                                                                 "TestRes",
270                                                                                 "TestNamespace",
271                                                                                 "TestResourcesNameSpace",
272                                                                                 provider,
273                                                                                 true,
274                                                                                 out unmatchables);
275                         
276                         Assert.AreEqual (1,unmatchables.Length);
277                         Assert.AreEqual (7,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard +2
278                 }
279
280                 [Test, ExpectedException (typeof (InvalidCastException))]
281                 public void ResourceNamesNotString ()
282                 {
283                         // throws InvalidCastException in .net framework
284                         Dictionary<object, object> testResources = new Dictionary<object, object> ();
285                         string [] unmatchables;
286                         
287                         testResources.Add (DateTime.MinValue, "1");
288                         
289                         StronglyTypedResourceBuilder.Create (testResources,
290                                                                 "TestRes",
291                                                                 "TestNamespace",
292                                                                 "TestResourcesNameSpace",
293                                                                 provider,
294                                                                 true,
295                                                                 out unmatchables);
296                         
297                 }
298                 
299         }
300 }
301
302 #endif