-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 4.5.2
- Loading branch information
Showing
22 changed files
with
380 additions
and
246 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
XCalendar.Core.Tests/Extensions/DateTimeExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
XCalendar.Core.Tests/Extensions/DayOfWeekExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
102
XCalendar.Core.Tests/Extensions/StringsExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.