Skip to content

Commit

Permalink
[chore] Display object type, ID when converting object to string (#556)
Browse files Browse the repository at this point in the history
- Override `ToString` for EasyPost objects to display object type and optional ID
- Add unit test for string representation of object
  • Loading branch information
nwithan8 authored Apr 10, 2024
1 parent 4765175 commit 3121fc8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions EasyPost.Tests/baseTests/EasyPostObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions EasyPost/_base/EasyPostObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public abstract class EasyPostObject : EphemeralEasyPostObject
/// <returns>A dictionary representation of this object's properties.</returns>
public virtual Dictionary<string, object> AsDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(AsJson())!;

/// <inheritdoc />
public override string ToString() => $"{Object!} {Id!}";

/// <inheritdoc />
public override bool Equals(object? obj) => GetType() == obj?.GetType() && GetHashCode() == ((EasyPostObject)obj).GetHashCode();

Expand Down
3 changes: 3 additions & 0 deletions EasyPost/_base/EphemeralEasyPostObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public abstract class EphemeralEasyPostObject : IEasyPostObject

#endregion

/// <inheritdoc />
public override string ToString() => Object!;

/// <inheritdoc />
public override bool Equals(object? obj) => GetType() == obj?.GetType() && GetHashCode() == ((EphemeralEasyPostObject)obj).GetHashCode();

Expand Down

0 comments on commit 3121fc8

Please sign in to comment.