Skip to content

Commit f7adf18

Browse files
committed
Change FileResult.WithLastModified's input to nullable.
1 parent 0ecec9d commit f7adf18

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/FluentAssertions.AspNetCore.Mvc/FileResultAssertions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public FileResultAssertions WithFileDownloadName(string expectedFileDownloadName
110110
/// <param name="reasonArgs">
111111
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
112112
/// </param>
113-
public FileResultAssertions WithLastModified(DateTimeOffset expectedLastModified, string reason = "",
113+
public FileResultAssertions WithLastModified(DateTimeOffset? expectedLastModified, string reason = "",
114114
params object[] reasonArgs)
115115
{
116116
var actualLastModified = FileResultSubject.LastModified;

tests/FluentAssertions.AspNetCore.Mvc.Tests/FileResultAssertions_Tests.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,30 @@ public void WithFileDownloadName_GivenUnexpected_ShouldFail()
5757
.WithMessage(failureMessage);
5858
}
5959

60-
[Fact]
61-
public void WithLastModified_GivenExpectedValue_ShouldPass()
60+
[Theory]
61+
[InlineData("2009-06-15 13:45:30 -7h")]
62+
[InlineData(null)]
63+
public void WithLastModified_GivenExpectedValue_ShouldPass(string dateText)
6264
{
6365
var result = TestDataGenerator.CreateFileContentResult();
64-
result.LastModified = DateTimeOffset.Parse("2009-06-15T13:45:30.0000000-07:00");
66+
result.LastModified = TestDataGenerator.CreateDateTimeOffset(dateText);
6567

66-
result.Should().BeFileResult().WithLastModified(DateTimeOffset.Parse("2009-06-15T13:45:30.0000000-07:00"));
68+
result.Should().BeFileResult()
69+
.WithLastModified(TestDataGenerator.CreateDateTimeOffset(dateText));
6770
}
6871

69-
[Fact]
70-
public void WithLastModified_GivenUnexpected_ShouldFail()
72+
[Theory]
73+
[InlineData("2010-07-16 14:46:31 -6h", "2009-06-15 13:45:30 -7h")]
74+
[InlineData(null, "2009-06-15 13:45:30 -7h")]
75+
[InlineData("2010-07-16 14:46:31 -6h", null)]
76+
public void WithLastModified_GivenUnexpected_ShouldFail(
77+
string expected, string actual)
7178
{
72-
var actualValue = DateTimeOffset.Parse("2009-06-15T13:45:30.0000000-07:00");
73-
var expectedValue = DateTimeOffset.Parse("2010-07-16T14:46:31.0000000-06:00");
79+
var actualValue = TestDataGenerator.CreateDateTimeOffset(actual);
80+
var expectedValue = TestDataGenerator.CreateDateTimeOffset(expected);
7481
var result = TestDataGenerator.CreateFileContentResult();
7582
result.LastModified = actualValue;
76-
var failureMessage = "Expected \"FileResult.LastModified\" to be '<2010-07-16 14:46:31 -6h>' but found '<2009-06-15 13:45:30 -7h>'";
83+
var failureMessage = $"Expected \"FileResult.LastModified\" to be '<{expected ?? "null"}>' but found '<{actual ?? "null"}>'";
7784

7885
Action a = () => result.Should().BeFileResult().WithLastModified(expectedValue);
7986

tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/TestDataGenerator.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Globalization;
24
using System.IO;
35
using System.Text;
6+
using System.Text.RegularExpressions;
47

58
namespace FluentAssertions.AspNetCore.Mvc.Tests.Helpers
69
{
@@ -45,5 +48,19 @@ public static byte[] CreateBytes(string content = "")
4548
var bytes = Encoding.UTF8.GetBytes(content);
4649
return bytes;
4750
}
51+
52+
public static DateTimeOffset? CreateDateTimeOffset(string dateText)
53+
{
54+
if (dateText == null)
55+
return null;
56+
var match = dateRegex.Match(dateText);
57+
return new DateTimeOffset(
58+
DateTime.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture),
59+
TimeSpan.FromHours(int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)));
60+
}
61+
62+
private static readonly Regex dateRegex
63+
= new Regex(@"^(\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}) (-?\d+)h$");
64+
4865
}
4966
}

0 commit comments

Comments
 (0)