Skip to content

File result assertions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,147 @@ public EmptyResult BeEmptyResult(string reason, params object[] reasonArgs)
return Subject as EmptyResult;
}

/// <summary>
/// Asserts that the subject is an <see cref="FileResult"/>.
/// </summary>
public FileResultAssertions BeFileResult()
{
return BeFileResult(string.Empty, null);
}

/// <summary>
/// Asserts that the subject is an <see cref="FileResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
public FileResultAssertions BeFileResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is FileResult)
.FailWith(Constants.CommonFailMessage, typeof(FileResult).Name, Subject.GetType().Name);

return new FileResultAssertions(Subject as FileResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="FileContentResult"/>.
/// </summary>
public FileContentResultAssertions BeFileContentResult()
{
return BeFileContentResult(string.Empty, null);
}

/// <summary>
/// Asserts that the subject is an <see cref="FileContentResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
public FileContentResultAssertions BeFileContentResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is FileContentResult)
.FailWith(Constants.CommonFailMessage, typeof(FileContentResult).Name, Subject.GetType().Name);

return new FileContentResultAssertions(Subject as FileContentResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
/// </summary>
internal FileStreamResultAssertions BeFileStreamResult()
{
return BeFileStreamResult(string.Empty, null);

}

/// <summary>
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
internal FileStreamResultAssertions BeFileStreamResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is FileStreamResult)
.FailWith(Constants.CommonFailMessage, typeof(FileStreamResult).Name, Subject.GetType().Name);

return new FileStreamResultAssertions(Subject as FileStreamResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="PhysicalFileResult"/>.
/// </summary>
internal PhysicalFileResultAssertions BePhysicalFileResult()
{
return BePhysicalFileResult(string.Empty, null);
}

/// <summary>
/// Asserts that the subject is an <see cref="FileStreamResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
internal PhysicalFileResultAssertions BePhysicalFileResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is PhysicalFileResult)
.FailWith(Constants.CommonFailMessage, typeof(PhysicalFileResult).Name, Subject.GetType().Name);

return new PhysicalFileResultAssertions(Subject as PhysicalFileResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
/// </summary>
internal VirtualFileResultAssertions BeVirtualFileResult()
{
return BeVirtualFileResult(string.Empty, null);
}

/// <summary>
/// Asserts that the subject is an <see cref="VirtualFileResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
internal VirtualFileResultAssertions BeVirtualFileResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is VirtualFileResult)
.FailWith(Constants.CommonFailMessage, typeof(VirtualFileResult).Name, Subject.GetType().Name);

return new VirtualFileResultAssertions(Subject as VirtualFileResult);
}

/// <summary>
/// Asserts that the subject is an <see cref="JsonResult"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using System.Diagnostics;

namespace FluentAssertions.AspNetCore.Mvc
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using FluentAssertions.Execution;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using Microsoft.AspNetCore.Mvc;
using System;

namespace FluentAssertions.AspNetCore.Mvc
{
Expand Down
18 changes: 18 additions & 0 deletions src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
<data name="CommonTypeFailMessage" xml:space="preserve">
<value>Expected {0} to be of type '{1}' but was '{2}'</value>
</data>
<data name="FileContentResult_WithFileContents_LengthFail" xml:space="preserve">
<value>Expected "FileContentResult.FileContents" to have {0} byte(s), but found {1}.</value>
</data>
<data name="FileContentResult_WithFileContents_MatchFail" xml:space="preserve">
<value>Expected "FileContentResult.FileContents[{0}]" to be {1:x2}, but found {2:x2}.</value>
</data>
<data name="RedirectToActionResult_RouteValues_ContainsKey" xml:space="preserve">
<value>RedirectToActionResult.RouteValues does not contain key {0}.</value>
</data>
Expand Down
77 changes: 77 additions & 0 deletions src/FluentAssertions.AspNetCore.Mvc/FileContentResultAssertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using FluentAssertions.Execution;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace FluentAssertions.AspNetCore.Mvc
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="FileContentResult" /> is in the expected state.
/// </summary>>
[DebuggerNonUserCode]
public class FileContentResultAssertions : FileResultAssertions
{
#region Public Constructors

public FileContentResultAssertions(FileContentResult fileResult)
: base(fileResult)
{
}

#endregion

#region Public Properties

/// <summary>
/// The <see cref="FileContentResult.FileContents">FileContents</see> on the <see cref="FileContentResult"/>.
/// </summary>
[SuppressMessage("Performance", "CA1819:Properties should not return arrays",
Justification = "It needs to return the same instance as FileContentResult")]
public byte[] FileContents => FileContentResultSubject.FileContents;

#endregion Private Properties

#region Private Properties

private FileContentResult FileContentResultSubject => (FileContentResult)Subject;

#endregion Private Properties

#region Public Methods

/// <summary>
/// Asserts that the file contents is the expected bytes.
/// </summary>
/// <param name="expectedFileContents">The expected file contents.</param>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
/// </param>
internal FileContentResultAssertions WithFileContents(byte[] expectedFileContents, string reason = "",
params object[] reasonArgs)
{
var actualFileContents = FileContentResultSubject.FileContents;

Execute.Assertion
.ForCondition(expectedFileContents.Length == actualFileContents.Length)
.BecauseOf(reason, reasonArgs)
.FailWith(FailureMessages.FileContentResult_WithFileContents_LengthFail, expectedFileContents.Length, actualFileContents.Length);
for (int i = 0; i < expectedFileContents.Length; i++)
{
var expectedByte = expectedFileContents[i];
var actualByte = actualFileContents[i];
Execute.Assertion
.ForCondition(expectedByte == actualByte)
.BecauseOf(reason, reasonArgs)
.FailWith(FailureMessages.FileContentResult_WithFileContents_MatchFail, i, expectedByte, actualByte);
}

return this;
}

#endregion
}
}
Loading