J’ai une classe avec un tas d’opérateurs surchargés:
public static double[,] operator +(Masortingx masortingx, double[,] array) public static double[,] operator -(Masortingx masortingx, double[,] array) public static double[,] operator *(Masortingx masortingx, double[,] array)
Pour tous, j’aimerais tester les opérandes pour null. J’ai un test NUnit
pour ça:
public void MasortingxOperatorOperandIsNullThrows(Func op) { Masortingx m = null; var right = new double[,] {{1, 1}, {1, 1}}; Assert.Throws(() => op(m, right)); }
Comment puis-je passer un lambda pour chaque opérateur comme (l,r) => l + r
?
Vous pouvez passer exactement cela:
MasortingxOperatorOperandIsNullThrows((l,r) => l + r);
Vous ne pouvez pas appliquer immédiatement l’atsortingbut TestCase contenant une expression lambda, autrement dit, le test suivant serait non valide:
[TestCase((a, b) => a + b)] public void WillNotComstackTest(Func func) { Assert.GreaterOrEqual(func(1.0, 1.0), 1.0); }
Cependant, vous pouvez utiliser l’atsortingbut TestCaseSource avec un IEnumerable de vos expressions lambda, comme ceci:
[TestFixture] public class TestClass { private IEnumerable> TestCases { get { yield return (a, b) => a + b; yield return (a, b) => a * b; yield return (a, b) => a / b; } } [TestCaseSource("TestCases")] public void Test(Func func) { Assert.GreaterOrEqual(func(1.0, 1.0), 1.0); } }