2 // StronglyTypedResourceBuilderNamespaceTests.cs - tests the
3 // generatedCodeNamespace and resourcesNamespace params of the main
7 // Gary Barnett (gary.barnett.mono@gmail.com)
9 // Copyright (C) Gary Barnett (2012)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
34 using System.Resources.Tools;
36 using Microsoft.CSharp;
37 using System.Collections.Generic;
39 namespace MonoTests.System.Resources.Tools {
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;
61 testResources = new Dictionary<string, object> ();
62 testResources.Add ("akey", String.Empty);
66 public void GeneratedCodeNamespaceEmpty ()
68 // empty namespace allowed
69 string [] unmatchables;
70 string input, expected;
75 expected = String.Empty;
77 ccu = StronglyTypedResourceBuilder.Create (testResources,
80 "TestResourceNameSpace",
85 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
89 public void GeneratedCodeNamespaceNull ()
91 // null should be replaced with String.Empty
92 string [] unmatchables;
93 string input, expected;
98 expected = String.Empty;
100 ccu = StronglyTypedResourceBuilder.Create (testResources,
103 "TestResourceNameSpace",
108 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
112 public void GeneratedCodeNamespaceProviderInvalidIdentifiersOK ()
114 // identifiers which are still invalid after CreateValidIdentifier called allowed through in .NET framework
115 string [] unmatchables;
116 string input, output, expected;
122 ccu = StronglyTypedResourceBuilder.Create (testResources,
125 "TestResourcesNameSpace",
130 output = ccu.Namespaces [0].Name;
132 Assert.AreEqual (expected,output);
136 public void GeneratedCodeNamespaceProviderKeywords ()
139 string [] unmatchables;
142 foreach (string input in keywords) {
143 ccu = StronglyTypedResourceBuilder.Create (testResources,
146 "TestResourcesNameSpace",
151 expected = provider.CreateValidIdentifier (input);
153 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
158 public void GeneratedCodeNamespaceProviderKeywordsMultipart ()
160 // .NET framework does not check individiual elements of multipart namespace
161 string expected, input;
162 string [] unmatchables;
165 foreach (string word in keywords) {
167 input = "Primary." + word;
169 ccu = StronglyTypedResourceBuilder.Create (testResources,
172 "TestResourcesNameSpace",
177 expected = provider.CreateValidIdentifier (input);
179 Assert.AreEqual (expected,ccu.Namespaces [0].Name);
184 public void GeneratedCodeNamespaceSpecialChars ()
186 // invalid chars replaced with _ noting (. and :) are allowed by .NET framework
187 string [] unmatchables;
188 string input, output, expected;
192 foreach (char c in specialChars) {
193 input = "test" + c.ToString ();
195 if (c == '.' || c == ':')
198 expected = StronglyTypedResourceBuilder.VerifyResourceName(input, provider);
200 ccu = StronglyTypedResourceBuilder.Create (testResources,
203 "TestResourcesNameSpace",
208 output = ccu.Namespaces [0].Name;
210 Assert.AreEqual (expected,output);
215 public void ResourcesNamespaceEmpty ()
217 // when ResourcesNamespace is String.Empty no namespace is used
218 string [] unmatchables;
219 string input, output, expected;
221 CodeMemberProperty resourceManager;
222 CodeVariableDeclarationStatement cvds;
224 input = String.Empty;
226 expected = "TestClass";
228 ccu = StronglyTypedResourceBuilder.Create (testResources,
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 ();
241 Assert.AreEqual (expected,output);
245 public void ResourcesNamespaceNull ()
247 // when ResourcesNamespace is null generatedCodeNamespace is used in its place
248 string[] unmatchables;
249 string input, output, expected;
251 CodeMemberProperty resourceManager;
252 CodeVariableDeclarationStatement cvds;
256 expected = "TestNameSpace.TestClass";
258 ccu = StronglyTypedResourceBuilder.Create (testResources,
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 ();
271 Assert.AreEqual (expected,output);
275 public void ResourcesNamespaceProviderKeywords ()
277 // not validated against provider keywords in .net framework
278 string output,expected;
279 string [] unmatchables;
281 CodeMemberProperty resourceManager;
282 CodeVariableDeclarationStatement cvds;
284 foreach (string input in keywords) {
285 ccu = StronglyTypedResourceBuilder.Create (testResources,
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 ();
299 Assert.AreEqual (expected,output);
304 public void ResourcesNamespaceSpecialChars ()
306 // ResourcesNamespace doesnt seem to be validated at all in .NET framework
307 string [] unmatchables;
308 string input, output, expected;
310 CodeMemberProperty resourceManager;
311 CodeVariableDeclarationStatement cvds;
313 foreach (char c in specialChars) {
314 input = "test" + c.ToString ();
316 expected = input + ".TestClass";
318 ccu = StronglyTypedResourceBuilder.Create (testResources,
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 ();
331 Assert.AreEqual (expected,output);