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.
33 using NUnit.Framework;
35 using System.Resources.Tools;
37 using Microsoft.CSharp;
38 using System.Collections.Generic;
40 namespace MonoTests.System.Resources.Tools {
42 public class StronglyTypedResourceBuilderResourceNameTests {
43 CSharpCodeProvider provider = new CSharpCodeProvider ();
45 [Test, ExpectedException (typeof (ArgumentException))]
46 public void ResourceNamesCaseSensitiveDupes ()
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;
52 testResources.Add ("FortyTwo", String.Empty);
53 testResources.Add ("fortytwo", String.Empty);
55 StronglyTypedResourceBuilder.Create (testResources,
58 "TestResourcesNameSpace",
64 [Test, ExpectedException (typeof (ArgumentException))]
65 public void ResourceNamesCaseSensitiveDupesIgnored ()
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;
71 testResources.Add (">>FortyTwo", String.Empty);
72 testResources.Add (">>fortytwo", String.Empty);
74 StronglyTypedResourceBuilder.Create (testResources,
77 "TestResourcesNameSpace",
83 [Test, ExpectedException (typeof (ArgumentException))]
84 public void ResourceNamesCaseSensitiveDupesInvalid ()
86 // passing in case insensitive dupes of invalid resourcenames still raises exception
87 Dictionary<string, object> testResources = new Dictionary<string, object> ();
88 string [] unmatchables;
90 testResources.Add ("Fo$rtyTwo", String.Empty);
91 testResources.Add ("fo$rtytwo", String.Empty);
93 StronglyTypedResourceBuilder.Create (testResources,
96 "TestResourcesNameSpace",
103 public void ResourceNamesCaseSensitiveDupesWithSpecialChars ()
105 // resourcenames with special chars that would result in case insentive dupes go to unmatchables
107 Dictionary<string, object> testResources = new Dictionary<string, object> ();
108 string [] unmatchables;
110 testResources.Add ("Fo&rtyTwo", String.Empty);
111 testResources.Add ("fo_rtytwo", String.Empty);
113 StronglyTypedResourceBuilder.Create (testResources,
116 "TestResourcesNameSpace",
121 Assert.AreEqual (2,unmatchables.Length);
125 public void ResourceNamesDuplicate_NETBUG ()
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
138 Dictionary<string, object> testResources = new Dictionary<string, object> ();
139 string [] unmatchables;
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");
151 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
154 "TestResourcesNameSpace",
159 int matchedResources = testResources.Count - unmatchables.Length;
160 int membersExpected = matchedResources + 5; // 5 standard members
162 Assert.AreEqual (membersExpected,ccu.Namespaces [0].Types [0].Members.Count);
166 public void ResourceNamesDuplicate ()
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
174 Dictionary<string, object> testResources = new Dictionary<string, object> ();
175 string [] unmatchables;
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");
183 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
186 "TestResourcesNameSpace",
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);
197 public void ResourceNamesIgnored ()
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;
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
214 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
217 "TestResourcesNameSpace",
222 Assert.AreEqual(0,unmatchables.Length);
224 Assert.AreEqual (8,ccu.Namespaces [0].Types [0].Members.Count); // 3 valid + 5 standard
225 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
227 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
229 Assert.IsNotNull (StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
234 public void ResourceNamesInvalid ()
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;
241 testResources.Add ("ResourceManager", String.Empty);
242 testResources.Add ("Culture", String.Empty);
243 testResources.Add ("t$est", String.Empty);
245 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
248 "TestResourcesNameSpace",
253 Assert.AreEqual (3,unmatchables.Length);
254 Assert.AreEqual (5,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard
258 public void ResourceNamesInvalidDifferentCase ()
260 // resources named Culture and ResourceManager go into unmatchables (case sensitive)
261 Dictionary<string, object> testResources = new Dictionary<string, object> ();
262 string [] unmatchables;
264 testResources.Add ("resourceManager", String.Empty);
265 testResources.Add ("culture", String.Empty);
266 testResources.Add ("t$est", String.Empty);
268 CodeCompileUnit ccu = StronglyTypedResourceBuilder.Create (testResources,
271 "TestResourcesNameSpace",
276 Assert.AreEqual (1,unmatchables.Length);
277 Assert.AreEqual (7,ccu.Namespaces [0].Types [0].Members.Count); // 5 standard +2
280 [Test, ExpectedException (typeof (InvalidCastException))]
281 public void ResourceNamesNotString ()
283 // throws InvalidCastException in .net framework
284 Dictionary<object, object> testResources = new Dictionary<object, object> ();
285 string [] unmatchables;
287 testResources.Add (DateTime.MinValue, "1");
289 StronglyTypedResourceBuilder.Create (testResources,
292 "TestResourcesNameSpace",