From e1f4f70d63b69041ad61fff88a350e67bf16a0d2 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Wed, 25 Jan 2017 16:52:04 -0800 Subject: [PATCH 1/2] Add tests for "op" Replace with null checks --- .../NestedObjectTests.cs | 23 +++++++++++++++ .../ObjectAdapterTests.cs | 20 +++++++++++++ .../SimpleDTOWithNestedDTOWithNullCheck.cs | 15 ++++++++++ .../SimpleDTOWithNullCheck.cs | 29 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs create mode 100644 test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs index 345ffe2..cc2e990 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/NestedObjectTests.cs @@ -907,6 +907,29 @@ public void Replace() Assert.Equal(12, doc.SimpleDTO.DecimalValue); } + [Fact] + public void Replace_DTOWithNullCheck() + { + // Arrange + var doc = new SimpleDTOWithNestedDTOWithNullCheck() + { + SimpleDTOWithNullCheck = new SimpleDTOWithNullCheck() + { + StringProperty = "A" + } + }; + + // create patch + var patchDoc = new JsonPatchDocument(); + patchDoc.Replace(o => o.SimpleDTOWithNullCheck.StringProperty, "B"); + + // Act + patchDoc.ApplyTo(doc); + + // Assert + Assert.Equal("B", doc.SimpleDTOWithNullCheck.StringProperty); + } + [Fact] public void ReplaceWithSerialization() { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs index 5898311..d14daad 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/ObjectAdapterTests.cs @@ -648,6 +648,26 @@ public void Replace() Assert.Equal(12, doc.DecimalValue); } + [Fact] + public void Replace_DTOWithNullCheck() + { + // Arrange + var doc = new SimpleDTOWithNullCheck() + { + StringProperty = "A", + }; + + // create patch + var patchDoc = new JsonPatchDocument(); + patchDoc.Replace(o => o.StringProperty, "B"); + + // Act + patchDoc.ApplyTo(doc); + + // Assert + Assert.Equal("B", doc.StringProperty); + } + [Fact] public void ReplaceWithSerialization() { diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs new file mode 100644 index 0000000..308f23b --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNestedDTOWithNullCheck.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.JsonPatch +{ + public class SimpleDTOWithNestedDTOWithNullCheck + { + public SimpleDTOWithNullCheck SimpleDTOWithNullCheck { get; set; } + + public SimpleDTOWithNestedDTOWithNullCheck() + { + SimpleDTOWithNullCheck = new SimpleDTOWithNullCheck(); + } + } +} diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs new file mode 100644 index 0000000..3bf34f9 --- /dev/null +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs @@ -0,0 +1,29 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNetCore.JsonPatch +{ + public class SimpleDTOWithNullCheck + { + string stringProperty; + + public string StringProperty + { + get + { + return stringProperty; + } + + set + { + if (value == null) + { + throw new ArgumentNullException(); + } + stringProperty = value; + } + } + } +} From 6a7e4bbe5458ca6793f4990f5f444a39775d93ff Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 26 Jan 2017 11:25:09 -0800 Subject: [PATCH 2/2] Formatting --- .../SimpleDTOWithNullCheck.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs index 3bf34f9..d2a5fe5 100644 --- a/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs +++ b/test/Microsoft.AspNetCore.JsonPatch.Test/SimpleDTOWithNullCheck.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.JsonPatch { public class SimpleDTOWithNullCheck { - string stringProperty; + private string stringProperty; public string StringProperty { @@ -22,6 +22,7 @@ public string StringProperty { throw new ArgumentNullException(); } + stringProperty = value; } }