Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve test coverage: OpenTelemetry.Api CallerArgumentExpressionAttribute #3362

Merged
merged 20 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3b2c203
tests for CallerArgumentExpressionAttribute
TimothyMothra Jun 13, 2022
7f41b90
move to GuardTest
TimothyMothra Jun 14, 2022
c53b81f
fix build errors
TimothyMothra Jun 20, 2022
813e69d
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jun 21, 2022
ef3a7ec
fix indentation
TimothyMothra Jun 23, 2022
de0e05f
Merge branch '3353_CallerArgumentExpressionAttribute' of https://gith…
TimothyMothra Jun 23, 2022
c12777b
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jun 29, 2022
554f3b4
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 5, 2022
6051e5f
fix preprocessor
TimothyMothra Jul 5, 2022
198816b
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 11, 2022
8e85b14
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 15, 2022
a88af80
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 18, 2022
7c44451
move CallerArgumentExpressionAttribute inside Guard.
TimothyMothra Jul 22, 2022
f497750
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 22, 2022
2de9fb8
Revert "move CallerArgumentExpressionAttribute inside Guard."
TimothyMothra Jul 22, 2022
e67bc58
Merge branch '3353_CallerArgumentExpressionAttribute' of https://gith…
TimothyMothra Jul 22, 2022
d8b6aa9
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 22, 2022
d99e099
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 25, 2022
edb1d4d
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
cijothomas Jul 29, 2022
662bbf7
Merge branch 'main' into 3353_CallerArgumentExpressionAttribute
TimothyMothra Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Api/Internal/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace System.Runtime.CompilerServices
/// <summary>
/// Allows capturing of the expressions passed to a method.
/// </summary>
/// <remarks>
/// Borrowed from: <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CallerArgumentExpressionAttribute.cs"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name
Expand Down
43 changes: 34 additions & 9 deletions test/OpenTelemetry.Tests/Internal/GuardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@
// </copyright>

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using Xunit;

namespace OpenTelemetry.Internal.Tests
{
#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name
public class Thing
#pragma warning restore SA1649 // File name should match first type name
#pragma warning restore SA1402 // File may only contain a single type
{
public string Bar { get; set; }
}

public class GuardTest
{
[Fact]
Expand Down Expand Up @@ -171,5 +163,38 @@ public void ZeroTest()
Assert.Contains("Must not be zero", ex1.Message);
Assert.Equal("0", ex1.ParamName);
}

public class Thing
{
public string Bar { get; set; }
}

#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Borrowed from: <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System/Runtime/CompilerServices/CallerArgumentExpressionAttributeTests.cs"/>.
/// </summary>
public class CallerArgumentExpressionAttributeTests
{
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("paramName")]
public static void Ctor_ParameterName_Roundtrip(string value)
{
var caea = new CallerArgumentExpressionAttribute(value);
Assert.Equal(value, caea.ParameterName);
}

[Fact]
public static void BasicTest()
{
Assert.Equal("\"hello\"", GetValue("hello"));
Assert.Equal("3 + 2", GetValue(3 + 2));
Assert.Equal("new object()", GetValue(new object()));
}

private static string GetValue(object argument, [CallerArgumentExpression("argument")] string expr = null) => expr;
}
#endif
}
}