2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using Xunit ;
6
7
7
8
namespace Microsoft . Net . Http . Headers
8
9
{
9
10
public class DateParserTest
10
11
{
11
- [ Fact ]
12
- public void TryParse_SetOfValidValueStrings_ParsedCorrectly ( )
12
+ [ Theory ]
13
+ [ MemberData ( nameof ( ValidStringData ) ) ]
14
+ public void TryParse_SetOfValidValueStrings_ParsedCorrectly ( string input , DateTimeOffset expected )
13
15
{
14
16
// We don't need to validate all possible date values, since they're already tested in HttpRuleParserTest.
15
17
// Just make sure the parser calls HttpRuleParser methods correctly.
16
- CheckValidParsedValue ( "Tue, 15 Nov 1994 08:12:31 GMT" , new DateTimeOffset ( 1994 , 11 , 15 , 8 , 12 , 31 , TimeSpan . Zero ) ) ;
17
- CheckValidParsedValue ( " Sunday, 06-Nov-94 08:49:37 GMT " , new DateTimeOffset ( 1994 , 11 , 6 , 8 , 49 , 37 , TimeSpan . Zero ) ) ;
18
- CheckValidParsedValue ( " Tue, \r \n 15 Nov \r \n 1994 08:12:31 GMT " , new DateTimeOffset ( 1994 , 11 , 15 , 8 , 12 , 31 , TimeSpan . Zero ) ) ;
18
+ DateTimeOffset result ;
19
+ Assert . True ( HeaderUtilities . TryParseDate ( input , out result ) ) ;
20
+ Assert . Equal ( expected , result ) ;
19
21
}
20
22
21
- [ Fact ]
22
- public void TryParse_SetOfInvalidValueStrings_ReturnsFalse ( )
23
+ private static IEnumerable < object [ ] > ValidStringData ( )
24
+ {
25
+ yield return new object [ ] { "Tue, 15 Nov 1994 08:12:31 GMT" , new DateTimeOffset ( 1994 , 11 , 15 , 8 , 12 , 31 , TimeSpan . Zero ) } ;
26
+ yield return new object [ ] { " Sunday, 06-Nov-94 08:49:37 GMT " , new DateTimeOffset ( 1994 , 11 , 6 , 8 , 49 , 37 , TimeSpan . Zero ) } ;
27
+ yield return new object [ ] { " Tue,\r \n 15 Nov\r \n 1994 08:12:31 GMT " , new DateTimeOffset ( 1994 , 11 , 15 , 8 , 12 , 31 , TimeSpan . Zero ) } ;
28
+ yield return new object [ ] { "Sat, 09-Dec-2017 07:07:03 GMT " , new DateTimeOffset ( 2017 , 12 , 09 , 7 , 7 , 3 , TimeSpan . Zero ) } ;
29
+ }
30
+
31
+ [ Theory ]
32
+ [ MemberData ( nameof ( InvalidStringData ) ) ]
33
+ public void TryParse_SetOfInvalidValueStrings_ReturnsFalse ( string input )
34
+ {
35
+ DateTimeOffset result ;
36
+ Assert . False ( HeaderUtilities . TryParseDate ( input , out result ) ) ;
37
+ Assert . Equal ( new DateTimeOffset ( ) , result ) ;
38
+ }
39
+
40
+ private static IEnumerable < object [ ] > InvalidStringData ( )
23
41
{
24
- CheckInvalidParsedValue ( null ) ;
25
- CheckInvalidParsedValue ( string . Empty ) ;
26
- CheckInvalidParsedValue ( " " ) ;
27
- CheckInvalidParsedValue ( "!!Sunday, 06-Nov-94 08:49:37 GMT" ) ;
42
+ yield return new object [ ] { null } ;
43
+ yield return new object [ ] { string . Empty } ;
44
+ yield return new object [ ] { " " } ;
45
+ yield return new object [ ] { "!!Sunday, 06-Nov-94 08:49:37 GMT" } ;
28
46
}
29
47
30
48
[ Fact ]
@@ -36,23 +54,5 @@ public void ToString_UseDifferentValues_MatchExpectation()
36
54
Assert . Equal ( "Fri, 01 Jan 2010 01:01:01 GMT" ,
37
55
HeaderUtilities . FormatDate ( new DateTimeOffset ( 2010 , 1 , 1 , 1 , 1 , 1 , TimeSpan . Zero ) ) ) ;
38
56
}
39
-
40
- #region Helper methods
41
-
42
- private void CheckValidParsedValue ( string input , DateTimeOffset expectedResult )
43
- {
44
- DateTimeOffset result ;
45
- Assert . True ( HeaderUtilities . TryParseDate ( input , out result ) ) ;
46
- Assert . Equal ( expectedResult , result ) ;
47
- }
48
-
49
- private void CheckInvalidParsedValue ( string input )
50
- {
51
- DateTimeOffset result ;
52
- Assert . False ( HeaderUtilities . TryParseDate ( input , out result ) ) ;
53
- Assert . Equal ( new DateTimeOffset ( ) , result ) ;
54
- }
55
-
56
- #endregion
57
57
}
58
58
}
0 commit comments