Skip to content

Commit

Permalink
ExDM- Harmonize the code with the 01-DelegateEventsScript.md document #…
Browse files Browse the repository at this point in the history
…404

- Updated sample code for delegates.
- UT 👍
  • Loading branch information
mpostol committed Sep 27, 2024
1 parent e269082 commit 80f89db
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ namespace TP.FunctionalProgramming
[TestClass]
public class AnonymousFunctionsUnitTest
{
/// <summary>
/// While raising this delegate variable the null-conditional operator is not applied.
/// Hence, this argument must not be null to prevent throwing an exception.
/// </summary>
[TestMethod]
public void NullCallBack()
{
AnonymousFunctions _newInstance = new AnonymousFunctions();
Assert.ThrowsException<NullReferenceException>(() => _newInstance.ConsistencyCheck(null));
}

[TestMethod]
public void NamedMethodCallBackTest()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//____________________________________________________________________________________________________________________________________
//
// Copyright (C) 2023, Mariusz Postol LODZ POLAND.
// Copyright (C) 2024, Mariusz Postol LODZ POLAND.
//
// To be in touch join the community by pressing the `Watch` button and get started commenting using the discussion panel at
//
Expand All @@ -14,9 +14,48 @@

namespace TP.FunctionalProgramming
{
/// <summary>
/// This testing class reveals features of delegate.
/// </summary>
[TestClass]
public class DelegateExampleUnitTest
{
/// <summary>
/// Typically the delegate value is instantiated using a new operator.
/// It is possible to instantiate the delegate value by applying more concise syntax
/// using only the method name or definition of the anonymous function.
/// </summary>
[TestMethod]
public void Instantiation()
{
DelegateExample _newInstance = new DelegateExample();
_newInstance.PerformCalculationVar = new DelegateExample.PerformCalculation(PerformSumMethod);// Delegate value instantiation using new operator
Assert.IsNotNull(_newInstance.PerformCalculationVar);
Assert.AreEqual<int>(5, _newInstance.PerformCalculationVar(2, 2));
_newInstance = new DelegateExample();
_newInstance.PerformCalculationVar = PerformSumMethod; // Auto instantiation using only method name.
// It looks like assignment of a method to delegate variable
Assert.IsNotNull(_newInstance.PerformCalculationVar);
Assert.AreEqual<int>(5, _newInstance.PerformCalculationVar(2, 2));
_newInstance.PerformCalculationVar = (x, y) => x + y + 1;
Assert.IsNotNull(_newInstance.PerformCalculationVar);
Assert.AreEqual<int>(5, _newInstance.PerformCalculationVar(2, 2));
}

/// <summary>
/// The delegate variable is of reference type - the null value can be assigned.
/// It indicates that invocation of the delegate variable containing null throws an exception.
/// Thanks to the null-conditional operator nothing is invoked if the delegate variable evaluates to null
/// </summary>
[TestMethod]
public void NullInvocation()
{
DelegateExample _newInstance = new DelegateExample();
_newInstance.PerformCalculationVar = null;
Assert.ThrowsException<NullReferenceException>(() => _newInstance.PerformCalculationVar(0, 0));
_newInstance.PerformCalculationVar?.Invoke(0, 0); //thanks to the null-conditional operator nothing is invoked
}

[TestMethod]
public void SumTestMethod()
{
Expand Down Expand Up @@ -63,6 +102,12 @@ public void PerformSumMethodCalledTest()
Assert.AreSame(_args, EventArgs.Empty);
}

/// <summary>
/// This function adds the values of two integers and returns the result incremented by 1.
/// </summary>
/// <param name="x">The first parameter used by the add operation</param>
/// <param name="y">The second parameter used by the add operation</param>
/// <returns>the sum of x and y parameters incremented by 1</returns>
private int PerformSumMethod(int x, int y)
{
checked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public class AnonymousFunctions

internal delegate void CallBackTestDelegate(bool testResult);

/// <summary>
/// Invoking this method the consistency of this state machine is checked.
/// The result is transferred to the user through the <paramref name="testResult"/> delegate pointing
/// out a method invoked to transfer the check result. While raising this delegate variable
/// the null-conditional operator is not applied. Hence, this argument must not be null to prevent
/// throwing an exception.
/// </summary>
/// <param name="testResult">This parameter contains a delegate value pointing out a method invoked
/// to transfer the check result. This argument must not be null to prevent throwing an exception.
/// </param>
/// <exception cref="NullReferenceException">exception thrown if the <paramref name="testResult"/> evaluates to null.
/// </exception>
[Conditional("DEBUG")]
internal void ConsistencyCheck(CallBackTestDelegate testResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DelegateExample
/// The perform calculation variable
/// </summary>
/// <returns>System.Int32.</returns>
public PerformCalculation PerformCalculationVar;
public PerformCalculation PerformCalculationVar = null;

/// <summary>
/// An example of event
Expand All @@ -38,8 +38,6 @@ public class DelegateExample
/// </summary>
public DelegateExample()
{
PerformCalculationVar = new PerformCalculation(PerformSumMethod);
PerformCalculationVar = new PerformCalculation(PerformSubtractMethod);
//PerformCalculationVar = new PerformCalculation(PerformSubtractDoubleMethod); //No overload for 'DelegateExample.PerformSubtractDoubleMethod(double, double)' matches delegate 'DelegateExample.PerformCalculation'
}

Expand Down

0 comments on commit 80f89db

Please sign in to comment.