Fix XamlMember comparison and get relevant tests working.
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml / XamlMemberTest.cs
1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 using System;
24 using System.Collections;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Linq;
28 using System.Reflection;
29 using System.Text;
30 using System.Windows.Markup;
31 using System.Xaml;
32 using System.Xaml.Schema;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Xaml
36 {
37         [TestFixture]
38         // FIXME: uncomment TypeConverter tests
39         public class XamlMemberTest
40         {
41                 XamlSchemaContext sctx = new XamlSchemaContext (new XamlSchemaContextSettings ());
42                 EventInfo ass_load = typeof (AppDomain).GetEvent ("AssemblyLoad");
43                 PropertyInfo str_len = typeof (string).GetProperty ("Length");
44                 PropertyInfo sb_len = typeof (StringBuilder).GetProperty ("Length");
45                 MethodInfo dummy_add = typeof (XamlMemberTest).GetMethod ("DummyAddMethod");
46                 MethodInfo dummy_get = typeof (XamlMemberTest).GetMethod ("DummyGetMethod");
47                 MethodInfo dummy_set = typeof (XamlMemberTest).GetMethod ("DummySetMethod");
48                 MethodInfo dummy_set2 = typeof (Dummy).GetMethod ("DummySetMethod");
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void ConstructorEventInfoNullEventInfo ()
53                 {
54                         new XamlMember ((EventInfo) null, sctx);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentNullException))]
59                 public void ConstructorEventInfoNullSchemaContext ()
60                 {
61                         new XamlMember (ass_load, null);
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentNullException))]
66                 public void ConstructorPropertyInfoNullPropertyInfo ()
67                 {
68                         new XamlMember ((PropertyInfo) null, sctx);
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentNullException))]
73                 public void ConstructorPropertyInfoNullSchemaContext ()
74                 {
75                         new XamlMember (str_len, null);
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (ArgumentNullException))]
80                 public void ConstructorAddMethodNullName ()
81                 {
82                         new XamlMember (null, GetType ().GetMethod ("DummyAddMEthod"), sctx);
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (ArgumentNullException))]
87                 public void ConstructorAddMethodNullMethod ()
88                 {
89                         new XamlMember ("DummyAddMethod", null, sctx);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentNullException))]
94                 public void ConstructorAddMethodNullSchemaContext ()
95                 {
96                         new XamlMember ("DummyAddMethod", dummy_add, null);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentNullException))]
101                 public void ConstructorGetSetMethodNullName ()
102                 {
103                         new XamlMember (null, dummy_get, dummy_set, sctx);
104                 }
105
106                 [Test]
107                 public void ConstructorGetSetMethodNullGetMethod ()
108                 {
109                         new XamlMember ("DummyProp", null, dummy_set, sctx);
110                 }
111
112                 [Test]
113                 public void ConstructorGetSetMethodNullSetMethod ()
114                 {
115                         new XamlMember ("DummyProp", dummy_get, null, sctx);
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (ArgumentNullException))]
120                 public void ConstructorGetSetMethodNullGetSetMethod ()
121                 {
122                         new XamlMember ("DummyProp", null, null, sctx);
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentNullException))]
127                 public void ConstructorGetSetMethodNullSchemaContext ()
128                 {
129                         new XamlMember ("DummyProp", dummy_get, dummy_set, null);
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentNullException))]
134                 public void ConstructorNameTypeNullName ()
135                 {
136                         new XamlMember (null, new XamlType (typeof (string), sctx), false);
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentNullException))]
141                 public void ConstructorNameTypeNullType ()
142                 {
143                         new XamlMember ("Length", null, false);
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentException))]
148                 public void AddMethodInvalid ()
149                 {
150                         // It is not of expected kind of member here:
151                         // "Attached property setter and attached event adder methods must have two parameters."
152                         new XamlMember ("AssemblyLoad", ass_load.GetAddMethod (), sctx);
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentException))]
157                 public void GetMethodInvlaid ()
158                 {
159                         // It is not of expected kind of member here:
160                         // "Attached property getter methods must have one parameter and a non-void return type."
161                         new XamlMember ("Length", sb_len.GetGetMethod (), null, sctx);
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (ArgumentException))]
166                 public void SetMethodInvalid ()
167                 {
168                         // It is not of expected kind of member here:
169                         // "Attached property setter and attached event adder methods must have two parameters."
170                         new XamlMember ("Length", null, sb_len.GetSetMethod (), sctx);
171                 }
172
173                 [Test]
174                 public void MethodsFromDifferentType ()
175                 {
176                         // allowed...
177                         var i = new XamlMember ("Length", dummy_get, dummy_set2, sctx);
178                         Assert.IsNotNull (i.DeclaringType, "#1");
179                         // hmm...
180                         Assert.AreEqual (GetType (), i.DeclaringType.UnderlyingType, "#2");
181                 }
182
183                 // default values.
184
185                 [Test]
186                 public void EventInfoDefaultValues ()
187                 {
188                         var m = new XamlMember (typeof (AppDomain).GetEvent ("AssemblyLoad"), sctx);
189
190                         Assert.IsNotNull (m.DeclaringType, "#2");
191                         Assert.AreEqual (typeof (AppDomain), m.DeclaringType.UnderlyingType, "#2-2");
192                         Assert.IsNotNull (m.Invoker, "#3");
193                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
194                         Assert.AreEqual (ass_load.GetAddMethod (), m.Invoker.UnderlyingSetter, "#3-3");
195                         Assert.IsFalse (m.IsUnknown, "#4");
196                         Assert.IsFalse (m.IsReadPublic, "#5");
197                         Assert.IsTrue (m.IsWritePublic, "#6");
198                         Assert.AreEqual ("AssemblyLoad", m.Name, "#7");
199                         Assert.IsTrue (m.IsNameValid, "#8");
200                         Assert.AreEqual ("clr-namespace:System;assembly=mscorlib", m.PreferredXamlNamespace, "#9");
201                         Assert.AreEqual (new XamlType (typeof (AppDomain), sctx), m.TargetType, "#10");
202                         Assert.IsNotNull (m.Type, "#11");
203                         Assert.AreEqual (typeof (AssemblyLoadEventHandler), m.Type.UnderlyingType, "#11-2");
204 //                      Assert.IsNotNull (m.TypeConverter, "#12"); // EventConverter
205                         Assert.IsNull (m.ValueSerializer, "#13");
206                         Assert.IsNull (m.DeferringLoader, "#14");
207                         Assert.AreEqual (ass_load, m.UnderlyingMember, "#15");
208                         Assert.IsFalse (m.IsReadOnly, "#16");
209                         Assert.IsTrue (m.IsWriteOnly, "#17");
210                         Assert.IsFalse (m.IsAttachable, "#18");
211                         Assert.IsTrue (m.IsEvent, "#19");
212                         Assert.IsFalse (m.IsDirective, "#20");
213                         Assert.IsNotNull (m.DependsOn, "#21");
214                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
215                         Assert.IsFalse (m.IsAmbient, "#22");
216                 }
217
218                 [Test]
219                 public void PropertyInfoDefaultValues ()
220                 {
221                         var m = new XamlMember (typeof (string).GetProperty ("Length"), sctx);
222
223                         Assert.IsNotNull (m.DeclaringType, "#2");
224                         Assert.AreEqual (typeof (string), m.DeclaringType.UnderlyingType, "#2-2");
225                         Assert.IsNotNull (m.Invoker, "#3");
226                         Assert.AreEqual (str_len.GetGetMethod (), m.Invoker.UnderlyingGetter, "#3-2");
227                         Assert.AreEqual (str_len.GetSetMethod (), m.Invoker.UnderlyingSetter, "#3-3");
228                         Assert.IsFalse (m.IsUnknown, "#4");
229                         Assert.IsTrue (m.IsReadPublic, "#5");
230                         Assert.IsFalse (m.IsWritePublic, "#6");
231                         Assert.AreEqual ("Length", m.Name, "#7");
232                         Assert.IsTrue (m.IsNameValid, "#8");
233                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, m.PreferredXamlNamespace, "#9");
234                         Assert.AreEqual (new XamlType (typeof (string), sctx), m.TargetType, "#10");
235                         Assert.IsNotNull (m.Type, "#11");
236                         Assert.AreEqual (typeof (int), m.Type.UnderlyingType, "#11-2");
237                         Assert.IsNotNull (m.TypeConverter, "#12");
238                         Assert.IsNull (m.ValueSerializer, "#13");
239                         Assert.IsNull (m.DeferringLoader, "#14");
240                         Assert.AreEqual (str_len, m.UnderlyingMember, "#15");
241                         Assert.IsTrue (m.IsReadOnly, "#16");
242                         Assert.IsFalse (m.IsWriteOnly, "#17");
243                         Assert.IsFalse (m.IsAttachable, "#18");
244                         Assert.IsFalse (m.IsEvent, "#19");
245                         Assert.IsFalse (m.IsDirective, "#20");
246                         Assert.IsNotNull (m.DependsOn, "#21");
247                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
248                         Assert.IsFalse (m.IsAmbient, "#22");
249                 }
250
251                 public void DummyAddMethod (object o, AssemblyLoadEventHandler h)
252                 {
253                 }
254
255                 public int DummyGetMethod (object o)
256                 {
257                         return 5;
258                 }
259
260                 public void DummySetMethod (object o, int v)
261                 {
262                 }
263
264                 public class Dummy
265                 {
266                         public int DummyGetMethod (object o)
267                         {
268                                 return 5;
269                         }
270
271                         public void DummySetMethod (object o, int v)
272                         {
273                         }
274                 }
275
276                 [Test]
277                 public void AddMethodDefaultValues ()
278                 {
279                         var m = new XamlMember ("DummyAddMethod", dummy_add, sctx);
280
281                         Assert.IsNotNull (m.DeclaringType, "#2");
282                         Assert.AreEqual (GetType (), m.DeclaringType.UnderlyingType, "#2-2");
283                         Assert.IsNotNull (m.Invoker, "#3");
284                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
285                         Assert.AreEqual (dummy_add, m.Invoker.UnderlyingSetter, "#3-3");
286                         Assert.IsFalse (m.IsUnknown, "#4");
287                         Assert.IsFalse (m.IsReadPublic, "#5");
288                         Assert.IsTrue (m.IsWritePublic, "#6");
289                         Assert.AreEqual ("DummyAddMethod", m.Name, "#7");
290                         Assert.IsTrue (m.IsNameValid, "#8");
291                         var ns = "clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name;
292                         Assert.AreEqual (ns, m.PreferredXamlNamespace, "#9");
293                         // since it is unknown.
294                         Assert.AreEqual (new XamlType (typeof (object), sctx), m.TargetType, "#10");
295                         Assert.IsNotNull (m.Type, "#11");
296                         Assert.AreEqual (typeof (AssemblyLoadEventHandler), m.Type.UnderlyingType, "#11-2");
297 //                      Assert.IsNotNull (m.TypeConverter, "#12");
298                         Assert.IsNull (m.ValueSerializer, "#13");
299                         Assert.IsNull (m.DeferringLoader, "#14");
300                         Assert.AreEqual (dummy_add, m.UnderlyingMember, "#15");
301                         Assert.IsFalse (m.IsReadOnly, "#16");
302                         Assert.IsTrue (m.IsWriteOnly, "#17");
303                         Assert.IsTrue (m.IsAttachable, "#18");
304                         Assert.IsTrue (m.IsEvent, "#19");
305                         Assert.IsFalse (m.IsDirective, "#20");
306                         Assert.IsNotNull (m.DependsOn, "#21");
307                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
308                         Assert.IsFalse (m.IsAmbient, "#22");
309                 }
310
311                 [Test]
312                 public void GetSetMethodDefaultValues ()
313                 {
314                         var m = new XamlMember ("DummyProp", dummy_get, dummy_set, sctx);
315
316                         Assert.IsNotNull (m.DeclaringType, "#2");
317                         Assert.AreEqual (GetType (), m.DeclaringType.UnderlyingType, "#2-2");
318                         Assert.IsNotNull (m.Invoker, "#3");
319                         Assert.AreEqual (dummy_get, m.Invoker.UnderlyingGetter, "#3-2");
320                         Assert.AreEqual (dummy_set, m.Invoker.UnderlyingSetter, "#3-3");
321                         Assert.IsFalse (m.IsUnknown, "#4");
322                         Assert.IsTrue (m.IsReadPublic, "#5");
323                         Assert.IsTrue (m.IsWritePublic, "#6");
324                         Assert.AreEqual ("DummyProp", m.Name, "#7");
325                         Assert.IsTrue (m.IsNameValid, "#8");
326                         var ns = "clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name;
327                         Assert.AreEqual (ns, m.PreferredXamlNamespace, "#9");
328                         // since it is unknown.
329                         Assert.AreEqual (new XamlType (typeof (object), sctx), m.TargetType, "#10");
330                         Assert.IsNotNull (m.Type, "#11");
331                         Assert.AreEqual (typeof (int), m.Type.UnderlyingType, "#11-2");
332 //                      Assert.IsNotNull (m.TypeConverter, "#12");
333                         Assert.IsNull (m.ValueSerializer, "#13");
334                         Assert.IsNull (m.DeferringLoader, "#14");
335                         Assert.AreEqual (dummy_get, m.UnderlyingMember, "#15");
336                         Assert.IsFalse (m.IsReadOnly, "#16");
337                         Assert.IsFalse (m.IsWriteOnly, "#17");
338                         Assert.IsTrue (m.IsAttachable, "#18");
339                         Assert.IsFalse (m.IsEvent, "#19");
340                         Assert.IsFalse (m.IsDirective, "#20");
341                         Assert.IsNotNull (m.DependsOn, "#21");
342                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
343                         Assert.IsFalse (m.IsAmbient, "#22");
344                 }
345
346                 [Test]
347                 public void NameTypeDefaultValues ()
348                 {
349                         var m = new XamlMember ("Length", new XamlType (typeof (string), sctx), false);
350
351                         Assert.IsNotNull (m.DeclaringType, "#2");
352                         Assert.AreEqual (typeof (string), m.DeclaringType.UnderlyingType, "#2-2");
353                         Assert.IsNotNull (m.Invoker, "#3");
354                         Assert.IsNull (m.Invoker.UnderlyingGetter, "#3-2");
355                         Assert.IsNull (m.Invoker.UnderlyingSetter, "#3-3");
356                         Assert.IsTrue (m.IsUnknown, "#4");
357                         Assert.IsTrue (m.IsReadPublic, "#5");
358                         Assert.IsTrue (m.IsWritePublic, "#6");
359                         Assert.AreEqual ("Length", m.Name, "#7");
360                         Assert.IsTrue (m.IsNameValid, "#8");
361                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, m.PreferredXamlNamespace, "#9");
362                         Assert.AreEqual (new XamlType (typeof (string), sctx), m.TargetType, "#10");
363                         Assert.IsNotNull (m.Type, "#11");
364                         Assert.AreEqual (typeof (object), m.Type.UnderlyingType, "#11-2");
365                         Assert.IsNull (m.TypeConverter, "#12");
366                         Assert.IsNull (m.ValueSerializer, "#13");
367                         Assert.IsNull (m.DeferringLoader, "#14");
368                         Assert.IsNull (m.UnderlyingMember, "#15");
369                         Assert.IsFalse (m.IsReadOnly, "#16");
370                         Assert.IsFalse (m.IsWriteOnly, "#17");
371                         Assert.IsFalse (m.IsAttachable, "#18");
372                         Assert.IsFalse (m.IsEvent, "#19");
373                         Assert.IsFalse (m.IsDirective, "#20");
374                         Assert.IsNotNull (m.DependsOn, "#21");
375                         Assert.AreEqual (0, m.DependsOn.Count, "#21-2");
376                         Assert.IsFalse (m.IsAmbient, "#22");
377                 }
378
379                 [Test]
380                 public void UnderlyingMember ()
381                 {
382                         Assert.IsTrue (new XamlMember (ass_load, sctx).UnderlyingMember is EventInfo, "#1");
383                         Assert.IsTrue (new XamlMember (str_len, sctx).UnderlyingMember is PropertyInfo, "#2");
384                         Assert.AreEqual (dummy_get, new XamlMember ("DummyProp", dummy_get, dummy_set, sctx).UnderlyingMember, "#3");
385                         Assert.AreEqual (dummy_add, new XamlMember ("DummyAddMethod", dummy_add, sctx).UnderlyingMember, "#4");
386                         Assert.IsNull (new XamlMember ("Length", new XamlType (typeof (string), sctx), false).UnderlyingMember, "#5");
387                 }
388
389                 [Test]
390                 public void EqualsTest ()
391                 {
392                         XamlMember m;
393                         var xt = XamlLanguage.Type;
394                         m = new XamlMember ("Type", xt, false);
395                         var type_type = xt.GetMember ("Type");
396                         Assert.AreNotEqual (m, xt.GetMember ("Type"), "#1"); // whoa!
397                         Assert.AreNotEqual (type_type, m, "#2"); // whoa!
398                         Assert.AreEqual (type_type, xt.GetMember ("Type"), "#3");
399                         Assert.AreEqual (type_type.ToString (), m.ToString (), "#4");
400
401                         Assert.AreEqual (xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), "#5");
402                         Assert.AreEqual (xt.GetAllMembers ().FirstOrDefault (mm => mm.Name == "Type"), xt.GetMember ("Type"), "#6");
403
404                         // different XamlSchemaContext
405                         Assert.AreNotEqual (m, XamlLanguage.Type.GetMember ("Type"), "#7");
406                         Assert.AreNotEqual (XamlLanguage.Type.GetMember ("Type"), new XamlSchemaContext ().GetXamlType (typeof (Type)).GetMember ("Type"), "#7");
407                         Assert.AreEqual (XamlLanguage.Type.GetMember ("Type"), new XamlSchemaContext ().GetXamlType (typeof (TypeExtension)).GetMember ("Type"), "#8");
408                 }
409
410                 [Test]
411                 public void ToStringTest ()
412                 {
413                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}_Initialization", XamlLanguage.Initialization.ToString (), "#1");
414
415                         // Wow. Uncomment this, and it will show .NET returns the XamlMember.ToString() results *inconsistently*.
416                         //Assert.AreEqual ("System.Windows.Markup.XData", XamlLanguage.XData.ToString (), "#2pre");
417                         Assert.AreEqual (XamlLanguage.Xaml2006Namespace, XamlLanguage.XData.PreferredXamlNamespace, "#2pre2");
418
419                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}XData.Text", XamlLanguage.XData.GetMember ("Text").ToString (), "#2");
420
421                         var pi = typeof (string).GetProperty ("Length");
422                         Assert.AreEqual ("{http://schemas.microsoft.com/winfx/2006/xaml}String.Length", new XamlMember (pi, sctx).ToString (), "#3");
423
424                         Assert.AreEqual ("System.Xaml.XamlSchemaContext.FooBar", new XamlMember ("FooBar", typeof (XamlSchemaContext).GetMethod ("GetPreferredPrefix"), null, sctx).ToString (), "#4");
425
426                         Assert.AreEqual ("{urn:foo}bar", new XamlDirective ("urn:foo", "bar").ToString (), "#5");
427                 }
428         }
429 }