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

CultureInfoConverterTests.cs « tests « System.ComponentModel.TypeConverter « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74c1a60a39638a9a25d8f0bd8b6d26d24b7961da (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
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information. 

//
// System.ComponentModel.CultureInfoConverter test cases
//
// Authors:
// 	Gert Driesen (drieseng@users.sourceforge.net)
//
// (c) 2008 Gert Driesen
//

using System.ComponentModel.Design.Serialization;
using System.Globalization;
using Xunit;

namespace System.ComponentModel.Tests
{
    public class CultureInfoConverterTest
    {
        private CultureInfoConverter converter;

        public CultureInfoConverterTest()
        {
            converter = new CultureInfoConverter();
        }

        [Fact]
        public void CanConvertFrom()
        {
            Assert.True(converter.CanConvertFrom(typeof(string)));
            Assert.False(converter.CanConvertFrom(typeof(CultureInfo)));
            Assert.False(converter.CanConvertFrom(typeof(object)));
            Assert.False(converter.CanConvertFrom(typeof(int)));
        }

        [Fact]
        public void CanConvertTo()
        {
            Assert.True(converter.CanConvertTo(typeof(string)));
            Assert.False(converter.CanConvertTo(typeof(object)));
            Assert.False(converter.CanConvertTo(typeof(CultureInfo)));
            Assert.False(converter.CanConvertTo(typeof(int)));
            Assert.True(converter.CanConvertTo(typeof(InstanceDescriptor)));
        }

        [ActiveIssue(11611, TestPlatforms.AnyUnix)]
        [Fact]
        public void ConvertFrom_String()
        {
            CultureInfo c;

            c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture,
                String.Empty);
            Assert.Equal(CultureInfo.InvariantCulture, c);

            c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture,
                "nl-BE");
            Assert.Equal(new CultureInfo("nl-BE"), c);

            Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "Dutch (Bel"));
            Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "duTcH (Bel"));

            c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(Default)");
            Assert.Equal(CultureInfo.InvariantCulture, c);
        }

        [ActiveIssue(11611, TestPlatforms.AnyUnix)]
        [Fact]
        public void ConvertFrom_String_IncompleteName()
        {
            converter.ConvertFrom(null, CultureInfo.InvariantCulture,
                "nl-B");
        }

        [ActiveIssue(11611, TestPlatforms.AnyUnix)]
        [Fact]
        public void ConvertFrom_String_InvalidCulture()
        {
            ArgumentException ex;

            ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(default)"));
            // The (default) culture cannot be converted to
            // a CultureInfo object on this computer
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
            Assert.True(ex.Message.IndexOf("(default)") != -1);
            Assert.Null(ex.ParamName);

            ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, " "));
            // The   culture cannot be converted to
            // a CultureInfo object on this computer
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
            Assert.True(ex.Message.IndexOf("   ") != -1);
            Assert.Null(ex.ParamName);

            ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "\r\n"));
            // The \r\n culture cannot be converted to
            // a CultureInfo object on this computer
            Assert.Equal(typeof(ArgumentException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
            Assert.True(ex.Message.IndexOf("\r\n") != -1);
            Assert.Null(ex.ParamName);
        }

        [Fact]
        public void ConvertFrom_Value_Null()
        {
            NotSupportedException ex = Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, (string)null));
            // CultureInfoConverter cannot convert from (null)
            Assert.Equal(typeof(NotSupportedException), ex.GetType());
            Assert.Null(ex.InnerException);
            Assert.NotNull(ex.Message);
            Assert.True(ex.Message.IndexOf(typeof(CultureInfoConverter).Name) != -1);
            Assert.True(ex.Message.IndexOf("(null)") != -1);
        }

        [Fact]
        public void ConvertToString()
        {
            Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new MyCultureInfo()));
            Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, null));
            Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, CultureInfo.InvariantCulture));
            Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new CultureInfo("nl-BE")));
        }

        [Serializable]
        private sealed class MyCultureInfo : CultureInfo
        {
            internal MyCultureInfo() : base("nl-BE")
            {
            }

            public override string DisplayName
            {
                get { return "display"; }
            }

            public override string EnglishName
            {
                get { return "english"; }
            }
        }

        [Fact]
        public void GetCultureName()
        {
            CustomCultureInfoConverter custom_converter = new CustomCultureInfoConverter();

            CultureInfo fr_culture = CultureInfo.GetCultureInfo("fr-FR");
            Assert.Equal(fr_culture.Name, custom_converter.GetCultureName(fr_culture));

            CultureInfo es_culture = CultureInfo.GetCultureInfo("es-MX");
            Assert.Equal(es_culture.Name, custom_converter.GetCultureName(es_culture));
        }

        private class CustomCultureInfoConverter : CultureInfoConverter
        {
            public new string GetCultureName(CultureInfo culture)
            {
                return base.GetCultureName(culture);
            }
        }
    }
}