Skip to content

Commit 50c5dcc

Browse files
committed
Add func extensions sync/async IsTrue, IsFalse.
1 parent ce7993b commit 50c5dcc

2 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+
// Author : RzR
4+
// Created On : 2025-07-11 12:38
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2025-07-11 13:01
8+
// ***********************************************************************
9+
// <copyright file="FuncExtensions.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using System;
20+
using System.Threading.Tasks;
21+
using DomainCommonExtensions.DataTypeExtensions;
22+
23+
#endregion
24+
25+
namespace DomainCommonExtensions.CommonExtensions
26+
{
27+
/// <summary>
28+
/// A function extensions.
29+
/// </summary>
30+
public static class FuncExtensions
31+
{
32+
/// <summary>
33+
/// A Func&lt;bool&gt; extension method that query if 'func' result is true.
34+
/// </summary>
35+
/// <exception cref="ArgumentNullException">
36+
/// Thrown when one or more required arguments are null.
37+
/// </exception>
38+
/// <param name="func">The func to act on.</param>
39+
/// <returns>
40+
/// True if true, false if not.
41+
/// </returns>
42+
public static bool IsTrue(this Func<bool> func)
43+
{
44+
if (func.IsNull())
45+
throw new ArgumentNullException(nameof(func));
46+
47+
return func.Invoke().IsTrue();
48+
}
49+
50+
/// <summary>
51+
/// A Func&lt;bool&gt; extension method that query if 'func' result is false.
52+
/// </summary>
53+
/// <param name="func">The func to act on.</param>
54+
/// <returns>
55+
/// True if false, false if not.
56+
/// </returns>
57+
public static bool IsFalse(this Func<bool> func)
58+
{
59+
return func.IsTrue().IsFalse();
60+
}
61+
62+
/// -------------------------------------------------------------------------------------------------
63+
/// <summary>
64+
/// A Func&lt;bool&gt; extension method that query if 'func' result is true.
65+
/// </summary>
66+
/// <exception cref="ArgumentNullException">
67+
/// Thrown when one or more required arguments are null.
68+
/// </exception>
69+
/// <param name="func">The func to act on.</param>
70+
/// <returns>
71+
/// True if true, false if not.
72+
/// </returns>
73+
/// =================================================================================================
74+
public static bool IsTrue(this Func<Task<bool>> func)
75+
{
76+
if (func.IsNull())
77+
throw new ArgumentNullException(nameof(func));
78+
79+
return func.Invoke().Result.IsTrue();
80+
}
81+
82+
/// -------------------------------------------------------------------------------------------------
83+
/// <summary>
84+
/// A Func&lt;bool&gt; extension method that query if 'func' result is false.
85+
/// </summary>
86+
/// <param name="func">The func to act on.</param>
87+
/// <returns>
88+
/// True if false, false if not.
89+
/// </returns>
90+
/// =================================================================================================
91+
public static bool IsFalse(this Func<Task<bool>> func)
92+
{
93+
return func.IsTrue().IsFalse();
94+
}
95+
}
96+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DataTypeTests
3+
// Author : RzR
4+
// Created On : 2025-07-11 12:43
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2025-07-11 12:58
8+
// ***********************************************************************
9+
// <copyright file="FuncExtensionsTests.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using System;
20+
using System.Threading.Tasks;
21+
using DomainCommonExtensions.CommonExtensions;
22+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23+
24+
#endregion
25+
26+
namespace DataTypeTests.UtilsTests
27+
{
28+
[TestClass]
29+
public class FuncExtensionsTests
30+
{
31+
[TestMethod]
32+
public void IsTrue_Func_ShouldBe_True_Test()
33+
{
34+
Func<bool> func = () => { return 1 == 1; };
35+
36+
var funcResult = func.IsTrue();
37+
38+
Assert.IsNotNull(funcResult);
39+
Assert.IsTrue(funcResult);
40+
}
41+
42+
[TestMethod]
43+
public void IsFalse_Func_ShouldBe_True_Test()
44+
{
45+
Func<bool> func = () => { return 1 == 0; };
46+
47+
var funcResult = func.IsFalse();
48+
49+
Assert.IsNotNull(funcResult);
50+
Assert.IsTrue(funcResult);
51+
}
52+
53+
[TestMethod]
54+
public void IsTrue_AsyncFunc_ShouldBe_True_Test()
55+
{
56+
Func<Task<bool>> func = async () => { return await Task.FromResult(1 == 1); };
57+
58+
var funcResult = func.IsTrue();
59+
60+
Assert.IsNotNull(funcResult);
61+
Assert.IsTrue(funcResult);
62+
}
63+
64+
[TestMethod]
65+
public void IsFalse_AsyncFunc_ShouldBe_True_Test()
66+
{
67+
Func<Task<bool>> func = async () => { return await Task.FromResult(1 == 0); };
68+
69+
var funcResult = func.IsFalse();
70+
71+
Assert.IsNotNull(funcResult);
72+
Assert.IsTrue(funcResult);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)