Skip to content

Commit

Permalink
4.5.2 (#163)
Browse files Browse the repository at this point in the history
Release 4.5.2
  • Loading branch information
ME-MarvinE authored Sep 8, 2023
2 parents 535184f + 95f50a8 commit ae4dff8
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 246 deletions.
84 changes: 84 additions & 0 deletions XCalendar.Core.Tests/Extensions/DateTimeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Globalization;
using XCalendar.Core.Extensions;
using Xunit;

namespace XCalendar.Core.Tests.Extensions
{
public class DateTimeExtensionsTests
{
[Theory]
[InlineData(2023, 9, 15, 2023, 9, 29)]
[InlineData(2024, 1, 7, 2024, 1, 21)]
[InlineData(2022, 12, 25, 2023, 1, 8)]
public void AddWeeksShouldReturnCorrectDate(
int year, int month, int day,
int expectedYear, int expectedMonth, int expectedDay)
{
var date = new DateTime(year, month, day);

var result = date.AddWeeks(2);

Assert.Equal(new DateTime(expectedYear, expectedMonth, expectedDay), result);
}

[Theory]
[InlineData(2023, 9, 15, 2023, 9, 29)]
[InlineData(2024, 1, 7, 2024, 1, 21)]
[InlineData(2022, 12, 25, 2023, 1, 8)]
public void TryAddWeeksShouldReturnTrue(
int year, int month, int day,
int expectedYear, int expectedMonth, int expectedDay)
{
var date = new DateTime(year, month, day);

var success = date.TryAddWeeks(2, out DateTime result);

Assert.True(success);
Assert.Equal(new DateTime(expectedYear, expectedMonth, expectedDay), result);
}

[Theory]
[InlineData(2023, 9, 15, DayOfWeek.Sunday, 2023, 9, 10)]
[InlineData(2024, 1, 7, DayOfWeek.Sunday, 2024, 1, 7)]
[InlineData(2022, 12, 25, DayOfWeek.Sunday, 2022, 12, 25)]
public void FirstDayOfWeekShouldReturnCorrectDate(
int year, int month, int day, DayOfWeek dayOfWeek,
int expectedYear, int expectedMonth, int expectedDay)
{
var date = new DateTime(year, month, day);

var result = date.FirstDayOfWeek(dayOfWeek);

Assert.Equal(new DateTime(expectedYear, expectedMonth, expectedDay), result);
}

[Theory]
[InlineData(2023, 9, 15, DayOfWeek.Sunday, 2023, 9, 16)]
[InlineData(2024, 1, 7, DayOfWeek.Sunday, 2024, 1, 13)]
[InlineData(2022, 12, 25, DayOfWeek.Sunday, 2022, 12, 31)]
public void LastDayOfWeekShouldReturnCorrectDate(
int year, int month, int day, DayOfWeek dayOfWeek,
int expectedYear, int expectedMonth, int expectedDay)
{
var date = new DateTime(year, month, day);

var result = date.LastDayOfWeek(dayOfWeek);

Assert.Equal(new DateTime(expectedYear, expectedMonth, expectedDay), result);
}

[Theory]
[InlineData(2023, 9, 15, DayOfWeek.Sunday, 3)]
[InlineData(2024, 1, 7, DayOfWeek.Sunday, 2)]
[InlineData(2022, 12, 25, DayOfWeek.Sunday, 5)]
public void CalendarWeekOfMonthShouldReturnCorrectWeek(int year, int month, int day, DayOfWeek dayOfWeek, int expectedWeekOfMonth)
{
var date = new DateTime(year, month, day);

var result = date.CalendarWeekOfMonth(dayOfWeek);

Assert.Equal(expectedWeekOfMonth, result);
}
}
}
44 changes: 44 additions & 0 deletions XCalendar.Core.Tests/Extensions/DayOfWeekExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Xunit;
using XCalendar.Core.Extensions;
using FluentAssertions;

namespace XCalendar.Core.Tests.Extensions
{
public class DayOfWeekExtensionsTests
{
[Theory]
[InlineData(DayOfWeek.Sunday, new[] { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday })]
[InlineData(DayOfWeek.Monday, new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday })]
[InlineData(DayOfWeek.Tuesday, new[] { DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday })]
[InlineData(DayOfWeek.Wednesday, new[] { DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday })]
[InlineData(DayOfWeek.Thursday, new[] { DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday })]
[InlineData(DayOfWeek.Friday, new[] { DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday })]
[InlineData(DayOfWeek.Saturday, new[] { DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday })]
public void GetWeekAsFirstShouldReturnCorrectOrder(DayOfWeek inputDayOfWeek, DayOfWeek[] expectedOrder)
{
// Act
var week = inputDayOfWeek.GetWeekAsFirst();

// Assert
week.Should().ContainInOrder(expectedOrder);
}

[Theory]
[InlineData(DayOfWeek.Sunday, new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday })]
[InlineData(DayOfWeek.Monday, new[] { DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday })]
[InlineData(DayOfWeek.Tuesday, new[] { DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday })]
[InlineData(DayOfWeek.Wednesday, new[] { DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday })]
[InlineData(DayOfWeek.Thursday, new[] { DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday })]
[InlineData(DayOfWeek.Friday, new[] { DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday })]
[InlineData(DayOfWeek.Saturday, new[] { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday })]
public void GetWeekAsLastShouldReturnCorrectOrder(DayOfWeek inputDayOfWeek, DayOfWeek[] expectedOrder)
{
// Act
var week = inputDayOfWeek.GetWeekAsLast();

// Assert
week.Should().ContainInOrder(expectedOrder);
}
}
}
102 changes: 102 additions & 0 deletions XCalendar.Core.Tests/Extensions/StringsExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using FluentAssertions;
using Xunit;
using XCalendar.Core.Extensions;
using System.Globalization;

namespace XCalendar.Core.Tests.Extensions
{
public class StringsExtensionsTests
{
[Theory]
[InlineData("Hello, World!", 5, "Hello")]
[InlineData("Testing", 7, "Testing")]
[InlineData("12345", 3, "123")]
[InlineData("Short", 10, "Short")]
[InlineData("", 5, "")]
[InlineData(null, 5, "")]
public void TruncateStringToMaxLengthWithValidInputIntReturnsTruncatedString(string input, int maxLength, string expected)
{
string result = input.TruncateStringToMaxLength(maxLength);

result.Should().Be(expected);
}

[Theory]
[InlineData("Hello, World!", "5", "Hello")]
[InlineData("Testing", "7", "Testing")]
[InlineData("12345", "3", "123")]
[InlineData("Short", "10", "Short")]
[InlineData("", "5", "")]
[InlineData("Hello, World!", "3", "Hel")]
[InlineData("Hello, World!", "4", "Hell")]
public void TruncateStringToMaxLengthWithValidInputObjectReturnsTruncatedString(string input, object maxLength, string expected)
{
string result = input.TruncateStringToMaxLength(maxLength);

result.Should().Be(expected);
}

[Theory]
[InlineData("Hello, World!", 0, "")]
[InlineData("Testing", -1, "")]
[InlineData("12345", 0, "")]
[InlineData("", 0, "")]
public void TruncateStringToMaxLengthWithZeroOrNegativeMaxLengthReturnsEmptyString(string input, int maxLength, string expected)
{
string result = input.TruncateStringToMaxLength(maxLength);

result.Should().Be(expected);
}

[Theory]
[InlineData("Hello, World!", "invalid", "")]
[InlineData("12345", true, "")]
[InlineData(null, "5L", "")]
[InlineData(null, null, "")]
public void TruncateStringToMaxLengthWithInvalidParameterReturnsEmptyString(string input, object maxLength, string expected)
{
string result = input.TruncateStringToMaxLength(maxLength);

result.Should().Be(expected);
}

[Theory]
[InlineData("hello", "Hello")]
[InlineData("world", "World")]
[InlineData("t", "T")]
[InlineData("123", "123")]
[InlineData("", "")]
[InlineData(null, "")]
public void ToTitleCaseShouldReturnStringWithFirstLetterUppercased(string input, string expectedOutput)
{
var result = input.ToTitleCase(CultureInfo.InvariantCulture);

result.Should().Be(expectedOutput);
}

[Theory]
[InlineData("monday tuesday wednesday thursday friday saturday sunday", "en-US", "Monday Tuesday Wednesday Thursday Friday Saturday Sunday")]
[InlineData("lundi mardi mercredi jeudi vendredi samedi dimanche", "fr-FR", "Lundi Mardi Mercredi Jeudi Vendredi Samedi Dimanche")]
[InlineData("lunes martes miércoles jueves viernes sábado domingo", "es-ES", "Lunes Martes Miércoles Jueves Viernes Sábado Domingo")]
[InlineData("الاثنين الثلاثاء الأربعاء الخميس الجمعة السبت الأحد", "ar-SA", "الاثنين الثلاثاء الأربعاء الخميس الجمعة السبت الأحد")]
[InlineData("星期一 星期二 星期三 星期四 星期五 星期六 星期日", "zh-CN", "星期一 星期二 星期三 星期四 星期五 星期六 星期日")]
public void ToTitleCaseShouldConvertToTitleCaseWithDifferentCultures(string input, string cultureName, string expectedOutput)
{
CultureInfo culture = new CultureInfo(cultureName);

string result = input.ToTitleCase(culture);

result.Should().Be(expectedOutput);
}

[Theory]
[InlineData("Hello", "Hello")]
[InlineData("World", "World")]
public void ToTitleCaseShouldNotChangeStringStartingWithUppercaseLetter(string input, string expectedOutput)
{
var result = input.ToTitleCase(CultureInfo.InvariantCulture);

result.Should().Be(expectedOutput);
}
}
}
25 changes: 13 additions & 12 deletions XCalendar.Core/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static bool TryAddYears(this DateTime self, int value, out DateTime resul
/// <returns>A <see cref="DateTime"/> representing the first day of the week.</returns>
public static DateTime FirstDayOfWeek(this DateTime self)
{
return self.FirstDayOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.FirstDayOfWeek(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the first day of this instance's week using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -233,7 +233,7 @@ public static DateTime FirstDayOfWeek(this DateTime self, DayOfWeek startingDayO
/// <returns>A <see cref="DateTime"/> representing the first day of the week.</returns>
public static DateTime LastDayOfWeek(this DateTime self)
{
return self.LastDayOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.LastDayOfWeek(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the last day of this instance's week using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -295,7 +295,7 @@ public static DateTime LastDayOfYear(this DateTime self)
/// <returns>The number representing the position of this instance's day in this instance's week.</returns>
public static int DayOfWeek(this DateTime self)
{
return self.DayOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.DayOfWeek(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the position of this instance's day in this instance's week using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand All @@ -304,16 +304,17 @@ public static int DayOfWeek(this DateTime self)
/// <returns>The number representing the position of this instance's day in this instance's week.</returns>
public static int DayOfWeek(this DateTime self, DayOfWeek startingDayOfWeek)
{
List<DateTime> currentWeek = self.DaysOfWeek(startingDayOfWeek);
return currentWeek.IndexOf(self.Date) + 1;
List<DayOfWeek> weekAsFirst = startingDayOfWeek.GetWeekAsFirst();

return weekAsFirst.IndexOf(self.Date.DayOfWeek) + 1;
}
/// <summary>
/// Gets which week of this instance's month this instance is in.
/// </summary>
/// <returns>The number corresponding to which week of this instance's month it is in</returns>
public static int CalendarWeekOfMonth(this DateTime self)
{
return self.CalendarWeekOfMonth(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarWeekOfMonth(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets which week of this instance's month this instance is in using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -379,7 +380,7 @@ public static List<DateTime> CalendarWeekInMonth(this DateTime self, int week, D
/// <returns>The number of weeks in this instance's month counting the weeks that span into the previous or next year.</returns>
public static int CalendarWeeksInMonth(this DateTime self)
{
return self.CalendarWeeksInMonth(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarWeeksInMonth(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the number of weeks in this instance's month counting the weeks that span into the previous or next month using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -422,7 +423,7 @@ public static List<DateTime> CalendarDaysInMonth(this DateTime self, DayOfWeek s
/// <returns>The list of days which are before this instance's month and within the same week as its first day.</returns>
public static List<DateTime> CalendarPreviousDaysInMonth(this DateTime self)
{
return self.CalendarPreviousDaysInMonth(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarPreviousDaysInMonth(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the days which are before this instance's month and within the same week as its first day using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand All @@ -446,7 +447,7 @@ public static List<DateTime> CalendarPreviousDaysInMonth(this DateTime self, Day
/// <returns>The list of days which are after this instance's month and within the same week as its last day.</returns>
public static List<DateTime> CalendarNextDaysInMonth(this DateTime self)
{
return self.CalendarNextDaysInMonth(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarNextDaysInMonth(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the days which are after this instance's month and within the same week as its last day using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -476,7 +477,7 @@ public static List<DateTime> CalendarNextDaysInMonth(this DateTime self, DayOfWe
/// <returns>The highest number of weeks that occur inside a month within this instance's year.</returns>
public static int CalendarHighestMonthWeekCountOfYear(this DateTime self)
{
return self.CalendarHighestMonthWeekCountOfYear(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarHighestMonthWeekCountOfYear(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the highest number of weeks that occur inside a month within this instance's year using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -507,7 +508,7 @@ public static int CalendarHighestMonthWeekCountOfYear(this DateTime self, DayOfW
/// <returns>The lowest number of weeks that occur inside a month within this instance's year.</returns>
public static int CalendarLowestMonthWeekCountOfYear(this DateTime self)
{
return self.CalendarLowestMonthWeekCountOfYear(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek);
return self.CalendarLowestMonthWeekCountOfYear(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
}
/// <summary>
/// Gets the lowest number of weeks that occur inside a month within this instance's year using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down Expand Up @@ -539,7 +540,7 @@ public static int CalendarLowestMonthWeekCountOfYear(this DateTime self, DayOfWe
/// <returns>The list of dates in this instance's week.</returns>
public static List<DateTime> DaysOfWeek(this DateTime self, int weekOffset = 0)
{
return self.DaysOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek, weekOffset);
return self.DaysOfWeek(CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek, weekOffset);
}
/// <summary>
/// Gets the dates in this instance's week using the specified <see cref="System.DayOfWeek"/> as the week start.
Expand Down
1 change: 1 addition & 0 deletions XCalendar.Core/Extensions/DayOfWeekExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;

namespace XCalendar.Core.Extensions
{
Expand Down
52 changes: 52 additions & 0 deletions XCalendar.Core/Extensions/StringsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Globalization;

namespace XCalendar.Core.Extensions
{
public static class StringsExtensions
{
public static string TruncateStringToMaxLength(this string value, object parameter)
{
int maxLength = 0;

bool parameterIsNotNullAndIsAInt = parameter != null && int.TryParse(parameter.ToString(), out maxLength);

if (parameterIsNotNullAndIsAInt)
{
return value.TruncateStringToMaxLength(maxLength);
}

return string.Empty;
}

public static string TruncateStringToMaxLength(this string value, int maxLength)
{
if (value == null)
{
return string.Empty;
}

if (maxLength <= 0)
{
return string.Empty;
}
else if (maxLength >= value.Length)
{
return value;
}
else
{
return value.Substring(0, maxLength);
}
}

public static string ToTitleCase(this string text, CultureInfo culture)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

return culture.TextInfo.ToTitleCase(text);
}
}
}
Loading

0 comments on commit ae4dff8

Please sign in to comment.