|
| 1 | +using FluentAssertions.Execution; |
| 2 | +using FluentAssertions.Primitives; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using System.Diagnostics; |
| 5 | + |
| 6 | +namespace FluentAssertions.AspNetCore.Mvc |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Base class for <see cref="ObjectResultAssertions"/>. |
| 10 | + /// </summary> |
| 11 | + [DebuggerNonUserCode] |
| 12 | + public class ObjectResultAssertionsBase<TObjectResult, TObjectResultAssertion> : ObjectAssertions |
| 13 | + where TObjectResult : ObjectResult |
| 14 | + where TObjectResultAssertion : ObjectResultAssertionsBase<TObjectResult, TObjectResultAssertion> |
| 15 | + { |
| 16 | + #region Public Constructors |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="ObjectResultAssertions" /> class. |
| 19 | + /// </summary> |
| 20 | + /// <param name="subject">The object to test assertion on</param> |
| 21 | + public ObjectResultAssertionsBase(TObjectResult subject) |
| 22 | + : base(subject) |
| 23 | + { |
| 24 | + |
| 25 | + } |
| 26 | + #endregion |
| 27 | + |
| 28 | + #region Public Properties |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The value on the ObjectResult |
| 32 | + /// </summary> |
| 33 | + public object Value => ObjectResultSubject.Value; |
| 34 | + |
| 35 | + #endregion |
| 36 | + |
| 37 | + #region Protected Properties |
| 38 | + |
| 39 | + /// <inheritdoc /> |
| 40 | + protected override string Identifier => typeof(TObjectResult).Name; |
| 41 | + /// <summary> |
| 42 | + /// The <see cref="ReferenceTypeAssertions{TSubject, TAssertions}.Subject"/> casted to the correct type. |
| 43 | + /// </summary> |
| 44 | + protected TObjectResult ObjectResultSubject => (TObjectResult)Subject; |
| 45 | + |
| 46 | + #endregion |
| 47 | + |
| 48 | + #region Public Methods |
| 49 | + /// <summary> |
| 50 | + /// Asserts the value is of the expected type. |
| 51 | + /// </summary> |
| 52 | + /// <typeparam name="TValue">The expected type.</typeparam> |
| 53 | + /// <returns>The typed value.</returns> |
| 54 | + public TValue ValueAs<TValue>() |
| 55 | + { |
| 56 | + var value = ObjectResultSubject.Value; |
| 57 | + |
| 58 | + if (value == null) |
| 59 | + Execute.Assertion |
| 60 | + .WithDefaultIdentifier($"{Identifier}.Value") |
| 61 | + .FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue)); |
| 62 | + |
| 63 | + Execute.Assertion |
| 64 | + .ForCondition(value is TValue) |
| 65 | + .WithDefaultIdentifier($"{Identifier}.Value") |
| 66 | + .FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType()); |
| 67 | + |
| 68 | + return (TValue)value; |
| 69 | + } |
| 70 | + #endregion |
| 71 | + |
| 72 | + } |
| 73 | +} |
0 commit comments