|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using nanoFramework.TestFramework; |
| 3 | + |
| 4 | +namespace NFUnitTestAttributes |
| 5 | +{ |
| 6 | + [TestClass] |
| 7 | + public class CallerArgumentExpressionAttributeTests |
| 8 | + { |
| 9 | + public string Field = "Field value"; |
| 10 | + |
| 11 | + public string Property => "Property value"; |
| 12 | + |
| 13 | + [TestMethod] |
| 14 | + public void CallerArgumentExpressionSetsParameterNameFromField() |
| 15 | + { |
| 16 | + var sut = new CallerArgumentExpressionAttributeTests(); |
| 17 | + const string expect = "sut.Field"; |
| 18 | + var actual = TestCallerArgumentExpression(sut.Field); |
| 19 | + |
| 20 | + Assert.AreEqual(expect, actual); |
| 21 | + } |
| 22 | + |
| 23 | + [TestMethod] |
| 24 | + public void CallerArgumentExpressionSetsParameterNameFromMethodParameter() |
| 25 | + { |
| 26 | + const string expect = "methodParameter"; |
| 27 | + var actual = TestMethodParameter("Method parameter value"); |
| 28 | + |
| 29 | + Assert.AreEqual(expect, actual); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public void CallerArgumentExpressionSetsParameterNameFromNull() |
| 34 | + { |
| 35 | + const string expect = "null"; |
| 36 | + var actual = TestCallerArgumentExpression(null); |
| 37 | + |
| 38 | + Assert.AreEqual(expect, actual); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void CallerArgumentExpressionSetsParameterNameFromProperty() |
| 43 | + { |
| 44 | + var sut = new CallerArgumentExpressionAttributeTests(); |
| 45 | + const string expect = "sut.Property"; |
| 46 | + var actual = TestCallerArgumentExpression(sut.Property); |
| 47 | + |
| 48 | + Assert.AreEqual(expect, actual); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void CallerArgumentExpressionSetsParameterNameFromVariable() |
| 53 | + { |
| 54 | + const string variableName = "Variable value"; |
| 55 | + const string expect = nameof(variableName); |
| 56 | + var actual = TestCallerArgumentExpression(variableName); |
| 57 | + |
| 58 | + Assert.AreEqual(expect, actual); |
| 59 | + } |
| 60 | + |
| 61 | + // ReSharper disable once EntityNameCapturedOnly.Local |
| 62 | +#pragma warning disable IDE0060 |
| 63 | + private static string TestCallerArgumentExpression(object objectValue, [CallerArgumentExpression(nameof(objectValue))] string parameterName = null) => parameterName; |
| 64 | +#pragma warning restore IDE0060 |
| 65 | + |
| 66 | + private static string TestMethodParameter(string methodParameter) => TestCallerArgumentExpression(methodParameter); |
| 67 | + } |
| 68 | +} |
0 commit comments