From fe2adce5174433fb9254d66b4ad81e16ef93a002 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 5 Dec 2023 09:32:22 -0600 Subject: [PATCH 1/3] attempt to get above coverage threshold --- test/Flurl.Test/Http/NewtonsoftTests.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/test/Flurl.Test/Http/NewtonsoftTests.cs b/test/Flurl.Test/Http/NewtonsoftTests.cs index 557273ad..7c351c18 100644 --- a/test/Flurl.Test/Http/NewtonsoftTests.cs +++ b/test/Flurl.Test/Http/NewtonsoftTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using NUnit.Framework; using Flurl.Http; using Flurl.Http.Newtonsoft; @@ -19,30 +20,42 @@ public class NewtonsoftGetTests : GetTests protected override HttpTest CreateHttpTest() => base.CreateHttpTest() .WithSettings(settings => settings.JsonSerializer = new NewtonsoftJsonSerializer()); - [Test] - public async Task can_get_dynamic() { + [TestCaseSource(nameof(GetJson))] + public async Task can_get_dynamic(Task getJson) { HttpTest.RespondWithJson(new { id = 1, name = "Frank" }); - var data = await "http://some-api.com".GetJsonAsync(); + var data = await getJson; Assert.AreEqual(1, data.id); Assert.AreEqual("Frank", data.name); } - [Test] - public async Task can_get_dynamic_list() { + [TestCaseSource(nameof(GetJsonList))] + public async Task can_get_dynamic_list(Task> getList) { HttpTest.RespondWithJson(new[] { new { id = 1, name = "Frank" }, new { id = 2, name = "Claire" } }); - var data = await "http://some-api.com".GetJsonListAsync(); + var data = await getList; Assert.AreEqual(1, data[0].id); Assert.AreEqual("Frank", data[0].name); Assert.AreEqual(2, data[1].id); Assert.AreEqual("Claire", data[1].name); } + + private static IEnumerable> GetJson() { + yield return "http://some-api.com".GetJsonAsync(); + yield return new Url("http://some-api.com").GetJsonAsync(); + yield return new Uri("http://some-api.com").GetJsonAsync(); + } + + private static IEnumerable>> GetJsonList() { + yield return "http://some-api.com".GetJsonListAsync(); + yield return new Url("http://some-api.com").GetJsonListAsync(); + yield return new Uri("http://some-api.com").GetJsonListAsync(); + } } [TestFixture, Parallelizable] From 68ec9d910c51e7bdc73be244b0b2930e359e5b42 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 5 Dec 2023 09:43:13 -0600 Subject: [PATCH 2/3] oops..GeneratedExtensions are excluded so not the coverage problem --- test/Flurl.Test/Http/NewtonsoftTests.cs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/test/Flurl.Test/Http/NewtonsoftTests.cs b/test/Flurl.Test/Http/NewtonsoftTests.cs index 7c351c18..557273ad 100644 --- a/test/Flurl.Test/Http/NewtonsoftTests.cs +++ b/test/Flurl.Test/Http/NewtonsoftTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using NUnit.Framework; using Flurl.Http; using Flurl.Http.Newtonsoft; @@ -20,42 +19,30 @@ public class NewtonsoftGetTests : GetTests protected override HttpTest CreateHttpTest() => base.CreateHttpTest() .WithSettings(settings => settings.JsonSerializer = new NewtonsoftJsonSerializer()); - [TestCaseSource(nameof(GetJson))] - public async Task can_get_dynamic(Task getJson) { + [Test] + public async Task can_get_dynamic() { HttpTest.RespondWithJson(new { id = 1, name = "Frank" }); - var data = await getJson; + var data = await "http://some-api.com".GetJsonAsync(); Assert.AreEqual(1, data.id); Assert.AreEqual("Frank", data.name); } - [TestCaseSource(nameof(GetJsonList))] - public async Task can_get_dynamic_list(Task> getList) { + [Test] + public async Task can_get_dynamic_list() { HttpTest.RespondWithJson(new[] { new { id = 1, name = "Frank" }, new { id = 2, name = "Claire" } }); - var data = await getList; + var data = await "http://some-api.com".GetJsonListAsync(); Assert.AreEqual(1, data[0].id); Assert.AreEqual("Frank", data[0].name); Assert.AreEqual(2, data[1].id); Assert.AreEqual("Claire", data[1].name); } - - private static IEnumerable> GetJson() { - yield return "http://some-api.com".GetJsonAsync(); - yield return new Url("http://some-api.com").GetJsonAsync(); - yield return new Uri("http://some-api.com").GetJsonAsync(); - } - - private static IEnumerable>> GetJsonList() { - yield return "http://some-api.com".GetJsonListAsync(); - yield return new Url("http://some-api.com").GetJsonListAsync(); - yield return new Uri("http://some-api.com").GetJsonListAsync(); - } } [TestFixture, Parallelizable] From 54a461c6736be87402109289cefc2bc15ad13581 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 5 Dec 2023 10:06:12 -0600 Subject: [PATCH 3/3] coverage threshold should be met now --- .github/workflows/ci.yml | 2 +- test/Flurl.Test/Http/NewtonsoftTests.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a6409b7..369b00df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,4 +26,4 @@ jobs: run: dotnet build -c Release --no-restore - name: Test - run: dotnet test --no-build -c Release /p:CollectCoverage=true /p:Threshold=80 /p:Include=\"[Flurl]*,[Flurl.Http]*\" /p:Exclude="[*]*.GeneratedExtensions" + run: dotnet test --no-build -c Release /p:CollectCoverage=true /p:Threshold=80 /p:Include=\"[Flurl]*,[Flurl.Http]*,[Flurl.Http.Newtonsoft]*\" /p:Exclude="[*]*.GeneratedExtensions" diff --git a/test/Flurl.Test/Http/NewtonsoftTests.cs b/test/Flurl.Test/Http/NewtonsoftTests.cs index 557273ad..b0f15696 100644 --- a/test/Flurl.Test/Http/NewtonsoftTests.cs +++ b/test/Flurl.Test/Http/NewtonsoftTests.cs @@ -43,6 +43,19 @@ public async Task can_get_dynamic_list() { Assert.AreEqual(2, data[1].id); Assert.AreEqual("Claire", data[1].name); } + + [Test] + public async Task null_response_returns_null_dynamic() { + // a null IFlurlResponse is likely not even possible in real-world scenarios, but we have + // null checks that need to be tested in order to meet our coverage threshold for this lib. + Task resp = Task.FromResult(null); + + var json = await resp.ReceiveJson(); + Assert.IsNull(json); + + var list = await resp.ReceiveJsonList(); + Assert.IsNull(list); + } } [TestFixture, Parallelizable]