Skip to content

Commit

Permalink
Fix calendar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j-troc committed Feb 25, 2020
1 parent 6994ae9 commit da2a421
Showing 1 changed file with 74 additions and 75 deletions.
149 changes: 74 additions & 75 deletions source/icu.net.tests/CalendarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,56 +261,43 @@ public void SetTimeZoneTest()
}
}

[Test]
public void FirstDayOfWeekTest()
[TestCase(Calendar.UCalendarDaysOfWeek.Sunday, ExpectedResult = Calendar.UCalendarDaysOfWeek.Sunday)]
[TestCase(Calendar.UCalendarDaysOfWeek.Thursday, ExpectedResult = Calendar.UCalendarDaysOfWeek.Thursday)]
public Calendar.UCalendarDaysOfWeek FirstDayOfWeekTest(Calendar.UCalendarDaysOfWeek firstDayOfWeek)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
var newDay = Calendar.UCalendarDaysOfWeek.Thursday;

var val0 = cal.FirstDayOfWeek;
cal.FirstDayOfWeek = newDay;
var val1 = cal.FirstDayOfWeek;

Assert.AreEqual(Calendar.UCalendarDaysOfWeek.Sunday, val0);
Assert.AreEqual(newDay, val1);
cal.FirstDayOfWeek = firstDayOfWeek;
return cal.FirstDayOfWeek;
}
}

[TestCase(Calendar.UCalendarDaysOfWeek.Sunday, ExpectedResult = 2)]
[TestCase(Calendar.UCalendarDaysOfWeek.Thursday, ExpectedResult = 1)]
[Test]
public void WeekOfYearTest()
public int WeekOfYearTest(Calendar.UCalendarDaysOfWeek firstDayOfWeek)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
cal.Clear();
cal.DayOfMonth = 4;
var newDay = Calendar.UCalendarDaysOfWeek.Thursday;

var val0 = cal.WeekOfYear;
cal.FirstDayOfWeek = newDay;
var val1 = cal.WeekOfYear;


Assert.AreEqual(2, val0);
Assert.AreEqual(1, val1);
cal.FirstDayOfWeek = firstDayOfWeek;
return cal.WeekOfYear;
}
}

[Test]
public void MinimalDaysInFirstWeekTest()
[TestCase(5, ExpectedResult = 1)]
[TestCase(1, ExpectedResult = 2)]
public int MinimalDaysInFirstWeekTest(int minimumDaysInFirstWeek)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
cal.Clear();
cal.DayOfMonth = 4;
var newMinimum = 5;

var val0 = cal.WeekOfYear;
cal.MinimalDaysInFirstWeek = newMinimum;
var val1 = cal.WeekOfYear;

Assert.AreEqual(2, val0);
Assert.AreEqual(1, val1);
cal.MinimalDaysInFirstWeek = minimumDaysInFirstWeek;
return cal.WeekOfYear;
}
}

Expand Down Expand Up @@ -379,35 +366,28 @@ public void LenientTest_ThrowsArgumentException()
}
}

[Test]
public void WeekOfMonthTest()
[TestCase(5, ExpectedResult = 1)]
[TestCase(1, ExpectedResult = 2)]
public int WeekOfMonthTest(int minimumDaysInFirstWeek)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
cal.Clear();
cal.DayOfMonth = 4;
var newMinimum = 5;

var val0 = cal.WeekOfMonth;
cal.MinimalDaysInFirstWeek = newMinimum;
var val1 = cal.WeekOfMonth;

Assert.AreEqual(2, val0);
Assert.AreEqual(1, val1);
cal.MinimalDaysInFirstWeek = minimumDaysInFirstWeek;
return cal.WeekOfMonth;
}
}

[Test]
public void EraTest()
[TestCase(2000, ExpectedResult = 1)]
[TestCase(-1, ExpectedResult = 0)]
public int EraTest(int year)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
var era1 = cal.Era;
cal.Year = -1;
var era0 = cal.Era;

Assert.AreEqual(1, era1);
Assert.AreEqual(0, era0);
cal.Year = year;
return cal.Era;
}
}

Expand All @@ -425,56 +405,61 @@ public void ZoneOffsetTest()
}
}

[Test]
public void DstOffsetTest()
[TestCase(Calendar.UCalendarMonths.July, ExpectedResult = 3600000 /* 60min * 60s * 1000ms */)]
[TestCase(Calendar.UCalendarMonths.January, ExpectedResult = 0)]
public int DstOffsetTest(Calendar.UCalendarMonths month)
{
var expected0 = 60 * 60 * 1000;
var expected1 = 0;

var zone = new TimeZone("Europe/Paris");

using (var cal = new GregorianCalendar(zone))
{
cal.Month = Calendar.UCalendarMonths.July;
cal.Month = month;
cal.DayOfMonth = 20;

var offset0 = cal.DstOffset;
cal.Month = Calendar.UCalendarMonths.January;
var offset1 = cal.DstOffset;
return cal.DstOffset;
}
}

Assert.AreEqual(expected0, offset0);
Assert.AreEqual(expected1, offset1);
[TestCase(3, ExpectedResult = Calendar.UCalendarAMPMs.Am)]
[TestCase(14, ExpectedResult = Calendar.UCalendarAMPMs.Pm)]
public Calendar.UCalendarAMPMs AmPmTest(int hourOfDay)
{
using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
cal.HourOfDay = hourOfDay;
return cal.AmPm;
}
}

[Test]
public void AmPmTest()
[Platform(Include="win")]
public void SetTimeZoneTestWin()
{
var tzdbId = "Romance Standard Time";

var timezones = TimeZoneInfo.GetSystemTimeZones();

var timezone = timezones.First(tzi => tzi.Id == tzdbId);

using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
cal.HourOfDay = 3;
var val0 = cal.AmPm;
cal.HourOfDay = 14;
var val1 = cal.AmPm;
cal.SetTimeZone(timezone);

var tz = cal.GetTimeZone();

Assert.AreEqual(Calendar.UCalendarAMPMs.Am, val0);
Assert.AreEqual(Calendar.UCalendarAMPMs.Pm, val1);
Assert.AreEqual(tzdbId, tz.Id);
}
}

[Test]
public void SetTimeZone2Test()
[Platform(Exclude="win")]
public void SetTimeZoneTestLinux()
{
var tzdbId = "Europe/Paris";
var winId = "Romance Standard Time";


var timezones = TimeZoneInfo.GetSystemTimeZones();

var timezone = timezones.FirstOrDefault(tzi => tzi.Id == tzdbId);
if(timezone==null)
{
timezone = timezones.First(tzi => tzi.Id == winId);
}
var timezone = timezones.First(tzi => tzi.Id == tzdbId);

using (var cal = new GregorianCalendar(new TimeZone("UTC")))
{
Expand All @@ -487,21 +472,35 @@ public void SetTimeZone2Test()
}

[Test]
public void GetTimeZoneInfoTest()
[Platform(Include="win")]
public void GetTimeZoneInfoTestWin()
{
var tzdbId = "Europe/Zagreb";
var winId = "Central European Standard Time";
var tzdbId = "Central European Standard Time";

var timezone = new TimeZone(tzdbId);

using (var cal = new GregorianCalendar(timezone))
{
var result = cal.GetTimeZoneInfo();

Assert.IsTrue(result.Id == winId || result.Id == tzdbId);
Assert.IsTrue(result.Id == tzdbId);
}
}

[Test]
[Platform(Exclude="win")]
public void GetTimeZoneInfoTestLinux()
{
var tzdbId = "Europe/Zagreb";

var timezone = new TimeZone(tzdbId);

using (var cal = new GregorianCalendar(timezone))
{
var result = cal.GetTimeZoneInfo();

Assert.IsTrue(result.Id == tzdbId);
}
}
}
}

0 comments on commit da2a421

Please sign in to comment.