[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Design / Test / System.Resources.Tools / StronglyTypedResourceBuilderBaseNameTests.cs
1 //
2 // StronglyTypedResourceBuilderBaseNameTests.cs - tests the baseName 
3 // parameter passed to main Create overload
4 //
5 // Author:
6 //      Gary Barnett (gary.barnett.mono@gmail.com)
7 // 
8 // Copyright (C) Gary Barnett (2012)
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Resources.Tools;
34 using System.CodeDom;
35 using Microsoft.CSharp;
36 using System.Collections.Generic;
37
38 namespace MonoTests.System.Resources.Tools {
39         [TestFixture]
40         public class StronglyTypedResourceBuilderBaseNameTests  {
41                 static string [] keywords = {"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", 
42                                         "checked", "class", "const", "continue", "decimal", "default", "delegate", 
43                                         "do", "double", "else", "enum", "event", "explicit", "extern", "FALSE", 
44                                         "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", 
45                                         "implicit", "in", "int", "interface", "internal", "is", "lock", "long", 
46                                         "namespace", "new", "null", "object", "operator", "out", "override", "params", 
47                                         "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", 
48                                         "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", 
49                                         "throw", "TRUE", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", 
50                                         "ushort", "using", "virtual", "volatile", "void", "while" };
51                 static char [] specialChars = { ' ', '\u00A0', '.', ',', ';', '|', '~', '@', '#', '%', '^', '&', 
52                                         '*', '+', '-', '/', '\\', '<', '>', '?', '[', ']', '(', ')', '{', 
53                                         '}', '\"', '\'', ':', '!'};
54                 CSharpCodeProvider provider = new CSharpCodeProvider ();
55                 Dictionary<string, object> testResources;
56                 
57                 [SetUp]
58                 public void Setup ()
59                 {
60                         testResources = new Dictionary<string, object> ();
61                         testResources.Add ("akey", String.Empty);
62                 }
63                 
64                 [Test]
65                 public void BaseNameEmpty ()
66                 {
67                         // empty class name should change to _
68                         string [] unmatchables;
69                         CodeCompileUnit ccu;
70                         string input, expected;
71                         
72                         input = String.Empty;
73                         
74                         ccu = StronglyTypedResourceBuilder.Create (testResources,
75                                                                 input,
76                                                                 "TestNamespace",
77                                                                 "TestResourcesNameSpace",
78                                                                 provider,
79                                                                 true,
80                                                                 out unmatchables);
81                         
82                         expected = "_";
83                         
84                         Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name);
85                 }
86                 
87                 [Test, ExpectedException (typeof (ArgumentException))]
88                 public void BaseNameInvalidIdentifier ()
89                 {
90                         // identifier invalid after Going through provider.CreateValidIdentifier throw exception in .NET framework
91                         string [] unmatchables;
92                         string input;
93                         
94                         input = "cla$ss";
95                         
96                         StronglyTypedResourceBuilder.Create (testResources,
97                                                                 input,
98                                                                 "TestNamespace",
99                                                                 "TestResourcesNameSpace",
100                                                                 provider,
101                                                                 true,
102                                                                 out unmatchables);
103                 }
104                 
105                 [Test]
106                 public void BaseNameKeywords ()
107                 {
108                         // provider.CreateValidIdentifier used to return valid identifier
109                         string expected;
110                         string [] unmatchables;
111                         CodeCompileUnit ccu;
112                         
113                         foreach (string input in keywords) {
114                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
115                                                                         input,
116                                                                         "TestNamespace",
117                                                                         "TestResourcesNameSpace",
118                                                                         provider,
119                                                                         true,
120                                                                         out unmatchables);
121                                 
122                                 expected = provider.CreateValidIdentifier (input);
123                                 
124                                 Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name);
125                         }
126                 }
127                 
128                 [Test, ExpectedException (typeof (ArgumentNullException))]
129                 public void BaseNameNull ()
130                 {
131                         // should throw exception
132                         string [] unmatchables;
133                         string input;
134                         
135                         input = null;
136                         
137                         StronglyTypedResourceBuilder.Create (testResources,
138                                                                 input,
139                                                                 "TestNamespace",
140                                                                 "TestResourcesNameSpace",
141                                                                 provider,
142                                                                 true,
143                                                                 out unmatchables);
144                 }
145                 
146                 [Test]
147                 public void BaseNameSpecialChars ()
148                 {
149                         // StronglyTypedResourceBuilder.VerifyResourceName seems to be used
150                         string [] unmatchables;
151                         CodeCompileUnit ccu;
152                         string input, expected;
153
154                         foreach (char c in specialChars) {
155                                 input = c.ToString ();
156                                 
157                                 ccu = StronglyTypedResourceBuilder.Create (testResources,
158                                                                         input,
159                                                                         "TestNamespace",
160                                                                         "TestResourcesNameSpace",
161                                                                         provider,
162                                                                         true,
163                                                                         out unmatchables);
164                                 
165                                 expected = StronglyTypedResourceBuilder.VerifyResourceName (input, provider);
166                                 
167                                 Assert.AreEqual (expected,ccu.Namespaces [0].Types [0].Name); 
168                         }
169                 }
170         }
171 }
172