Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Design / Test / System.Resources.Tools / StronglyTypedResourceBuilderOtherTests.cs
1 //
2 // StronglyTypedResourceBuilderOtherTests.cs - tests the internalClass, 
3 // codeProvider and resourceList params of the 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 #if NET_2_0
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 using System.IO;
39 using System.Reflection;
40 using System.Drawing;
41
42 namespace MonoTests.System.Resources.Tools {
43         [TestFixture]
44         public class StronglyTypedResourceBuilderOtherTests     {
45                 CSharpCodeProvider provider = new CSharpCodeProvider ();
46                 
47                 Dictionary<string,object> GetAllResourceTypes ()
48                 {
49                         Dictionary<string, object> tempDict = new Dictionary<string, object> ();
50                         Bitmap bmp = new Bitmap (100,100);
51                         MemoryStream wav = new MemoryStream (1000);
52                         
53                         tempDict.Add ("astring", "myvalue");
54                         tempDict.Add ("bmp", bmp);
55                         tempDict.Add ("wav", wav);  
56                         
57                         wav.Close ();
58                         return tempDict;
59                 }
60                 
61                 Dictionary<string,object> GetTestResources ()
62                 {
63                         Dictionary<string, object> tempDict = new Dictionary<string, object> ();
64                         tempDict.Add ("akey", string.Empty);
65                         
66                         return tempDict;
67                 }
68                 
69                 [Test]
70                 public void InternalClassFalse ()
71                 {
72                         // check access modifiers for class, Culture, ResourceManager, string, stream and standard resource properties
73                         Dictionary<string, object> testResources = GetAllResourceTypes ();
74                         string [] unmatchables;
75                         CodeCompileUnit ccu;
76                         CodeMemberProperty cmp;
77                         
78                         bool isInternal = false;
79                         
80                         ccu = StronglyTypedResourceBuilder.Create (testResources,
81                                                                 "TestClass",
82                                                                 "TestNamespace",
83                                                                 "TestResourcesNameSpace",
84                                                                 provider,
85                                                                 isInternal,
86                                                                 out unmatchables);
87                         
88                         CodeTypeDeclaration resType = ccu.Namespaces [0].Types [0];
89                         Assert.IsTrue (resType.TypeAttributes ==  TypeAttributes.Public);
90                         
91                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
92                                                                                         "ResourceManager", ccu);
93                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
94                                                         | MemberAttributes.Final
95                                                         | MemberAttributes.FamilyAndAssembly
96                                                         | MemberAttributes.FamilyOrAssembly));
97                         
98                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("Culture", ccu);
99                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
100                                                         | MemberAttributes.Final
101                                                         | MemberAttributes.FamilyAndAssembly
102                                                         | MemberAttributes.FamilyOrAssembly));
103                         
104                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
105                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
106                                                         | MemberAttributes.Final
107                                                         | MemberAttributes.FamilyAndAssembly
108                                                         | MemberAttributes.FamilyOrAssembly));
109                         
110                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
111                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
112                                                         | MemberAttributes.Final
113                                                         | MemberAttributes.FamilyAndAssembly
114                                                         | MemberAttributes.FamilyOrAssembly));
115                         
116                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
117                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
118                                                         | MemberAttributes.Final
119                                                         | MemberAttributes.FamilyAndAssembly
120                                                         | MemberAttributes.FamilyOrAssembly));
121                 }
122                 
123                 [Test]
124                 public void InternalClassTrue ()
125                 {
126                         // check access modifiers for class, Culture, ResourceManager, string, stream and standard resource properties
127                         Dictionary<string, object> testResources = GetAllResourceTypes ();
128                         string [] unmatchables;
129                         CodeCompileUnit ccu;
130                         CodeMemberProperty cmp;
131                         
132                         bool isInternal = true;
133                         
134                         ccu = StronglyTypedResourceBuilder.Create (testResources,
135                                                                 "TestClass",
136                                                                 "TestNamespace",
137                                                                 "TestResourcesNameSpace",
138                                                                 provider,
139                                                                 isInternal,
140                                                                 out unmatchables);
141                         
142                         
143                         CodeTypeDeclaration resType = ccu.Namespaces [0].Types [0];
144                         Assert.IsTrue (resType.TypeAttributes ==  TypeAttributes.NotPublic);
145                         
146                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> (
147                                                                                         "ResourceManager", ccu);
148                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
149                                                         | MemberAttributes.Final
150                                                         | MemberAttributes.Assembly));
151                         
152                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("Culture", ccu);
153                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
154                                                         | MemberAttributes.Final
155                                                         | MemberAttributes.Assembly));
156                         
157                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
158                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
159                                                         | MemberAttributes.Final
160                                                         | MemberAttributes.Assembly));
161                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
162                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
163                                                         | MemberAttributes.Final
164                                                         | MemberAttributes.Assembly));
165                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
166                         Assert.IsTrue (cmp.Attributes == (MemberAttributes.Abstract
167                                                         | MemberAttributes.Final
168                                                         | MemberAttributes.Assembly));
169                 }
170                 
171                 [Test, ExpectedException (typeof (ArgumentNullException))]
172                 public void ProviderNull ()
173                 {
174                         // should throw exception
175                         Dictionary<string, object> testResources = GetTestResources ();
176                         string [] unmatchables;
177
178                         StronglyTypedResourceBuilder.Create (testResources,
179                                                         "TestClass",
180                                                         "TestNamespace",
181                                                         "TestResourcesNameSpace",
182                                                         null, //setting provider to null
183                                                         true,
184                                                         out unmatchables);
185                 }
186                 
187                 [Test]
188                 public void ResourceListEmpty ()
189                 {
190                         //should still create class with default members
191                         Dictionary<string, object> testResources;
192                         string [] unmatchables;
193                         CodeCompileUnit ccu;
194                         
195                         testResources = new Dictionary<string, object> ();
196                         
197                         ccu = StronglyTypedResourceBuilder.Create (testResources,
198                                                                 "TestRes",
199                                                                 "TestNamespace",
200                                                                 "TestResourcesNameSpace",
201                                                                 provider,
202                                                                 true,
203                                                                 out unmatchables);
204                         
205                         Assert.AreEqual(5,ccu.Namespaces [0].Types [0].Members.Count);
206                 }
207                 
208                 [Test, ExpectedException (typeof (ArgumentNullException))]
209                 public void ResourceListNull ()
210                 {
211                         // should through exception
212                         Dictionary<string, object> testResources;
213                         string [] unmatchables; 
214                         CSharpCodeProvider provider = new CSharpCodeProvider ();
215                         
216                         testResources = null;
217                         
218                         StronglyTypedResourceBuilder.Create (testResources,
219                                                                 "TestRes",
220                                                                 "TestNamespace",
221                                                                 "TestResourcesNameSpace",
222                                                                 provider,
223                                                                 true,
224                                                                 out unmatchables);
225                 }
226                 
227         }
228 }
229
230
231 #endif