[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Design / Test / System.Resources.Tools / StronglyTypedResourceBuilderNamespaceTests.cs
1 //
2 // StronglyTypedResourceBuilderNamespaceTests.cs - tests the 
3 // generatedCodeNamespace and resourcesNamespace params of the main 
4 // Create overload
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 StronglyTypedResourceBuilderNamespaceTests {
42                 static string [] keywords = {"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", 
43                                         "checked", "class", "const", "continue", "decimal", "default", "delegate", 
44                                         "do", "double", "else", "enum", "event", "explicit", "extern", "FALSE", 
45                                         "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", 
46                                         "implicit", "in", "int", "interface", "internal", "is", "lock", "long", 
47                                         "namespace", "new", "null", "object", "operator", "out", "override", "params", 
48                                         "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", 
49                                         "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", 
50                                         "throw", "TRUE", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", 
51                                         "ushort", "using", "virtual", "volatile", "void", "while" };
52                 static char [] specialChars = { ' ', '\u00A0', '.', ',', ';', '|', '~', '@', '#', '%', '^', '&', 
53                                         '*', '+', '-', '/', '\\', '<', '>', '?', '[', ']', '(', ')', '{', 
54                                         '}', '\"', '\'', ':', '!'};
55                 CSharpCodeProvider provider = new CSharpCodeProvider ();
56                 Dictionary<string, object> testResources;
57                 
58                 [SetUp]
59                 public void Setup ()
60                 {
61                         testResources = new Dictionary<string, object> ();
62                         testResources.Add ("akey", String.Empty);
63                 }
64                 
65                 [Test]
66                 public void GeneratedCodeNamespaceEmpty ()
67                 {
68                         // empty namespace allowed
69                         string [] unmatchables;
70                         string input, expected;
71                         CodeCompileUnit ccu;
72                         
73                         input = String.Empty;
74                         
75                         expected = String.Empty;
76                         
77                         ccu = StronglyTypedResourceBuilder.Create (testResources,
78                                                                 "TestClass",
79                                                                 input,
80                                                                 "TestResourceNameSpace",
81                                                                 provider,
82                                                                 true,
83                                                                 out unmatchables);
84                         
85                         Assert.AreEqual (expected,ccu.Namespaces [0].Name);
86                 }
87                 
88                 [Test]
89                 public void GeneratedCodeNamespaceNull ()
90                 {
91                         // null should be replaced with String.Empty
92                         string [] unmatchables;
93                         string input, expected;
94                         CodeCompileUnit ccu;
95                         
96                         input = null;
97                         
98                         expected = String.Empty;
99                         
100                         ccu = StronglyTypedResourceBuilder.Create (testResources,
101                                                                 "TestClass",
102                                                                 input,
103                                                                 "TestResourceNameSpace",
104                                                                 provider,
105                                                                 true,
106                                                                 out unmatchables);
107                         
108                         Assert.AreEqual (expected,ccu.Namespaces [0].Name);
109                 }
110                 
111                 [Test]
112                 public void GeneratedCodeNamespaceProviderInvalidIdentifiersOK ()
113                 {
114                         // identifiers which are still invalid after CreateValidIdentifier called allowed through in .NET framework
115                         string [] unmatchables;
116                         string input, output, expected;
117                         CodeCompileUnit ccu;
118                         
119                         input = "te$st";
120                         expected = "te$st";
121                         
122                         ccu = StronglyTypedResourceBuilder.Create (testResources,
123                                                                 "TestClass",
124                                                                 input,
125                                                                 "TestResourcesNameSpace",
126                                                                 provider,
127                                                                 true,
128                                                                 out unmatchables);
129                         
130                         output = ccu.Namespaces [0].Name;
131                         
132                         Assert.AreEqual (expected,output);
133                 }
134                 
135                 [Test]
136                 public void GeneratedCodeNamespaceProviderKeywords ()
137                 {
138                         string expected;
139                         string [] unmatchables;
140                         CodeCompileUnit ccu;
141
142                         foreach (string input in keywords) {
143                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
144                                                                         "TestClass",
145                                                                         input,
146                                                                         "TestResourcesNameSpace",
147                                                                         provider,
148                                                                         true,
149                                                                         out unmatchables);
150                                 
151                                 expected = provider.CreateValidIdentifier (input);
152                                 
153                                 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
154                         }
155                 }
156                 
157                 [Test]
158                 public void GeneratedCodeNamespaceProviderKeywordsMultipart ()
159                 {
160                         // .NET framework does not check individiual elements of multipart namespace
161                         string expected, input;
162                         string [] unmatchables;
163                         CodeCompileUnit ccu;
164                         
165                         foreach (string word in keywords) {
166                                 
167                                 input = "Primary." + word;
168                                 
169                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
170                                                                         "TestClass",
171                                                                         input,
172                                                                         "TestResourcesNameSpace",
173                                                                         provider,
174                                                                         true,
175                                                                         out unmatchables);
176                                 
177                                 expected = provider.CreateValidIdentifier (input);
178                                 
179                                 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
180                         }
181                 }
182                 
183                 [Test]
184                 public void GeneratedCodeNamespaceSpecialChars ()
185                 {
186                         // invalid chars replaced with _ noting (. and :) are allowed by .NET framework
187                         string [] unmatchables;
188                         string input, output, expected;
189                         
190                         CodeCompileUnit ccu;
191                         
192                         foreach (char c in specialChars) {
193                                 input = "test" + c.ToString ();
194                                 
195                                 if (c == '.' || c == ':')
196                                         expected = input;
197                                 else
198                                         expected = StronglyTypedResourceBuilder.VerifyResourceName(input, provider);
199                         
200                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
201                                                                         "TestClass",
202                                                                         input,
203                                                                         "TestResourcesNameSpace",
204                                                                         provider,
205                                                                         true,
206                                                                         out unmatchables);
207                                 
208                                 output = ccu.Namespaces [0].Name;
209                                 
210                                 Assert.AreEqual (expected,output);
211                         }
212                 }
213                 
214                 [Test]
215                 public void ResourcesNamespaceEmpty ()
216                 {
217                         // when ResourcesNamespace is String.Empty no namespace is used
218                         string [] unmatchables;
219                         string input, output, expected;
220                         CodeCompileUnit ccu;
221                         CodeMemberProperty resourceManager;
222                         CodeVariableDeclarationStatement cvds;
223                         
224                         input = String.Empty;
225                         
226                         expected = "TestClass";
227                         
228                         ccu = StronglyTypedResourceBuilder.Create (testResources,
229                                                                 "TestClass",
230                                                                 "TestNameSpace",
231                                                                 input,
232                                                                 provider,
233                                                                 true,
234                                                                 out unmatchables);
235                         
236                         resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
237                                                                                                         "ResourceManager", ccu);
238                         cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
239                         output  = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
240                         
241                         Assert.AreEqual (expected,output);
242                 }
243                 
244                 [Test]
245                 public void ResourcesNamespaceNull ()
246                 {
247                         // when ResourcesNamespace is null generatedCodeNamespace is used in its place
248                         string[] unmatchables;
249                         string input, output, expected;
250                         CodeCompileUnit ccu;
251                         CodeMemberProperty resourceManager;
252                         CodeVariableDeclarationStatement cvds;
253                         
254                         input = null;
255                         
256                         expected = "TestNameSpace.TestClass";
257                         
258                         ccu = StronglyTypedResourceBuilder.Create (testResources,
259                                                                 "TestClass",
260                                                                 "TestNameSpace",
261                                                                 input,
262                                                                 provider,
263                                                                 true,
264                                                                 out unmatchables);
265                         
266                         resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
267                                                                                                         "ResourceManager", ccu);
268                         cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
269                         output  = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
270                         
271                         Assert.AreEqual (expected,output);
272                 }
273
274                 [Test]
275                 public void ResourcesNamespaceProviderKeywords ()
276                 {
277                         // not validated against provider keywords in .net framework
278                         string output,expected;
279                         string [] unmatchables;
280                         CodeCompileUnit ccu;
281                         CodeMemberProperty resourceManager;
282                         CodeVariableDeclarationStatement cvds;
283                         
284                         foreach (string input in keywords) {
285                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
286                                                                         "TestClass",
287                                                                         "TestNamespace",
288                                                                         input,
289                                                                         provider,
290                                                                         true,
291                                                                         out unmatchables);
292
293                                 expected = input + ".TestClass";
294                                 resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
295                                                                                                         "ResourceManager", ccu);
296                                 cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
297                                 output  = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
298                                 
299                                 Assert.AreEqual (expected,output);
300                         }
301                 }
302                 
303                 [Test]
304                 public void ResourcesNamespaceSpecialChars ()
305                 {
306                         // ResourcesNamespace doesnt seem to be validated at all in .NET framework
307                         string [] unmatchables;
308                         string input, output, expected;
309                         CodeCompileUnit ccu;
310                         CodeMemberProperty resourceManager;
311                         CodeVariableDeclarationStatement cvds;
312                         
313                         foreach (char c in specialChars) {
314                                 input = "test" + c.ToString ();
315                                 
316                                 expected = input + ".TestClass";
317                                 
318                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
319                                                                         "TestClass",
320                                                                         "TestNameSpace",
321                                                                         input,
322                                                                         provider,
323                                                                         true,
324                                                                         out unmatchables);
325                                 
326                                 resourceManager = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
327                                                                                                         "ResourceManager", ccu);
328                                 cvds = ((CodeVariableDeclarationStatement)((CodeConditionStatement)resourceManager.GetStatements [0]).TrueStatements [0]);
329                                 output  = ((CodePrimitiveExpression)((CodeObjectCreateExpression)cvds.InitExpression).Parameters [0]).Value.ToString ();
330                                 
331                                 Assert.AreEqual (expected,output);
332                         }
333                 }
334         }
335 }
336