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.
5 using System.Collections.Generic;
8 namespace System.Globalization.Tests
10 public class DateTimeFormatInfoAbbreviatedMonthGenitiveNames
13 public void AbbreviatedMonthGenitiveNames_GetInvariantInfo_ReturnsExpected()
15 Assert.Equal(new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" }, DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames);
19 public void AbbreviatedMonthGenitiveNames_Get_ReturnsClone()
21 var format = new DateTimeFormatInfo();
22 Assert.Equal(format.AbbreviatedMonthGenitiveNames, format.AbbreviatedMonthGenitiveNames);
23 Assert.NotSame(format.AbbreviatedMonthGenitiveNames, format.AbbreviatedMonthGenitiveNames);
26 public static IEnumerable<object[]> AbbreviatedMonthGenitiveNames_Set_TestData()
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[] { "", "", "", "", "", "", "", "", "", "", "", "", "" } };
33 [MemberData(nameof(AbbreviatedMonthGenitiveNames_Set_TestData))]
34 public void AbbreviatedMonthGenitiveNames_Set_GetReturnsExpected(string[] value)
36 var format = new DateTimeFormatInfo();
37 format.AbbreviatedMonthGenitiveNames = value;
38 Assert.Equal(value, format.AbbreviatedMonthGenitiveNames);
40 // Does not clone in setter, only in getter.
42 Assert.NotSame(value, format.AbbreviatedMonthGenitiveNames);
43 Assert.Equal(value, format.AbbreviatedMonthGenitiveNames);
47 public void AbbreviatedMonthGenitiveNames_SetNullValue_ThrowsArgumentNullException()
49 var format = new DateTimeFormatInfo();
50 AssertExtensions.Throws<ArgumentNullException>("value", () => format.AbbreviatedMonthGenitiveNames = null);
54 public void AbbreviatedMonthGenitiveNames_SetNullValueInValues_ThrowsArgumentNullException()
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", "" });
60 public static IEnumerable<object[]> AbbreviatedMonthGenitiveNames_SetInvalidLength_TestData()
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" } };
67 [MemberData(nameof(AbbreviatedMonthGenitiveNames_SetInvalidLength_TestData))]
68 public void AbbreviatedMonthGenitiveNames_SetNullValueInValues_ThrowsArgumentNullException(string[] value)
70 var format = new DateTimeFormatInfo();
71 AssertExtensions.Throws<ArgumentException>("value", () => format.AbbreviatedMonthGenitiveNames = value);
75 public void AbbreviatedMonthGenitiveNames_SetReadOnly_ThrowsInvalidOperationException()
77 Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" });
81 public void AbbreviatedMonthGenitiveNames_Format_ReturnsExpected()
83 var format = new DateTimeFormatInfo();
84 format.AbbreviatedMonthGenitiveNames = new string[] { "GenJan", "GenFeb", "GenMar", "GenApr", "GenMay", "GenJun", "GenJul", "GenAug", "GenSep", "GenOct", "GenNov", "GenDec", "Gen" };
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));
94 public void TestAbbreviatedGenitiveNamesWithAllCultures()
96 CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
97 DateTime dt = new DateTime(2000, 1, 20);
99 foreach (CultureInfo ci in cultures)
101 string formattedDate = dt.ToString("d MMM yyyy", ci);
103 for (int i = 0; i < 12; i++)
105 if (!ci.DateTimeFormat.MonthNames[i].Equals(ci.DateTimeFormat.MonthGenitiveNames[i]) ||
106 !ci.DateTimeFormat.AbbreviatedMonthNames[i].Equals(ci.DateTimeFormat.AbbreviatedMonthGenitiveNames[i]))
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));
114 // ParseExact should succeeded all the time even with non genitive cases .
115 Assert.Equal(dt, DateTime.ParseExact(formattedDate, "d MMM yyyy", ci));
120 public void AbbreviatedMonthGenitiveNames_FormatWithNull_ThrowsNullReferenceException()
122 var value = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
123 var format = new DateTimeFormatInfo
125 AbbreviatedMonthGenitiveNames = value
129 var dateTime = new DateTime(2014, 1, 28);
130 Assert.Throws<NullReferenceException>(() => dateTime.ToString("dd MMM yy", format));