Merge pull request #228 from QuickJack/3e163743eda89cc8c239779a75dd245be12aee3c
[mono.git] / mcs / class / System.Xaml / Test / System.Xaml.Schema / XamlValueConverterTest.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.Generic;
25 using System.ComponentModel;
26 using System.Reflection;
27 using System.Text;
28 using System.Xaml;
29 using System.Xaml.Schema;
30 using System.Xml;
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Xaml.Schema
34 {
35         [TestFixture]
36         public class XamlValueConverterTest
37         {
38                 [Test]
39                 [ExpectedException (typeof (ArgumentException))]
40                 public void ConstructorNullConverterTypeTargetType ()
41                 {
42                         // either of them must be non-null.
43                         new XamlValueConverter<TypeConverter> (null, null);
44                 }
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentException))]
48                 public void ConstructorNullConverterTypeTargetTypeNull ()
49                 {
50                         // either of them must be non-null.
51                         new XamlValueConverter<TypeConverter> (null, null, null);
52                 }
53
54                 [Test]
55                 public void ConstructorOnlyWithName ()
56                 {
57                         // ok
58                         new XamlValueConverter<TypeConverter> (null, null, "Foo");
59                 }
60
61                 [Test]
62                 public void ConstructorNullConverterType ()
63                 {
64                         // ok
65                         var c = new XamlValueConverter<TypeConverter> (null, XamlLanguage.Int32);
66                         Assert.IsNull (c.ConverterInstance, "#1");
67                 }
68
69                 [Test]
70                 public void ConstructorNullTargetType ()
71                 {
72                         // ok
73                         var c = new XamlValueConverter<TypeConverter> (typeof (Int32Converter), null);
74                         Assert.IsTrue (c.ConverterInstance is Int32Converter, "#1");
75                 }
76
77                 [Test]
78                 public void ConstructorNullName ()
79                 {
80                         // ok
81                         new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32, null);
82                 }
83
84                 [Test]
85                 public void ConverterTargetMismatch ()
86                 {
87                         // ok
88                         var c = new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.String, null);
89                         Assert.IsTrue (c.ConverterInstance is Int32Converter, "#1");
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (XamlSchemaException))]
94                 public void InconsistentConverterType ()
95                 {
96                         var c = new XamlValueConverter<TypeConverter> (typeof (int), XamlLanguage.String, null);
97                         Assert.IsNull (c.ConverterInstance, "#1");
98                 }
99
100                 [Test]
101                 public void ObjectType ()
102                 {
103                         // This test asserts that XamlLanguage.Object.TypeConverter.ConverterType is null for different reason.
104                         var c = new XamlValueConverter<TypeConverter> (typeof (TypeConverter), XamlLanguage.Object, null);
105                         Assert.IsNotNull (c.ConverterInstance, "#1");
106                         Assert.IsNull (XamlLanguage.Object.TypeConverter.ConverterInstance, "#2");
107                 }
108
109                 [Test]
110                 public void Equality ()
111                 {
112                         // ok
113                         var c1 = new XamlValueConverter<TypeConverter> (null, XamlLanguage.Int32);
114                         var c2 = new XamlValueConverter<TypeConverter> (null, XamlLanguage.Int32);
115                         var c3 = new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32);
116                         var c4 = new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32, null);
117                         var c5 = new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32, "Foo");
118                         Assert.IsTrue (c1 == c2, "#1");
119                         Assert.IsFalse (c1 == c3, "#2");
120                         Assert.IsTrue (c3 == c4, "#3");
121                         Assert.IsFalse (c4 == c5, "#4");
122                 }
123                 
124                 [Test]
125                 public void TestToString ()
126                 {
127                         Assert.AreEqual ("Int32Converter(Int32)", new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32).ToString (), "#1");
128                         Assert.AreEqual ("Foo", new XamlValueConverter<TypeConverter> (typeof (Int32Converter), XamlLanguage.Int32, "Foo").ToString (), "#2");
129                         Assert.AreEqual ("Int32Converter", new XamlValueConverter<TypeConverter> (typeof (Int32Converter), null).ToString (), "#1");
130                         Assert.AreEqual ("Int32", new XamlValueConverter<TypeConverter> (null, XamlLanguage.Int32).ToString (), "#3"); // huh, really? no difference from ConverterType?
131                 }
132         }
133 }