Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StringTypeConverterTestBase.cs « Drawing « tests « System.ComponentModel.TypeConverter « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5bb03f7bb6e4a6deb79804aeaac9c0b29aada2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.ComponentModel.TypeConverterTests
{
    public abstract class StringTypeConverterTestBase<T>
    {
        protected abstract T Default { get; }
        protected abstract TypeConverter Converter { get; }
        protected abstract bool StandardValuesSupported { get; }
        protected abstract bool StandardValuesExclusive { get; }
        protected abstract bool CreateInstanceSupported { get; }
        protected abstract bool IsGetPropertiesSupported { get; }

        protected virtual IEnumerable<Tuple<T, Dictionary<string, object>>> CreateInstancePairs
        {
            get { yield break; }
        }

        [Fact]
        public void GetStandardValuesSupported()
        {
            Assert.Equal(StandardValuesSupported, Converter.GetStandardValuesSupported());
            Assert.Equal(StandardValuesSupported, Converter.GetStandardValuesSupported(null));
        }

        [Fact]
        public void GetStandardValues()
        {
            if (!StandardValuesSupported)
            {
                Assert.Null(Converter.GetStandardValues());
            }
        }

        [Fact]
        public void GetStandardValuesExclusive()
        {
            Assert.Equal(StandardValuesExclusive, Converter.GetStandardValuesExclusive());
        }

        protected void CanConvertFrom(Type type)
        {
            Assert.True(Converter.CanConvertFrom(type));
            Assert.True(Converter.CanConvertFrom(null, type));
        }

        protected void CannotConvertFrom(Type type)
        {
            Assert.False(Converter.CanConvertFrom(type));
            Assert.False(Converter.CanConvertFrom(null, type));
        }

        protected void CanConvertTo(Type type)
        {
            Assert.True(Converter.CanConvertTo(type));
            Assert.True(Converter.CanConvertTo(null, type));
        }

        protected void CannotConvertTo(Type type)
        {
            Assert.False(Converter.CanConvertTo(type));
            Assert.False(Converter.CanConvertTo(null, type));
        }

        protected void TestConvertFromString(T value, string str)
        {
            Assert.Equal(value, (T)Converter.ConvertFrom(null, CultureInfo.InvariantCulture, str));
        }

        protected void TestConvertToString(T value, string str)
        {
            Assert.Equal(str, (string)Converter.ConvertTo(null, CultureInfo.InvariantCulture, value, typeof(string)));
        }

        protected void ConvertFromThrowsArgumentExceptionForString(string value)
        {
            AssertExtensions.Throws<ArgumentException>(null, () =>
            {
                Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
            });
        }

        protected void ConvertFromThrowsFormatInnerExceptionForString(string value)
        {
            var ex = Assert.Throws<Exception>(() =>
            {
                Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
            });
            Assert.NotNull(ex.InnerException);
            Assert.IsType<FormatException>(ex.InnerException);
        }

        protected void ConvertFromThrowsNotSupportedFor(object value)
        {
            Assert.Throws<NotSupportedException>(() =>
            {
                Converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
            });
        }

        protected void ConvertToThrowsNotSupportedForType(Type type)
        {
            Assert.Throws<NotSupportedException>(() =>
            {
                Converter.ConvertTo(null, CultureInfo.InvariantCulture, Default, type);
            });
        }

        [Fact]
        public void GetCreateInstanceSupported()
        {
            Assert.Equal(CreateInstanceSupported, Converter.GetCreateInstanceSupported());
            Assert.Equal(CreateInstanceSupported, Converter.GetCreateInstanceSupported(null));
        }

        [Fact]
        public void CreateInstance()
        {
            foreach (var pair in CreateInstancePairs)
            {
                Assert.Equal(pair.Item1, Converter.CreateInstance(pair.Item2));
            }
        }

        [Fact]
        public void GetPropertiesSupported()
        {
            Assert.Equal(IsGetPropertiesSupported, Converter.GetPropertiesSupported());
            Assert.Equal(IsGetPropertiesSupported, Converter.GetPropertiesSupported(null));
        }

        protected void ConvertFromInvariantStringThrowsArgumentException(string str)
        {
            AssertExtensions.Throws<ArgumentException>(null, () =>
            {
                Converter.ConvertFromInvariantString(str);
            });
        }

        protected void ConvertFromInvariantStringThrowsFormatInnerException(string str)
        {
            var ex = Assert.Throws<Exception>(() =>
            {
                Converter.ConvertFromInvariantString(str);
            });
            Assert.NotNull(ex.InnerException);
            Assert.IsType<FormatException>(ex.InnerException);
        }

        protected void ConvertFromStringThrowsArgumentException(string str)
        {
            AssertExtensions.Throws<ArgumentException>(null, () =>
            {
                Converter.ConvertFromString(str);
            });
        }

        protected void ConvertFromStringThrowsFormatInnerException(string str)
        {
            var ex = Assert.Throws<Exception>(() =>
            {
                Converter.ConvertFromString(str);
            });
            Assert.NotNull(ex.InnerException);
            Assert.IsType<FormatException>(ex.InnerException);
        }
    }
}