From 3121fc80d58406865b230a4a1d6f7fa1b5efa2c3 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 10 Apr 2024 14:47:09 -0600 Subject: [PATCH] [chore] Display object type, ID when converting object to string (#556) - Override `ToString` for EasyPost objects to display object type and optional ID - Add unit test for string representation of object --- CHANGELOG.md | 4 +++- .../baseTests/EasyPostObjectTests.cs | 19 +++++++++++++++++++ EasyPost/_base/EasyPostObject.cs | 3 +++ EasyPost/_base/EphemeralEasyPostObject.cs | 3 +++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69694ea5..2ec9c4a6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,10 @@ ## Next Release -- Fix payment method funding and deletion failures due to undetermined payment method type - Add Refund function in insurance service for requesting a refund for standalone insurance. +- Fix payment method funding and deletion failures due to undetermined payment method type +- Fix `ToString` method for `EasyPostObject`- and `EphemeralEasyPostObject`-based objects to return object type and + optional ID ## v6.2.1 (2024-03-18) diff --git a/EasyPost.Tests/baseTests/EasyPostObjectTests.cs b/EasyPost.Tests/baseTests/EasyPostObjectTests.cs index 1eaf4e771..8754b814b 100644 --- a/EasyPost.Tests/baseTests/EasyPostObjectTests.cs +++ b/EasyPost.Tests/baseTests/EasyPostObjectTests.cs @@ -80,5 +80,24 @@ public void TestEquals() Assert.False(apiKey == null); Assert.False(null == apiKey); } + + [Fact] + [Testing.Function] + public void TestToString() + { + const string @object = "the_object_type"; + const string id = "the_objet_id"; + + Address address = new() + { + Id = id, + Object = @object + }; + + const string expected = $"{@object} {id}"; + string actual = address.ToString(); + + Assert.Equal(expected, actual); + } } } diff --git a/EasyPost/_base/EasyPostObject.cs b/EasyPost/_base/EasyPostObject.cs index 5259c72e1..7304fccee 100644 --- a/EasyPost/_base/EasyPostObject.cs +++ b/EasyPost/_base/EasyPostObject.cs @@ -45,6 +45,9 @@ public abstract class EasyPostObject : EphemeralEasyPostObject /// A dictionary representation of this object's properties. public virtual Dictionary AsDictionary() => JsonConvert.DeserializeObject>(AsJson())!; + /// + public override string ToString() => $"{Object!} {Id!}"; + /// public override bool Equals(object? obj) => GetType() == obj?.GetType() && GetHashCode() == ((EasyPostObject)obj).GetHashCode(); diff --git a/EasyPost/_base/EphemeralEasyPostObject.cs b/EasyPost/_base/EphemeralEasyPostObject.cs index 3356eb160..3e3e0fc9c 100644 --- a/EasyPost/_base/EphemeralEasyPostObject.cs +++ b/EasyPost/_base/EphemeralEasyPostObject.cs @@ -26,6 +26,9 @@ public abstract class EphemeralEasyPostObject : IEasyPostObject #endregion + /// + public override string ToString() => Object!; + /// public override bool Equals(object? obj) => GetType() == obj?.GetType() && GetHashCode() == ((EphemeralEasyPostObject)obj).GetHashCode();