Skip to content

Commit

Permalink
Use C# 12 syntax (#740)
Browse files Browse the repository at this point in the history
- Use raw literals for JSON strings.
- use System.Text.Json in README examples instead of Newtonsoft.Json.
- Use collection expressions.
  • Loading branch information
martincostello authored Aug 17, 2024
1 parent 7fb6a9f commit 8b41515
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
48 changes: 23 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public static class ReverseFunction
}

public static Task<int[]> ReverseAsync(int[] values)
{
return Task.FromResult(values.Reverse().ToArray());
}
=> Task.FromResult(values.Reverse().ToArray());
}
```

Expand All @@ -67,10 +65,10 @@ Here's an example using xunit to verify that `ReverseFunction` works as intended

```csharp
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MartinCostello.Testing.AwsLambdaTestServer;
using Newtonsoft.Json;
using Xunit;

namespace MyFunctions;
Expand All @@ -86,8 +84,8 @@ public static class ReverseFunctionTests

await server.StartAsync(cancellationTokenSource.Token);

int[] value = new[] { 1, 2, 3 };
string json = JsonConvert.SerializeObject(value);
int[] value = [1, 2, 3];
string json = JsonSerializer.Serialize(value);

LambdaTestContext context = await server.EnqueueAsync(json);

Expand All @@ -101,9 +99,9 @@ public static class ReverseFunctionTests
Assert.True(response.IsSuccessful);

json = await response.ReadAsStringAsync();
int[] actual = JsonConvert.DeserializeObject<int[]>(json);
int[] actual = JsonSerializer.Deserialize<int[]>(json);

Assert.Equal(new[] { 3, 2, 1 }, actual);
Assert.Equal([3, 2, 1], actual);
}
}
```
Expand Down Expand Up @@ -225,10 +223,10 @@ An example of providing these values from an xunit test is shown below:

```csharp
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MartinCostello.Testing.AwsLambdaTestServer;
using Newtonsoft.Json;
using Xunit;

namespace MyFunctions;
Expand All @@ -244,14 +242,14 @@ public static class ReverseFunctionWithMobileSdkTests

await server.StartAsync(cancellationTokenSource.Token);

int[] value = new[] { 1, 2, 3 };
string json = JsonConvert.SerializeObject(value);
int[] value = [1, 2, 3];
string json = JsonSerializer.Serialize(value);
byte[] content = Encoding.UTF8.GetBytes(json);

var request = new LambdaTestRequest(content)
{
ClientContext = @"{ ""client"": { ""app_title"": ""my-app"" } }",
CognitoIdentity = @"{ ""identityId"": ""my-identity"" }",
ClientContext = """{ "client": { "app_title": "my-app" } }""",
CognitoIdentity = """{ "identityId": "my-identity" }""",
};

LambdaTestContext context = await server.EnqueueAsync(json);
Expand All @@ -266,9 +264,9 @@ public static class ReverseFunctionWithMobileSdkTests
Assert.True(response.IsSuccessful);

json = await response.ReadAsStringAsync();
int[] actual = JsonConvert.DeserializeObject<int[]>(json);
int[] actual = JsonSerializer.Deserialize<int[]>(json);

Assert.Equal(new[] { 3, 2, 1 }, actual);
Assert.Equal([3, 2, 1], actual);
}
}
```
Expand All @@ -285,10 +283,10 @@ An example of this customisation for an xunit test is shown below:

```csharp
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MartinCostello.Testing.AwsLambdaTestServer;
using Newtonsoft.Json;
using Xunit;

namespace MyFunctions;
Expand All @@ -311,8 +309,8 @@ public static class ReverseFunctionWithCustomOptionsTests

await server.StartAsync(cancellationTokenSource.Token);

int[] value = new[] { 1, 2, 3 };
string json = JsonConvert.SerializeObject(value);
int[] value = [1, 2, 3];
string json = JsonSerializer.Serialize(value);

LambdaTestContext context = await server.EnqueueAsync(json);

Expand All @@ -326,9 +324,9 @@ public static class ReverseFunctionWithCustomOptionsTests
Assert.True(response.IsSuccessful);

json = await response.ReadAsStringAsync();
int[] actual = JsonConvert.DeserializeObject<int[]>(json);
int[] actual = JsonSerializer.Deserialize<int[]>(json);

Assert.Equal(new[] { 3, 2, 1 }, actual);
Assert.Equal([3, 2, 1], actual);
}
}
```
Expand All @@ -341,13 +339,13 @@ Here's an example of configuring the test server to route its logs to xunit usin

```csharp
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MartinCostello.Logging.XUnit;
using MartinCostello.Testing.AwsLambdaTestServer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -374,8 +372,8 @@ public class ReverseFunctionWithLoggingTests : ITestOutputHelperAccessor

await server.StartAsync(cancellationTokenSource.Token);

int[] value = new[] { 1, 2, 3 };
string json = JsonConvert.SerializeObject(value);
int[] value = [1, 2, 3];
string json = JsonSerializer.Serialize(value);

LambdaTestContext context = await server.EnqueueAsync(json);

Expand All @@ -389,9 +387,9 @@ public class ReverseFunctionWithLoggingTests : ITestOutputHelperAccessor
Assert.True(response.IsSuccessful);

json = await response.ReadAsStringAsync();
int[] actual = JsonConvert.DeserializeObject<int[]>(json);
int[] actual = JsonSerializer.Deserialize<int[]>(json);

Assert.Equal(new[] { 3, 2, 1 }, actual);
Assert.Equal([3, 2, 1], actual);
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions tests/AwsLambdaTestServer.Tests/HttpLambdaTestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void Configure(IServiceCollection services)

cts.CancelAfter(TimeSpan.FromSeconds(2));

var context = await server.EnqueueAsync(@"{""Values"": [ 1, 2, 3 ]}");
var context = await server.EnqueueAsync("""{"Values": [ 1, 2, 3 ]}""");

_ = Task.Run(async () =>
{
Expand All @@ -53,7 +53,7 @@ void Configure(IServiceCollection services)
response!.IsSuccessful.ShouldBeTrue();
response.Content.ShouldNotBeNull();
response.Duration.ShouldBeGreaterThan(TimeSpan.Zero);
Encoding.UTF8.GetString(response.Content).ShouldBe(@"{""Sum"":6}");
Encoding.UTF8.GetString(response.Content).ShouldBe("""{"Sum":6}""");
}

[Fact]
Expand All @@ -70,7 +70,7 @@ void Configure(IServiceCollection services)

cts.CancelAfter(TimeSpan.FromSeconds(2));

var context = await server.EnqueueAsync(@"{""Values"": null}");
var context = await server.EnqueueAsync("""{"Values": null}""");

_ = Task.Run(async () =>
{
Expand Down
14 changes: 7 additions & 7 deletions tests/AwsLambdaTestServer.Tests/LambdaTestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public async Task Function_Can_Process_Request()

await server.StartAsync(cts.Token);

var context = await server.EnqueueAsync(@"{""Values"": [ 1, 2, 3 ]}");
var context = await server.EnqueueAsync("""{"Values": [ 1, 2, 3 ]}""");

CancelWhenResponseAvailable(context, cts);

Expand All @@ -189,7 +189,7 @@ public async Task Function_Can_Process_Request()
response!.IsSuccessful.ShouldBeTrue();
response.Content.ShouldNotBeNull();
response.Duration.ShouldBeGreaterThan(TimeSpan.Zero);
Encoding.UTF8.GetString(response.Content).ShouldBe(@"{""Sum"":6}");
Encoding.UTF8.GetString(response.Content).ShouldBe("""{"Sum":6}""");
}

[Fact]
Expand All @@ -201,7 +201,7 @@ public async Task Function_Can_Process_Request_With_Mobile_Sdk_Headers()

await server.StartAsync(cts.Token);

byte[] content = Encoding.UTF8.GetBytes(@"{""Values"": [ 1, 2, 3 ]}");
byte[] content = Encoding.UTF8.GetBytes("""{"Values": [ 1, 2, 3 ]}""");

var request = new LambdaTestRequest(content)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ public async Task Function_Can_Handle_Failed_Request()

await server.StartAsync(cts.Token);

var context = await server.EnqueueAsync(@"{""Values"": null}");
var context = await server.EnqueueAsync("""{"Values": null}""");

CancelWhenResponseAvailable(context, cts);

Expand All @@ -260,7 +260,7 @@ public async Task Function_Can_Handle_Failed_Initialization()

await server.StartAsync(cts.Token);

var context = await server.EnqueueAsync(@"{""Values"": null}");
var context = await server.EnqueueAsync("""{"Values": null}""");

CancelWhenResponseAvailable(context, cts);

Expand Down Expand Up @@ -376,8 +376,8 @@ public async Task Can_Use_Custom_Function_Variables()

var request = new LambdaTestRequest([], "my-request-id")
{
ClientContext = @"{""client"":{""app_title"":""my-app""}}",
CognitoIdentity = @"{""cognitoIdentityId"":""my-identity""}",
ClientContext = """{"client":{"app_title":"my-app"}}""",
CognitoIdentity = """{"cognitoIdentityId":"my-identity"}""",
};

var context = await server.EnqueueAsync(request);
Expand Down

0 comments on commit 8b41515

Please sign in to comment.