Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merge pull request #6637 from hughbe/globalization-tests-date-time-fo…
Browse files Browse the repository at this point in the history
…rmat-info

Add tests to DateTimeFormatInfo
  • Loading branch information
stephentoub committed Mar 5, 2016
2 parents d23a055 + a7d261d commit fc515b2
Show file tree
Hide file tree
Showing 34 changed files with 114 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -28,7 +26,7 @@ public void AMDesignator_Set()
[Fact]
public void AMDesignator_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AMDesignator = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AMDesignator = null); // Value is null
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AMDesignator = "AA"); // DateTimeFormatInfo.InvariantInfo is read only
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -28,9 +26,9 @@ public void AbbreviatedDayNames_Set()
[Fact]
public void AbbreviatedDayNames_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedDayNames = null); // Value is null
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedDayNames = new string[] { "1", "2", "3", null, "5", "6", "7" }); // Value has null
Assert.Throws<ArgumentException>(() => new DateTimeFormatInfo().AbbreviatedDayNames = new string[] { "sun" }); // Value.Length is not 7
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedDayNames = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedDayNames = new string[] { "1", "2", "3", null, "5", "6", "7" }); // Value has null
Assert.Throws<ArgumentException>("value", (() => new DateTimeFormatInfo().AbbreviatedDayNames = new string[] { "sun" })); // Value.Length is not 7

// DateTimeFormatInfo.InvariantInfo is read only
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AbbreviatedDayNames = new string[] { "1", "2", "3", "4", "5", "6", "7" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -28,9 +26,9 @@ public void AbbreviatedMonthNames_Set()
[Fact]
public void AbbreviatedMonths_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = null); // Value is null
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", null, "5", "6", "7", "8", "9", "10", "11", "12", "" }); // Value has null
Assert.Throws<ArgumentException>(() => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = new string[] { "Jan" }); // Value.Length is not 13
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", null, "5", "6", "7", "8", "9", "10", "11", "12", "" }); // Value has null
Assert.Throws<ArgumentException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthGenitiveNames = new string[] { "Jan" }); // Value.Length is not 13

// DateTimeFormatInfo.InvariantInfo is read only
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void AbbreviatedMonthNames_Set()
[Fact]
public void AbbreviatedMonths_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedMonthNames = null); // Value is null
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().AbbreviatedMonthNames = new string[] { "1", "2", "3", null, "5", "6", "7", "8", "9", "10", "11", "12", "" }); // Value has null
Assert.Throws<ArgumentException>(() => new DateTimeFormatInfo().AbbreviatedMonthNames = new string[] { "Jan" }); // Value.Length is not 13
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthNames = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthNames = new string[] { "1", "2", "3", null, "5", "6", "7", "8", "9", "10", "11", "12", "" }); // Value has null
Assert.Throws<ArgumentException>("value", () => new DateTimeFormatInfo().AbbreviatedMonthNames = new string[] { "Jan" }); // Value.Length is not 13

// DateTimeFormatInfo.InvariantInfo is read only
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthNames = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "" });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 Xunit;

namespace System.Globalization.Tests
{
public class DateTimeFormatInfoCalendar
{
[Fact]
public void Calendar_InvariantInfo()
{
Calendar calendar = DateTimeFormatInfo.InvariantInfo.Calendar;
Assert.IsType<GregorianCalendar>(calendar);
Assert.True(calendar.IsReadOnly);
}

[Fact]
public void Calendar_Set()
{
Calendar newCalendar = new GregorianCalendar(GregorianCalendarTypes.Localized);
var format = new DateTimeFormatInfo();
format.Calendar = newCalendar;
Assert.Equal(newCalendar, format.Calendar);

format.Calendar = newCalendar;
Assert.Equal(newCalendar, format.Calendar);
}

[Fact]
public void Calendar_Set_Invalid()
{
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().Calendar = null); // Value is null
Assert.Throws<ArgumentOutOfRangeException>("value", () => new DateTimeFormatInfo().Calendar = new ThaiBuddhistCalendar()); // Value is invalid
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.Calendar = new GregorianCalendar()); // DateTimeFormatInfo.InvariantInfo is read only
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -37,10 +34,18 @@ public void CalendarWeekRule_Set(CalendarWeekRule newCalendarWeekRule)
Assert.Equal(newCalendarWeekRule, format.CalendarWeekRule);
}

[Theory]
[InlineData(CalendarWeekRule.FirstDay - 1)]
[InlineData(CalendarWeekRule.FirstFourDayWeek + 1)]
public void CalendarWeekRule_Set_Invalid_ThrowsArgumentOutOfRangeException(CalendarWeekRule value)
{
Assert.Throws<ArgumentOutOfRangeException>("value", () => new DateTimeFormatInfo().CalendarWeekRule = value);
}

[Fact]
public void CalendarWeekRule_Set_Invalid()
public void CalendarWeekRule_Set_ReadOnly_ThrowsInvalidOperationException()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().CalendarWeekRule = (CalendarWeekRule)(-1)); // Value is invalid
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.CalendarWeekRule = CalendarWeekRule.FirstDay); // DateTimeFormatInfo.InvariantInfo is read only
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -28,9 +26,9 @@ public void DayNames_Set()
[Fact]
public void DayNames_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().DayNames = null); // Value is null
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().DayNames = new string[] { "1", "2", "3", null, "5", "6", "7" }); // Value has null
Assert.Throws<ArgumentException>(() => new DateTimeFormatInfo().DayNames = new string[] { "sun" }); // Value.Length is not 7
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().DayNames = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().DayNames = new string[] { "1", "2", "3", null, "5", "6", "7" }); // Value has null
Assert.Throws<ArgumentException>("value", () => new DateTimeFormatInfo().DayNames = new string[] { "sun" }); // Value.Length is not 7

// DateTimeFormatInfo.InvariantInfo is read only
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.DayNames = new string[] { "1", "2", "3", "4", "5", "6", "7" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -40,10 +38,17 @@ public void FirstDayOfWeek_Set(DayOfWeek newFirstDayOfWeek)
Assert.Equal(newFirstDayOfWeek, format.FirstDayOfWeek);
}

[Theory]
[InlineData(DayOfWeek.Sunday - 1)]
[InlineData(DayOfWeek.Saturday + 1)]
public void FirstDayOfWeek_Set_Invalid_ThrowsArgumentOutOfRangeException(DayOfWeek value)
{
Assert.Throws<ArgumentOutOfRangeException>("value", () => new DateTimeFormatInfo().FirstDayOfWeek = value);
}

[Fact]
public void FirstDayOfWeek_Set_Invalid()
public void FirstDayOfWeek_Set_ReadOnly_ThrowsInvalidOperationException()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().FirstDayOfWeek = (DayOfWeek)(-1)); // Value is invalid
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.FirstDayOfWeek = DayOfWeek.Wednesday); // DateTimeFormatInfo.InvariantInfo is read only
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -40,7 +38,7 @@ public void FullDateTimePattern_Set(string newFullDateTimePattern)
[Fact]
public void FullDateTimePattern_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().FullDateTimePattern = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().FullDateTimePattern = null); // Value is null
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.FullDateTimePattern = "dddd, dd MMMM yyyy HH:mm:ss"); // DateTimeFormatInfo.InvariantInfo is read only
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public void GetAbbreviatedDayName(DateTimeFormatInfo info, string[] expected)
}
}

[Fact]
public void GetAbbreviatedDayName_Invalid()
[Theory]
[InlineData(DayOfWeek.Sunday - 1)]
[InlineData(DayOfWeek.Saturday + 1)]
public void GetAbbreviatedDayName_Invalid_ThrowsArgumentOutOfRangeException(DayOfWeek dayofweek)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetAbbreviatedDayName((DayOfWeek)(-1))); // DayOfWeek is invalid
Assert.Throws<ArgumentOutOfRangeException>("dayofweek", () => new DateTimeFormatInfo().GetAbbreviatedDayName(dayofweek));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -32,10 +29,10 @@ public void GetAbbreviatedEraName(DateTimeFormatInfo format, int era, string exp
public void GetAbbreviatedEraName_Invalid()
{
var format = new CultureInfo("en-US").DateTimeFormat;
Assert.Throws<ArgumentOutOfRangeException>(() => format.GetAbbreviatedEraName(-1)); // Era < 0
Assert.Throws<ArgumentOutOfRangeException>("era", () => format.GetAbbreviatedEraName(-1)); // Era < 0

const int EnUSMaxEra = 1;
Assert.Throws<ArgumentOutOfRangeException>(() => format.GetAbbreviatedEraName(EnUSMaxEra + 1)); // Era > max era for the culture
Assert.Throws<ArgumentOutOfRangeException>("era", () => format.GetAbbreviatedEraName(EnUSMaxEra + 1)); // Era > max era for the culture
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void GetAbbreviatedMonthName(DateTimeFormatInfo info, string[] expected)
[Fact]
public void GetAbbreviatedMonthName_Invalid()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetAbbreviatedMonthName(MinMonth - 1)); // DayOfWeek is invalid
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetAbbreviatedMonthName(MaxMonth + 1)); // DayOfWeek is invalid
Assert.Throws<ArgumentOutOfRangeException>("month", () => new DateTimeFormatInfo().GetAbbreviatedMonthName(MinMonth - 1)); // Month is invalid
Assert.Throws<ArgumentOutOfRangeException>("month", (() => new DateTimeFormatInfo().GetAbbreviatedMonthName(MaxMonth + 1))); // Month is invalid
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ public void GetDayName(DateTimeFormatInfo format, string[] expected)
}
}

public void GetDayName_Invalid()
[Theory]
[InlineData(DayOfWeek.Sunday - 1)]
[InlineData(DayOfWeek.Saturday + 1)]
public void GetDayName_Invalid_ThrowsArgumentOutOfRangeException(DayOfWeek dayofweek)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetDayName((DayOfWeek)(-1))); // DayOfWeek is invalid
Assert.Throws<ArgumentOutOfRangeException>("dayofweek", () => new DateTimeFormatInfo().GetDayName(dayofweek));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -59,9 +58,9 @@ public void GetEra(DateTimeFormatInfo format, string eraName, int expected)
}

[Fact]
public void GetEra_Invalid()
public void GetEra_Null_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().GetEra(null)); // Era name is null
Assert.Throws<ArgumentNullException>("eraName", () => new DateTimeFormatInfo().GetEra(null)); // Era name is null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -35,7 +33,10 @@ public void GetEraName(DateTimeFormatInfo format, int era, string expected)
[Fact]
public void GetEraName_Invalid()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetEraName(-1)); // Era < 0
Assert.Throws<ArgumentOutOfRangeException>("era", () => new DateTimeFormatInfo().GetEraName(-1)); // Era < 0

const int EnUSMaxEra = 1;
Assert.Throws<ArgumentOutOfRangeException>("era", () => new CultureInfo("en-US").DateTimeFormat.GetAbbreviatedEraName(EnUSMaxEra + 1)); // Era > max era for the culture
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void GetMonthName(DateTimeFormatInfo format, string[] expected)
[Fact]
public void GetMonthName_Invalid()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetMonthName(MinMonth - 1));
Assert.Throws<ArgumentOutOfRangeException>(() => new DateTimeFormatInfo().GetMonthName(MaxMonth + 1));
Assert.Throws<ArgumentOutOfRangeException>("month", () => new DateTimeFormatInfo().GetMonthName(MinMonth - 1));
Assert.Throws<ArgumentOutOfRangeException>("month", () => new DateTimeFormatInfo().GetMonthName(MaxMonth + 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// 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;
using System.Collections.Generic;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -40,7 +38,7 @@ public void LongDatePattern_Set(string newLongDatePattern)
[Fact]
public void LongDatePattern_Set_Invalid()
{
Assert.Throws<ArgumentNullException>(() => new DateTimeFormatInfo().LongDatePattern = null); // Value is null
Assert.Throws<ArgumentNullException>("value", () => new DateTimeFormatInfo().LongDatePattern = null); // Value is null
Assert.Throws<InvalidOperationException>(() => DateTimeFormatInfo.InvariantInfo.LongDatePattern = "dddd, dd MMMM yyyy"); // DateTimeFormatInfo.InvariantInfo is read only
}
}
Expand Down
Loading

0 comments on commit fc515b2

Please sign in to comment.