e91ba2e56c6d4368ba3a85829dbb8787b06ea5bd
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Collections.Generic;
6 using Xunit;
7
8 namespace System.Globalization.Tests
9 {
10     public class DateTimeFormatInfoAbbreviatedMonthGenitiveNames
11     {
12         [Fact]
13         public void AbbreviatedMonthGenitiveNames_GetInvariantInfo_ReturnsExpected()
14         {
15             Assert.Equal(new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" }, DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames);
16         }
17
18         [Fact]
19         public void AbbreviatedMonthGenitiveNames_Get_ReturnsClone()
20         {
21             var format = new DateTimeFormatInfo();
22             Assert.Equal(format.AbbreviatedMonthGenitiveNames, format.AbbreviatedMonthGenitiveNames);
23             Assert.NotSame(format.AbbreviatedMonthGenitiveNames, format.AbbreviatedMonthGenitiveNames);
24         }
25
26         public static IEnumerable<object[]> AbbreviatedMonthGenitiveNames_Set_TestData()
27         {
28             yield return new object[] { new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" } };
29             yield return new object[] { new string[] { "", "", "", "", "", "", "", "", "", "", "", "", "" } };
30         }
31
32         [Theory]
33         [MemberData(nameof(AbbreviatedMonthGenitiveNames_Set_TestData))]
34         public void AbbreviatedMonthGenitiveNames_Set_GetReturnsExpected(string[] value)
35         {
36             var format = new DateTimeFormatInfo();
37             format.AbbreviatedMonthGenitiveNames = value;
38             Assert.Equal(value, format.AbbreviatedMonthGenitiveNames);
39
40             // Does not clone in setter, only in getter.
41             value[0] = null;
42             Assert.NotSame(value, format.AbbreviatedMonthGenitiveNames);
43             Assert.Equal(value, format.AbbreviatedMonthGenitiveNames);
44         }
45
46         [Fact]
47         public void AbbreviatedMonthGenitiveNames_SetNullValue_ThrowsArgumentNullException()
48         {
49             var format = new DateTimeFormatInfo();
50             AssertExtensions.Throws<ArgumentNullException>("value", () => format.AbbreviatedMonthGenitiveNames = null);
51         }
52
53         [Fact]
54         public void AbbreviatedMonthGenitiveNames_SetNullValueInValues_ThrowsArgumentNullException()
55         {
56             var format = new DateTimeFormatInfo();
57             AssertExtensions.Throws<ArgumentNullException>("value", () => format.AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", null, "5", "6", "7", "8", "9", "10", "11", "12", "" });
58         }
59
60         public static IEnumerable<object[]> AbbreviatedMonthGenitiveNames_SetInvalidLength_TestData()
61         {
62             yield return new object[] { new string[] { "Jan" } };
63             yield return new object[] { new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "", "Additional" } };
64         }
65
66         [Theory]
67         [MemberData(nameof(AbbreviatedMonthGenitiveNames_SetInvalidLength_TestData))]
68         public void AbbreviatedMonthGenitiveNames_SetNullValueInValues_ThrowsArgumentNullException(string[] value)
69         {
70             var format = new DateTimeFormatInfo();
71             AssertExtensions.Throws<ArgumentException>("value", () => format.AbbreviatedMonthGenitiveNames = value);
72         }
73
74         [Fact]
75         public void AbbreviatedMonthGenitiveNames_SetReadOnly_ThrowsInvalidOperationException()
76         {
77             Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" });
78         }
79
80         [Fact]
81         public void AbbreviatedMonthGenitiveNames_Format_ReturnsExpected()
82         {
83             var format = new DateTimeFormatInfo();
84             format.AbbreviatedMonthGenitiveNames = new string[] { "GenJan", "GenFeb", "GenMar", "GenApr", "GenMay", "GenJun", "GenJul", "GenAug", "GenSep", "GenOct", "GenNov", "GenDec", "Gen" };
85
86             var dateTime = new DateTime(1976, 6, 19);
87             string formattedDate = dateTime.ToString("d MMM yy", format);
88             Assert.Equal("19 GenJun 76", formattedDate);
89             Assert.Equal(dateTime, DateTime.ParseExact(formattedDate, "d MMM yy", format));
90             Assert.Equal(dateTime, DateTime.Parse(formattedDate, format));
91         }
92
93         [Fact]
94         public void TestAbbreviatedGenitiveNamesWithAllCultures()
95         {
96             CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
97             DateTime dt = new DateTime(2000, 1, 20);
98
99             foreach (CultureInfo ci in cultures)
100             {
101                 string formattedDate = dt.ToString("d MMM yyyy", ci);
102
103                 for (int i = 0; i < 12; i++)
104                 {
105                     if (!ci.DateTimeFormat.MonthNames[i].Equals(ci.DateTimeFormat.MonthGenitiveNames[i]) ||
106                         !ci.DateTimeFormat.AbbreviatedMonthNames[i].Equals(ci.DateTimeFormat.AbbreviatedMonthGenitiveNames[i]))
107                     {
108                         // We have genitive month names, we expect parsing to work and produce the exact original result.
109                         Assert.Equal(dt, DateTime.Parse(formattedDate, ci));
110                         break;
111                     }
112                 }
113
114                 // ParseExact should succeeded all the time even with non genitive cases .
115                 Assert.Equal(dt, DateTime.ParseExact(formattedDate, "d MMM yyyy", ci));
116             }
117         }
118
119         [Fact]
120         public void AbbreviatedMonthGenitiveNames_FormatWithNull_ThrowsNullReferenceException()
121         {
122             var value = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
123             var format = new DateTimeFormatInfo
124             {
125                 AbbreviatedMonthGenitiveNames = value
126             };
127             value[0] = null;
128
129             var dateTime = new DateTime(2014, 1, 28);
130             Assert.Throws<NullReferenceException>(() => dateTime.ToString("dd MMM yy", format));
131         }
132     }
133 }