Merge pull request #835 from HorstKakuschke/master
[mono.git] / mcs / class / System / Test / System.ComponentModel / NullableConverterTest.cs
1 //
2 // MonoTests.System.ComponentModel.NullableConverterTest
3 //
4 // Author:
5 //      Ivan N. Zlatev  <contact@i-nz.net>
6 //
7 // Copyright (C) 2008 Ivan N. Zlatev
8 //
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
31 #if NET_2_0
32
33 using System;
34 using System.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
37
38 using NUnit.Framework;
39
40 namespace MonoTests.System.ComponentModel
41 {
42         [TestFixture]
43         public class NullableConverterTest
44         {
45                 [TypeConverter (typeof(MyTypeConverter))]
46                 private struct MyType
47                 {
48                         int value;
49                 }
50
51                 private class MyTypeConverter : TypeConverter
52                 {
53                         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
54                         {
55                                 return true;
56                         }
57
58                         public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
59                         {
60                                 return new TypeConverter.StandardValuesCollection (new object[] {});
61                         }
62                 }
63
64                 [Test]
65                 public void PropertyValues ()
66                 {
67                         NullableConverter converter = new NullableConverter (typeof(MyType?));
68                         Assert.AreEqual (typeof(MyType?), converter.NullableType, "#1");
69                         Assert.AreEqual (typeof(MyType), converter.UnderlyingType, "#2");
70                         Assert.AreEqual (typeof(MyTypeConverter), converter.UnderlyingTypeConverter.GetType(), "#2");
71                 }
72
73                 [Test]
74                 public void CanConvertFrom ()
75                 {
76                         NullableConverter converter = new NullableConverter (typeof(MyType?));
77                         Assert.IsTrue (converter.CanConvertFrom (null, typeof(MyType)), "#1");
78                         Assert.IsFalse (converter.CanConvertFrom (null, typeof(object)), "#2");
79                 }
80
81                 [Test]
82                 public void CanConvertTo ()
83                 {
84                         NullableConverter converter = new NullableConverter (typeof(MyType?));
85                         Assert.IsTrue (converter.CanConvertTo (null, typeof(MyType)), "#1");
86                         Assert.IsFalse (converter.CanConvertTo (null, typeof(object)), "#2");
87                 }
88
89                 [Test]
90                 public void ConvertFrom_EmptyString ()
91                 {
92                         NullableConverter converter = new NullableConverter (typeof(MyType?));
93                         Assert.IsNull (converter.ConvertFrom (null, null, String.Empty), "#1");
94                 }
95
96                 [Test]
97                 public void GetStandardValues ()
98                 {
99                         NullableConverter converter = new NullableConverter (typeof(MyType?));
100                         Assert.IsTrue (converter.GetStandardValuesSupported (null), "#1");
101                         TypeConverter.StandardValuesCollection values = converter.GetStandardValues (null);
102                         // MyTypeConverter returns an empty values collection, but
103                         // NullableConverter adds a null value to the set.
104                         Assert.AreEqual (1, values.Count, "#2");
105                         Assert.AreEqual (null, values[0], "#3");
106                 }
107         }
108 }
109 #endif