Skip to content

Commit 3c7c0a6

Browse files
authored
Merge pull request #12 from faddiv/FileResult
File result assertions
2 parents 61613a5 + 29e3e53 commit 3c7c0a6

28 files changed

+1038
-29
lines changed

src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs

+141
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,147 @@ public EmptyResult BeEmptyResult(string reason, params object[] reasonArgs)
8686
return Subject as EmptyResult;
8787
}
8888

89+
/// <summary>
90+
/// Asserts that the subject is an <see cref="FileResult"/>.
91+
/// </summary>
92+
public FileResultAssertions BeFileResult()
93+
{
94+
return BeFileResult(string.Empty, null);
95+
}
96+
97+
/// <summary>
98+
/// Asserts that the subject is an <see cref="FileResult"/>.
99+
/// </summary>
100+
/// <param name="reason">
101+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
102+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
103+
/// </param>
104+
/// <param name="reasonArgs">
105+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
106+
/// </param>
107+
public FileResultAssertions BeFileResult(string reason, params object[] reasonArgs)
108+
{
109+
Execute.Assertion
110+
.BecauseOf(reason, reasonArgs)
111+
.ForCondition(Subject is FileResult)
112+
.FailWith(Constants.CommonFailMessage, typeof(FileResult).Name, Subject.GetType().Name);
113+
114+
return new FileResultAssertions(Subject as FileResult);
115+
}
116+
117+
/// <summary>
118+
/// Asserts that the subject is an <see cref="FileContentResult"/>.
119+
/// </summary>
120+
public FileContentResultAssertions BeFileContentResult()
121+
{
122+
return BeFileContentResult(string.Empty, null);
123+
}
124+
125+
/// <summary>
126+
/// Asserts that the subject is an <see cref="FileContentResult"/>.
127+
/// </summary>
128+
/// <param name="reason">
129+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
130+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
131+
/// </param>
132+
/// <param name="reasonArgs">
133+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
134+
/// </param>
135+
public FileContentResultAssertions BeFileContentResult(string reason, params object[] reasonArgs)
136+
{
137+
Execute.Assertion
138+
.BecauseOf(reason, reasonArgs)
139+
.ForCondition(Subject is FileContentResult)
140+
.FailWith(Constants.CommonFailMessage, typeof(FileContentResult).Name, Subject.GetType().Name);
141+
142+
return new FileContentResultAssertions(Subject as FileContentResult);
143+
}
144+
145+
/// <summary>
146+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
147+
/// </summary>
148+
internal FileStreamResultAssertions BeFileStreamResult()
149+
{
150+
return BeFileStreamResult(string.Empty, null);
151+
152+
}
153+
154+
/// <summary>
155+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
156+
/// </summary>
157+
/// <param name="reason">
158+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
159+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
160+
/// </param>
161+
/// <param name="reasonArgs">
162+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
163+
/// </param>
164+
internal FileStreamResultAssertions BeFileStreamResult(string reason, params object[] reasonArgs)
165+
{
166+
Execute.Assertion
167+
.BecauseOf(reason, reasonArgs)
168+
.ForCondition(Subject is FileStreamResult)
169+
.FailWith(Constants.CommonFailMessage, typeof(FileStreamResult).Name, Subject.GetType().Name);
170+
171+
return new FileStreamResultAssertions(Subject as FileStreamResult);
172+
}
173+
174+
/// <summary>
175+
/// Asserts that the subject is an <see cref="PhysicalFileResult"/>.
176+
/// </summary>
177+
internal PhysicalFileResultAssertions BePhysicalFileResult()
178+
{
179+
return BePhysicalFileResult(string.Empty, null);
180+
}
181+
182+
/// <summary>
183+
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
184+
/// </summary>
185+
/// <param name="reason">
186+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
187+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
188+
/// </param>
189+
/// <param name="reasonArgs">
190+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
191+
/// </param>
192+
internal PhysicalFileResultAssertions BePhysicalFileResult(string reason, params object[] reasonArgs)
193+
{
194+
Execute.Assertion
195+
.BecauseOf(reason, reasonArgs)
196+
.ForCondition(Subject is PhysicalFileResult)
197+
.FailWith(Constants.CommonFailMessage, typeof(PhysicalFileResult).Name, Subject.GetType().Name);
198+
199+
return new PhysicalFileResultAssertions(Subject as PhysicalFileResult);
200+
}
201+
202+
/// <summary>
203+
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
204+
/// </summary>
205+
internal VirtualFileResultAssertions BeVirtualFileResult()
206+
{
207+
return BeVirtualFileResult(string.Empty, null);
208+
}
209+
210+
/// <summary>
211+
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
212+
/// </summary>
213+
/// <param name="reason">
214+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
215+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
216+
/// </param>
217+
/// <param name="reasonArgs">
218+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
219+
/// </param>
220+
internal VirtualFileResultAssertions BeVirtualFileResult(string reason, params object[] reasonArgs)
221+
{
222+
Execute.Assertion
223+
.BecauseOf(reason, reasonArgs)
224+
.ForCondition(Subject is VirtualFileResult)
225+
.FailWith(Constants.CommonFailMessage, typeof(VirtualFileResult).Name, Subject.GetType().Name);
226+
227+
return new VirtualFileResultAssertions(Subject as VirtualFileResult);
228+
}
229+
89230
/// <summary>
90231
/// Asserts that the subject is an <see cref="JsonResult"/>.
91232
/// </summary>

src/FluentAssertions.AspNetCore.Mvc/AssertionsExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Diagnostics;
21
using Microsoft.AspNetCore.Mvc;
32
using Microsoft.AspNetCore.Routing;
3+
using System.Diagnostics;
44

55
namespace FluentAssertions.AspNetCore.Mvc
66
{

src/FluentAssertions.AspNetCore.Mvc/ContentResultAssertions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
2-
using FluentAssertions.Execution;
1+
using FluentAssertions.Execution;
32
using FluentAssertions.Primitives;
43
using Microsoft.AspNetCore.Mvc;
4+
using System;
55

66
namespace FluentAssertions.AspNetCore.Mvc
77
{

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
<data name="CommonTypeFailMessage" xml:space="preserve">
127127
<value>Expected {0} to be of type '{1}' but was '{2}'</value>
128128
</data>
129+
<data name="FileContentResult_WithFileContents_LengthFail" xml:space="preserve">
130+
<value>Expected "FileContentResult.FileContents" to have {0} byte(s), but found {1}.</value>
131+
</data>
132+
<data name="FileContentResult_WithFileContents_MatchFail" xml:space="preserve">
133+
<value>Expected "FileContentResult.FileContents[{0}]" to be {1:x2}, but found {2:x2}.</value>
134+
</data>
129135
<data name="RedirectToActionResult_RouteValues_ContainsKey" xml:space="preserve">
130136
<value>RedirectToActionResult.RouteValues does not contain key {0}.</value>
131137
</data>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using FluentAssertions.Execution;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
namespace FluentAssertions.AspNetCore.Mvc
7+
{
8+
/// <summary>
9+
/// Contains a number of methods to assert that a <see cref="FileContentResult" /> is in the expected state.
10+
/// </summary>>
11+
[DebuggerNonUserCode]
12+
public class FileContentResultAssertions : FileResultAssertions
13+
{
14+
#region Public Constructors
15+
16+
public FileContentResultAssertions(FileContentResult fileResult)
17+
: base(fileResult)
18+
{
19+
}
20+
21+
#endregion
22+
23+
#region Public Properties
24+
25+
/// <summary>
26+
/// The <see cref="FileContentResult.FileContents">FileContents</see> on the <see cref="FileContentResult"/>.
27+
/// </summary>
28+
[SuppressMessage("Performance", "CA1819:Properties should not return arrays",
29+
Justification = "It needs to return the same instance as FileContentResult")]
30+
public byte[] FileContents => FileContentResultSubject.FileContents;
31+
32+
#endregion Private Properties
33+
34+
#region Private Properties
35+
36+
private FileContentResult FileContentResultSubject => (FileContentResult)Subject;
37+
38+
#endregion Private Properties
39+
40+
#region Public Methods
41+
42+
/// <summary>
43+
/// Asserts that the file contents is the expected bytes.
44+
/// </summary>
45+
/// <param name="expectedFileContents">The expected file contents.</param>
46+
/// <param name="reason">
47+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
48+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
49+
/// </param>
50+
/// <param name="reasonArgs">
51+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
52+
/// </param>
53+
internal FileContentResultAssertions WithFileContents(byte[] expectedFileContents, string reason = "",
54+
params object[] reasonArgs)
55+
{
56+
var actualFileContents = FileContentResultSubject.FileContents;
57+
58+
Execute.Assertion
59+
.ForCondition(expectedFileContents.Length == actualFileContents.Length)
60+
.BecauseOf(reason, reasonArgs)
61+
.FailWith(FailureMessages.FileContentResult_WithFileContents_LengthFail, expectedFileContents.Length, actualFileContents.Length);
62+
for (int i = 0; i < expectedFileContents.Length; i++)
63+
{
64+
var expectedByte = expectedFileContents[i];
65+
var actualByte = actualFileContents[i];
66+
Execute.Assertion
67+
.ForCondition(expectedByte == actualByte)
68+
.BecauseOf(reason, reasonArgs)
69+
.FailWith(FailureMessages.FileContentResult_WithFileContents_MatchFail, i, expectedByte, actualByte);
70+
}
71+
72+
return this;
73+
}
74+
75+
#endregion
76+
}
77+
}

0 commit comments

Comments
 (0)