Skip to content

Commit

Permalink
test: add test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinWilkinson committed Apr 9, 2024
1 parent 1c71d75 commit 44142c4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Testing/CASLTests/Helpers/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="TestHelpers.cs" company="KinsonDigital">
// <copyright file="TestHelpers.cs" company="KinsonDigital">
// Copyright (c) KinsonDigital. All rights reserved.
// </copyright>

Expand Down Expand Up @@ -215,6 +215,30 @@ public static void SetFieldToNull(this object fieldContainer, string fieldName)
foundField.SetValue(fieldContainer, null);
}

/// <summary>
/// Sets a field with a name that matches the given <paramref name="fieldName"/> to the value of null.
/// </summary>
/// <param name="fieldContainer">The object that contains the field.</param>
/// <param name="fieldName">The name of the field.</param>
/// <param name="value">The value to set the field to.</param>
/// <typeparam name="T">The type of parameter.</typeparam>
public static void SetFieldValue<T>(this object fieldContainer, string fieldName, T value)
{
fieldContainer.Should().NotBeNull("setting the field value of a null object is not possible.");
fieldName.Should().NotBeNullOrEmpty("setting an field value requires a non-empty or null field name.");

var fields = fieldContainer.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

fields.Should().HaveCountGreaterThan(0, $"no fields exist in the object.");

var foundField = Array.Find(fields, f => f.Name == fieldName);

foundField.Should().NotBeNull($"a field with the name '{fieldName}' does not exist in the object.");
foundField.FieldType.Should().Be(typeof(T), "the generic type should match the actual field type.");

foundField.SetValue(fieldContainer, value);
}

/// <summary>
/// Gets the boolean field value with a name that matches the given <paramref name="fieldName"/> inside of the object
/// <paramref name="fieldContainer"/>.
Expand Down

0 comments on commit 44142c4

Please sign in to comment.