@@ -36,7 +36,7 @@ public class TimeUnitTests
36
36
37
37
[ Theory ]
38
38
[ MemberData ( nameof ( TestData ) ) ]
39
- public void CanParse ( string value , TimeSpan expected )
39
+ public void Parse_ValidInput_ReturnsExpectedTimeSpan ( string value , TimeSpan expected )
40
40
{
41
41
Assert . Equal ( expected , TimeUnit . Parse ( value ) ) ;
42
42
}
@@ -54,7 +54,7 @@ public void CanParse(string value, TimeSpan expected)
54
54
[ InlineData ( "1M!" ) ] // special character after unit
55
55
[ InlineData ( "1w#" ) ] // special character after unit
56
56
[ InlineData ( "1@y" ) ] // special character in middle
57
- public void VerifyParseFailure ( string value )
57
+ public void Parse_InvalidInput_ThrowsException ( string value )
58
58
{
59
59
Assert . ThrowsAny < Exception > ( ( ) => TimeUnit . Parse ( value ) ) ;
60
60
}
@@ -99,54 +99,93 @@ public void VerifyParseFailure(string value)
99
99
[ InlineData ( "12unknownunit" , false ) ]
100
100
[ InlineData ( "12h." , false ) ]
101
101
[ InlineData ( "Blah/Blahs" , false ) ]
102
- public void VerifyTryParse ( string value , bool expected )
102
+ public void TryParse_VariousInputs_ReturnsExpectedResult ( string value , bool expected )
103
103
{
104
104
bool success = TimeUnit . TryParse ( value , out var result ) ;
105
105
Assert . Equal ( expected , success ) ;
106
106
}
107
107
108
108
[ Fact ]
109
- public void VerifyMonthsVsMinutesCaseSensitive ( )
109
+ public void Parse_UppercaseM_ParsesAsMonths ( )
110
110
{
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 ) ;
115
132
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" ) ;
117
142
var minuteResult = TimeUnit . Parse ( "1m" ) ;
118
- Assert . Equal ( new TimeSpan ( 0 , 1 , 0 ) , minuteResult ) ;
119
143
120
- // Verify they are different
144
+ // Assert
121
145
Assert . NotEqual ( monthResult , minuteResult ) ;
122
146
}
123
147
124
148
[ 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 )
129
168
{
169
+ // Act
130
170
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 ) ;
151
190
}
152
191
}
0 commit comments