Skip to content

Commit 313d25f

Browse files
Copilotniemyjski
andcommitted
Refactor TimeUnit tests to follow best practices: use proper naming convention, improve AAA pattern, and simplify test logic
Co-authored-by: niemyjski <[email protected]>
1 parent 7e6613a commit 313d25f

File tree

1 file changed

+74
-35
lines changed

1 file changed

+74
-35
lines changed

tests/Exceptionless.DateTimeExtensions.Tests/TimeUnitTests.cs

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class TimeUnitTests
3636

3737
[Theory]
3838
[MemberData(nameof(TestData))]
39-
public void CanParse(string value, TimeSpan expected)
39+
public void Parse_ValidInput_ReturnsExpectedTimeSpan(string value, TimeSpan expected)
4040
{
4141
Assert.Equal(expected, TimeUnit.Parse(value));
4242
}
@@ -54,7 +54,7 @@ public void CanParse(string value, TimeSpan expected)
5454
[InlineData("1M!")] // special character after unit
5555
[InlineData("1w#")] // special character after unit
5656
[InlineData("1@y")] // special character in middle
57-
public void VerifyParseFailure(string value)
57+
public void Parse_InvalidInput_ThrowsException(string value)
5858
{
5959
Assert.ThrowsAny<Exception>(() => TimeUnit.Parse(value));
6060
}
@@ -99,54 +99,93 @@ public void VerifyParseFailure(string value)
9999
[InlineData("12unknownunit", false)]
100100
[InlineData("12h.", false)]
101101
[InlineData("Blah/Blahs", false)]
102-
public void VerifyTryParse(string value, bool expected)
102+
public void TryParse_VariousInputs_ReturnsExpectedResult(string value, bool expected)
103103
{
104104
bool success = TimeUnit.TryParse(value, out var result);
105105
Assert.Equal(expected, success);
106106
}
107107

108108
[Fact]
109-
public void VerifyMonthsVsMinutesCaseSensitive()
109+
public void Parse_UppercaseM_ParsesAsMonths()
110110
{
111-
// Uppercase M should be months
112-
var monthResult = TimeUnit.Parse("1M");
113-
var expectedMonthDays = (int)TimeSpanExtensions.AvgDaysInAMonth;
114-
Assert.Equal(new TimeSpan(expectedMonthDays, 0, 0, 0), monthResult);
111+
// Arrange
112+
var input = "1M";
113+
var expectedDays = (int)TimeSpanExtensions.AvgDaysInAMonth;
114+
var expected = new TimeSpan(expectedDays, 0, 0, 0);
115+
116+
// Act
117+
var result = TimeUnit.Parse(input);
118+
119+
// Assert
120+
Assert.Equal(expected, result);
121+
}
122+
123+
[Fact]
124+
public void Parse_LowercaseM_ParsesAsMinutes()
125+
{
126+
// Arrange
127+
var input = "1m";
128+
var expected = new TimeSpan(0, 1, 0);
129+
130+
// Act
131+
var result = TimeUnit.Parse(input);
115132

116-
// Lowercase m should be minutes
133+
// Assert
134+
Assert.Equal(expected, result);
135+
}
136+
137+
[Fact]
138+
public void Parse_MonthsAndMinutes_ProduceDifferentResults()
139+
{
140+
// Act
141+
var monthResult = TimeUnit.Parse("1M");
117142
var minuteResult = TimeUnit.Parse("1m");
118-
Assert.Equal(new TimeSpan(0, 1, 0), minuteResult);
119143

120-
// Verify they are different
144+
// Assert
121145
Assert.NotEqual(monthResult, minuteResult);
122146
}
123147

124148
[Theory]
125-
[InlineData("1y", 365)] // Approximately 365 days in a year
126-
[InlineData("1M", 30)] // Approximately 30 days in a month
127-
[InlineData("1w", 7)] // Exactly 7 days in a week
128-
public void VerifyNewTimeUnitsConvertCorrectly(string input, int expectedApproxDays)
149+
[InlineData("1y")]
150+
[InlineData("2y")]
151+
[InlineData("-1y")]
152+
public void Parse_YearUnit_ReturnsExpectedDays(string input)
153+
{
154+
// Act
155+
var result = TimeUnit.Parse(input);
156+
var expectedDays = int.Parse(input.Substring(0, input.Length - 1)) * TimeSpanExtensions.AvgDaysInAYear;
157+
158+
// Assert
159+
Assert.True(Math.Abs(result.TotalDays - expectedDays) < 1,
160+
$"Year conversion should be close to {expectedDays} days, got {result.TotalDays}");
161+
}
162+
163+
[Theory]
164+
[InlineData("1M")]
165+
[InlineData("3M")]
166+
[InlineData("-1M")]
167+
public void Parse_MonthUnit_ReturnsExpectedDays(string input)
129168
{
169+
// Act
130170
var result = TimeUnit.Parse(input);
131-
132-
// For years and months, check approximate values due to fractional constants
133-
if (input.EndsWith("y"))
134-
{
135-
Assert.True(Math.Abs(result.TotalDays - TimeSpanExtensions.AvgDaysInAYear) < 1,
136-
$"Year conversion should be close to {TimeSpanExtensions.AvgDaysInAYear} days, got {result.TotalDays}");
137-
Assert.True(Math.Abs(result.TotalDays - expectedApproxDays) < 10,
138-
$"Year conversion should be approximately {expectedApproxDays} days, got {result.TotalDays}");
139-
}
140-
else if (input.EndsWith("M"))
141-
{
142-
Assert.True(Math.Abs(result.TotalDays - TimeSpanExtensions.AvgDaysInAMonth) < 1,
143-
$"Month conversion should be close to {TimeSpanExtensions.AvgDaysInAMonth} days, got {result.TotalDays}");
144-
Assert.True(Math.Abs(result.TotalDays - expectedApproxDays) < 5,
145-
$"Month conversion should be approximately {expectedApproxDays} days, got {result.TotalDays}");
146-
}
147-
else if (input.EndsWith("w"))
148-
{
149-
Assert.Equal(expectedApproxDays, result.TotalDays);
150-
}
171+
var expectedDays = int.Parse(input.Substring(0, input.Length - 1)) * TimeSpanExtensions.AvgDaysInAMonth;
172+
173+
// Assert
174+
Assert.True(Math.Abs(result.TotalDays - expectedDays) < 1,
175+
$"Month conversion should be close to {expectedDays} days, got {result.TotalDays}");
176+
}
177+
178+
[Theory]
179+
[InlineData("1w", 7)]
180+
[InlineData("2w", 14)]
181+
[InlineData("4w", 28)]
182+
[InlineData("-1w", -7)]
183+
public void Parse_WeekUnit_ReturnsExpectedDays(string input, int expectedDays)
184+
{
185+
// Act
186+
var result = TimeUnit.Parse(input);
187+
188+
// Assert
189+
Assert.Equal(expectedDays, result.TotalDays);
151190
}
152191
}

0 commit comments

Comments
 (0)