Skip to content

Commit 0098979

Browse files
CoryCharltonjosesimoes
authored andcommitted
Moving attributes from System.Runtime (nanoframework#219)
***NO_CI*** (cherry picked from commit db43573)
1 parent 4dc84a8 commit 0098979

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Runtime.CompilerServices;
2+
using nanoFramework.TestFramework;
3+
4+
namespace NFUnitTestAttributes
5+
{
6+
[TestClass]
7+
public class CallerMemberNameAttributeTests
8+
{
9+
[TestMethod]
10+
public void CallerMemberNameAttributeGetsCallerMemberName()
11+
{
12+
const string expect = nameof(CallerMemberNameAttributeGetsCallerMemberName);
13+
var actual = TestCallerMemberName();
14+
15+
Assert.AreEqual(expect, actual);
16+
}
17+
18+
// ReSharper disable once EntityNameCapturedOnly.Local
19+
#pragma warning disable IDE0060
20+
private static string TestCallerMemberName([CallerMemberName] string memberName = null) => memberName;
21+
#pragma warning restore IDE0060
22+
}
23+
}

Tests/NFUnitTestAttributes/NFUnitTestAttributes.nfproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
</PropertyGroup>
2626
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2727
<ItemGroup>
28+
<Compile Include="CallerArgumentExpressionAttributeTests.cs" />
29+
<Compile Include="CallerMemberNameAttributeTests.cs" />
2830
<Compile Include="ConstructorTests.cs" />
2931
<Compile Include="Diagnostics\CodeAnalysis\NullableAttributesTests.cs" />
3032
<Compile Include="Runtime\CompilerServices\CallerArgumentExpressionAttributeTests.cs" />

0 commit comments

Comments
 (0)