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
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 StronglyTypedResourceBuilderResourceNameTests {
42 CSharpCodeProvider provider = new CSharpCodeProvider ();
44 [Test, ExpectedException (typeof (ArgumentException))]
45 public void ResourceNamesCaseSensitiveDupes ()
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;
51 testResources.Add ("FortyTwo", String.Empty);
52 testResources.Add ("fortytwo", String.Empty);
54 StronglyTypedResourceBuilder.Create (testResources,
57 "TestResourcesNameSpace",
63 [Test, ExpectedException (typeof (ArgumentException))]
64 public void ResourceNamesCaseSensitiveDupesIgnored ()
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;
70 testResources.Add (">>FortyTwo", String.Empty);
71 testResources.Add (">>fortytwo", String.Empty);
73 StronglyTypedResourceBuilder.Create (testResources,
76 "TestResourcesNameSpace",
82 [Test, ExpectedException (typeof (ArgumentException))]
83 public void ResourceNamesCaseSensitiveDupesInvalid ()
85 // passing in case insensitive dupes of invalid resourcenames still raises exception
86 Dictionary<string, object> testResources = new Dictionary<string, object> ();
87 string [] unmatchables;
89 testResources.Add ("Fo$rtyTwo", String.Empty);
90 testResources.Add ("fo$rtytwo", String.Empty);
92 StronglyTypedResourceBuilder.Create (testResources,
95 "TestResourcesNameSpace",
102 public void ResourceNamesCaseSensitiveDupesWithSpecialChars ()
104 // resourcenames with special chars that would result in case insentive dupes go to unmatchables
106 Dictionary<string, object> testResources = new Dictionary<string, object> ();
107 string [] unmatchables;
109 testResources.Add ("Fo&rtyTwo", String.Empty);
110 testResources.Add ("fo_rtytwo", String.Empty);
112 StronglyTypedResourceBuilder.Create (testResources,
115 "TestResourcesNameSpace",
120 Assert.AreEqual (2,unmatchables.Length);
124 public void ResourceNamesDuplicate_NETBUG ()
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
137 Dictionary<string, object> testResources = new Dictionary<string, object> ();
138 string [] unmatchables;
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");
150 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
153 "TestResourcesNameSpace",
158 int matchedResources = testResources.Count - unmatchables.Length;
159 int membersExpected = matchedResources + 5; // 5 standard members
161 Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);
165 public void ResourceNamesDuplicate ()
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
173 Dictionary<string, object> testResources = new Dictionary<string, object> ();
174 string [] unmatchables;
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");
182 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
185 "TestResourcesNameSpace",
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);
196 public void ResourceNamesIgnored ()
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;
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
213 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
216 "TestResourcesNameSpace",
221 Assert.AreEqual(0,unmatchables.Length);
223 Assert.AreEqual (8,ccu.Namespaces [0].Types [0].Members.Count); // 3 valid + 5 standard
224 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
226 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
228 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
233 public void ResourceNamesInvalid ()
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;
240 testResources.Add ("ResourceManager", String.Empty);
241 testResources.Add ("Culture", String.Empty);
242 testResources.Add ("t$est", String.Empty);
244 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
247 "TestResourcesNameSpace",
252 Assert.AreEqual (3,unmatchables.Length);
253 Assert.AreEqual (5,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard
257 public void ResourceNamesInvalidDifferentCase ()
259 // resources named Culture and ResourceManager go into unmatchables (case sensitive)
260 Dictionary<string, object> testResources = new Dictionary<string, object> ();
261 string [] unmatchables;
263 testResources.Add ("resourceManager", String.Empty);
264 testResources.Add ("culture", String.Empty);
265 testResources.Add ("t$est", String.Empty);
267 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
270 "TestResourcesNameSpace",
275 Assert.AreEqual (1,unmatchables.Length);
276 Assert.AreEqual (7,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard +2
279 [Test, ExpectedException (typeof (InvalidCastException))]
280 public void ResourceNamesNotString ()
282 // throws InvalidCastException in .net framework
283 Dictionary<object, object> testResources = new Dictionary<object, object> ();
284 string [] unmatchables;
286 testResources.Add (DateTime.MinValue, "1");
288 StronglyTypedResourceBuilder.Create (testResources,
291 "TestResourcesNameSpace",