Skip to content

Commit

Permalink
Add comments to binding gen tests for invocations on new lines (#91237)
Browse files Browse the repository at this point in the history
* Add comments to binding gen tests for invocations on new lines

* Address feedback & test static method call syntax

* Reorganize and comment the newline/whitespace scenarios

* Reorganize and comment the newline/whitespace scenarios for ConfigurationExtensions

---------

Co-authored-by: Jeff Handley <[email protected]>
  • Loading branch information
layomia and jeffhandley authored Aug 30, 2023
1 parent 526e6d3 commit a16670a
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,246 @@ namespace Microsoft.Extensions.SourceGeneration.Configuration.Binder.Tests
{
public partial class ConfigurationBinderTests : ConfigurationBinderTestsBase
{
// These are regression tests for https://github.com/dotnet/runtime/issues/90851
// Source Generator Interceptors rely on identifying an accurate invocation
// source location (line and character positions). These tests cover newline
// and whitespace scenarios to ensure the interceptors get wired up correctly.

[Fact]
public void GeneratorHandlesInvocationsOnNewline()
public void TestBindingInvocationsWithNewlines_GetMethodTypeArg()
{
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");

GeolocationRecord record = configuration.Get<
GeolocationRecord
>();
Verify();
// Newline between the configuration instance and the binding invocation (with the dot on the first line)
GeolocationRecord record1 = (GeolocationRecord)configuration.
Get(typeof(GeolocationRecord), _ => { });

AssertRecordIsBound(record1, 1, 2);

record = (GeolocationRecord)configuration
// Newline between the configuration instance and the binding invocation (with the dot on the second line)
GeolocationRecord record2 = (GeolocationRecord)configuration
.Get(typeof(GeolocationRecord), _ => { });
Verify();

TestHelpers
.GetConfigurationFromJsonString(@"{""Longitude"":3,""Latitude"":4}")
.Bind(record);
Verify(3, 4);
AssertRecordIsBound(record2, 1, 2);

int lat = configuration
.GetValue<int>("Latitude");
Assert.Equal(2, lat);
// Newlines between the instance, the invocation, and the arguments
GeolocationRecord record3 = (GeolocationRecord)configuration
.Get(
typeof(GeolocationRecord),
_ => { }
);

AssertRecordIsBound(record3, 1, 2);

// Newlines before and after the instance (with the dot on the first line)
GeolocationRecord record4 = (GeolocationRecord)
configuration.
Get(typeof(GeolocationRecord), _ => { });

AssertRecordIsBound(record4, 1, 2);

// Newlines before and after the instance (with the dot on the second line)
GeolocationRecord record5 = (GeolocationRecord)
configuration
.Get(typeof(GeolocationRecord), _ => { });

AssertRecordIsBound(record5, 1, 2);

// Newlines in every place possible
GeolocationRecord
record6
=
(
GeolocationRecord
)
configuration
.
Get
(
typeof
(
GeolocationRecord
)
,
_
=>
{
}
)
;

AssertRecordIsBound(record6, 1, 2);
}

record = configuration.Get
[Fact]
public void TestBindingInvocationsWithNewlines_GetMethodGeneric()
{
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");

// Newline between the invocation method name and the generic type argument
GeolocationRecord record1 = configuration.Get
<GeolocationRecord>();
Verify();

record = (GeolocationRecord)configuration
.Get(
typeof(GeolocationRecord), _ =>
{ });
Verify();
AssertRecordIsBound(record1, 1, 2);

// Newlines on either side of the generic type argument
GeolocationRecord record2 = configuration.Get<
GeolocationRecord
>();

AssertRecordIsBound(record2, 1, 2);

// Newlines in every place possible
GeolocationRecord
record3
=
configuration
.
Get
<
GeolocationRecord
>
()
;

AssertRecordIsBound(record3, 1, 2);
}

[Fact]
public void TestBindingInvocationsWithNewlines_BindExtensionMethod()
{
// Newline between the configuration instance and the extension method invocation
GeolocationRecord record1 = new GeolocationRecord();
TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}")
.Bind(record1);

AssertRecordIsBound(record1, 1, 2);

// Newlines between the method that returns the instance and the extension method invocation
GeolocationRecord record2 = new GeolocationRecord();
TestHelpers
.GetConfigurationFromJsonString(@"{""Longitude"":3,
""Latitude"":4}
")
.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}")
.Bind(record2);

AssertRecordIsBound(record2, 1, 2);

// Newlines within the argument to the method returning the configuration and around the extension method argument
GeolocationRecord record3 = new GeolocationRecord();
TestHelpers
.GetConfigurationFromJsonString(@"{""Longitude"":1,
""Latitude"":2}
")
.Bind(
record
record3
);
Verify(3, 4);

long latLong = configuration
.GetValue<
AssertRecordIsBound(record3, 1, 2);

// Newlines in every place possible
GeolocationRecord record4 = new GeolocationRecord();
TestHelpers
.
GetConfigurationFromJsonString
(
@"{""Longitude"":1, ""Latitude"":2}"
)
.
Bind
(
record4
)
;

AssertRecordIsBound(record4, 1, 2);
}

[Fact]
public void TestBindingInvocationsWithNewlines_BindStaticMethod()
{
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");

// Newline between the class and the static method invocation (with the dot on the first line)
GeolocationRecord record1 = new GeolocationRecord();
ConfigurationBinder.
Bind(configuration, record1);

// Newline between the class and the static method invocation (with the dot on the second line)
GeolocationRecord record2 = new GeolocationRecord();
ConfigurationBinder
.Bind(configuration, record2);

AssertRecordIsBound(record2, 1, 2);

// Newline before the arguments
GeolocationRecord record3 = new GeolocationRecord();
ConfigurationBinder.Bind(
configuration, record3);

AssertRecordIsBound(record3, 1, 2);

// Newlines in every place possible
GeolocationRecord record4 = new GeolocationRecord();
ConfigurationBinder
.
Bind
(
configuration
,
record4
)
;

AssertRecordIsBound(record4, 1, 2);
}

[Fact]
public void TestBindingInvocationsWithNewlines_GetValueMethod()
{
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");

// Newline between the configuration instance and the binding invocation (with the dot on the first line)
int lat1 = configuration.
GetValue<int>("Latitude");

Assert.Equal(2, lat1);

// Newline between the configuration instance and the binding invocation (with the dot on the second line)
int lat2 = configuration
.GetValue<int>("Latitude");

Assert.Equal(2, lat2);

// Newlines in every place possible
long
lat3
=
configuration
.
GetValue
<
int
>
(
"Latitude"
)
;
Assert.Equal(2, lat3);

// Newlines and pragmas wrapped around the generic type argument
long lat4 = configuration.GetValue<
#if DEBUG
int
#else
long
#endif
>
("Latitude")
;
Assert.Equal(2, lat);

record = (GeolocationRecord)configuration.
Get(typeof(GeolocationRecord), _ => { });
Verify();
>("Latitude");

record = (GeolocationRecord)
configuration.
Get(typeof(GeolocationRecord), _ => { });
Verify();

record = (GeolocationRecord)
configuration
.Get(typeof(GeolocationRecord), _ => { });
Verify();
Assert.Equal(2, lat4);
}

void Verify(int longitude = 1, int latitude = 2)
{
Assert.Equal(longitude, record.Longitude);
Assert.Equal(latitude, record.Latitude);
}
private static void AssertRecordIsBound(GeolocationRecord record, int longitude, int latitude)
{
Assert.Equal((longitude, latitude), (record.Longitude, record.Latitude));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.Extensions.Options.ConfigurationExtensions.Tests
{
public class ConfigurationExtensionsTests
public partial class ConfigurationExtensionsTests
{
private static IConfiguration s_emptyConfig { get; } = new ConfigurationBuilder().Build();

Expand Down
Loading

0 comments on commit a16670a

Please sign in to comment.