Skip to content
This repository was archived by the owner on May 30, 2021. It is now read-only.

Commit d962f55

Browse files
committed
Changed GetArgumentName expression extension to return a default if the switch doesn't support the type of expression at present.
1 parent 922d102 commit d962f55

File tree

3 files changed

+127
-9
lines changed

3 files changed

+127
-9
lines changed

src/MADE.Testing.MSTest/Extensions/ExpressionExtensions.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,30 @@ internal static class ExpressionExtensions
2424
/// <exception cref="T:System.ArgumentNullException">
2525
/// Thrown if the property expression is null.
2626
/// </exception>
27-
/// <exception cref="T:System.ArgumentException">
28-
/// Thrown if the argument is invalid or not a property.
29-
/// </exception>
3027
internal static string GetArgumentName<T>(this Expression<Func<T>> argumentExpression)
3128
{
3229
if (object.Equals(argumentExpression, null))
3330
{
3431
throw new ArgumentNullException(nameof(argumentExpression));
3532
}
3633

37-
MemberInfo member = null;
34+
string argumentName = null;
3835
switch (argumentExpression.Body)
3936
{
4037
case MemberExpression memberExpression:
41-
member = memberExpression.Member;
38+
argumentName = memberExpression.Member?.Name;
4239
break;
4340
case MethodCallExpression methodCallExpression:
44-
member = methodCallExpression.Method;
41+
argumentName = methodCallExpression.Method?.Name;
4542
break;
4643
}
4744

48-
if (member == null)
45+
if (string.IsNullOrWhiteSpace(argumentName))
4946
{
50-
throw new ArgumentException("Invalid argument", nameof(argumentExpression));
47+
argumentName = nameof(argumentExpression);
5148
}
5249

53-
return member.Name;
50+
return argumentName;
5451
}
5552
}
5653
}

test/MADE.Testing.MSTest.UnitTests/AsserterTests.IsFalse.cs

+55
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,60 @@ public void IsFalse_ConditionIsTrueBooleanMethodResult_ShouldThrowAssertFailedEx
6565
Asserter.IsFalse(() => o.MethodTest(true));
6666
});
6767
}
68+
69+
[TestMethod]
70+
public void IsFalse_ConditionIsSimpleAndBinaryExpressionThatReturnsFalse_ShouldNotThrowAssertFailedException()
71+
{
72+
var o = new TestObject { PropertyTest = true };
73+
74+
ExceptionAsserter.DoesNotThrowException<AssertFailedException>(
75+
() =>
76+
{
77+
Asserter.IsFalse(() => !(o.PropertyTest && o.MethodTest(true)));
78+
});
79+
}
80+
81+
[TestMethod]
82+
public void
83+
IsFalse_ConditionIsComplexAndOrBinaryExpressionThatReturnsTrueForLeftPartButFalseForRightPart_ShouldNotThrowAssertFailedException()
84+
{
85+
var o = new TestObject { PropertyTest = true };
86+
var p = new TestObject { PropertyTest = false };
87+
88+
ExceptionAsserter.DoesNotThrowException<AssertFailedException>(
89+
() =>
90+
{
91+
// (!(true && true) || false ) == false
92+
Asserter.IsFalse(() => !(o.PropertyTest && o.MethodTest(true)) || p.PropertyTest);
93+
});
94+
}
95+
96+
[TestMethod]
97+
public void IsFalse_ConditionIsSimpleAndBinaryExpressionThatReturnsTrue_ShouldThrowAssertFailedException()
98+
{
99+
var o = new TestObject { PropertyTest = false };
100+
101+
Assert.ThrowsException<AssertFailedException>(
102+
() =>
103+
{
104+
// (!(false && true)) == true
105+
Asserter.IsFalse(() => !(o.PropertyTest && o.MethodTest(true)));
106+
});
107+
}
108+
109+
[TestMethod]
110+
public void
111+
IsFalse_ConditionIsComplexAndOrBinaryExpressionThatReturnsTrueForAllParts_ShouldThrowAssertFailedException()
112+
{
113+
var o = new TestObject { PropertyTest = true };
114+
var p = new TestObject { PropertyTest = true };
115+
116+
Assert.ThrowsException<AssertFailedException>(
117+
() =>
118+
{
119+
// (!(true && false) || true) == true
120+
Asserter.IsFalse(() => !(o.PropertyTest && o.MethodTest(false)) || p.PropertyTest);
121+
});
122+
}
68123
}
69124
}

test/MADE.Testing.MSTest.UnitTests/AsserterTests.IsTrue.cs

+66
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,71 @@ public void IsTrue_ConditionIsFalseBooleanMethodResult_ShouldThrowAssertFailedEx
6565
Asserter.IsTrue(() => o.MethodTest(false));
6666
});
6767
}
68+
69+
[TestMethod]
70+
public void IsTrue_ConditionIsSimpleAndBinaryExpressionThatReturnsTrue_ShouldNotThrowAssertFailedException()
71+
{
72+
var o = new TestObject { PropertyTest = true };
73+
74+
ExceptionAsserter.DoesNotThrowException<AssertFailedException>(
75+
() =>
76+
{
77+
Asserter.IsTrue(() => o.PropertyTest && o.MethodTest(true));
78+
});
79+
}
80+
81+
[TestMethod]
82+
public void
83+
IsTrue_ConditionIsComplexAndOrBinaryExpressionThatReturnsTrueForLeftPart_ShouldNotThrowAssertFailedException()
84+
{
85+
var o = new TestObject { PropertyTest = true };
86+
var p = new TestObject { PropertyTest = false };
87+
88+
ExceptionAsserter.DoesNotThrowException<AssertFailedException>(
89+
() =>
90+
{
91+
Asserter.IsTrue(() => (o.PropertyTest && o.MethodTest(true)) || !p.PropertyTest);
92+
});
93+
}
94+
95+
[TestMethod]
96+
public void
97+
IsTrue_ConditionIsComplexAndOrBinaryExpressionThatReturnsFalseForLeftPartButTrueForRightPart_ShouldNotThrowAssertFailedException()
98+
{
99+
var o = new TestObject { PropertyTest = true };
100+
var p = new TestObject { PropertyTest = false };
101+
102+
ExceptionAsserter.DoesNotThrowException<AssertFailedException>(
103+
() =>
104+
{
105+
Asserter.IsTrue(() => (o.PropertyTest && o.MethodTest(false)) || !p.PropertyTest);
106+
});
107+
}
108+
109+
[TestMethod]
110+
public void IsTrue_ConditionIsSimpleAndBinaryExpressionThatReturnsFalse_ShouldThrowAssertFailedException()
111+
{
112+
var o = new TestObject { PropertyTest = false };
113+
114+
Assert.ThrowsException<AssertFailedException>(
115+
() =>
116+
{
117+
Asserter.IsTrue(() => o.PropertyTest && o.MethodTest(true));
118+
});
119+
}
120+
121+
[TestMethod]
122+
public void
123+
IsTrue_ConditionIsComplexAndOrBinaryExpressionThatReturnsFalseForAllParts_ShouldThrowAssertFailedException()
124+
{
125+
var o = new TestObject { PropertyTest = true };
126+
var p = new TestObject { PropertyTest = true };
127+
128+
Assert.ThrowsException<AssertFailedException>(
129+
() =>
130+
{
131+
Asserter.IsTrue(() => (o.PropertyTest && o.MethodTest(false)) || !p.PropertyTest);
132+
});
133+
}
68134
}
69135
}

0 commit comments

Comments
 (0)