-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for serialization of expandable fields
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/StripeTests/Infrastructure/ExpandableSerializationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
namespace StripeTests | ||
{ | ||
using Newtonsoft.Json; | ||
using Stripe; | ||
using Stripe.Infrastructure; | ||
using Xunit; | ||
|
||
public class ExpandableSerializationTest : BaseStripeTest | ||
{ | ||
[Fact] | ||
public void SerializeNotExpanded() | ||
{ | ||
var obj = new TestTopLevelObject | ||
{ | ||
NestedId = "id_not_expanded", | ||
Nested = null, | ||
}; | ||
|
||
var expected = "{\n \"nested\": \"id_not_expanded\"\n}"; | ||
Assert.Equal(expected, obj.ToJson().Replace("\r\n", "\n")); | ||
} | ||
|
||
[Fact] | ||
public void SerializeExpanded() | ||
{ | ||
var nested = new TestNestedObject | ||
{ | ||
Id = "id_expanded", | ||
Bar = 42, | ||
}; | ||
var obj = new TestTopLevelObject | ||
{ | ||
NestedId = nested.Id, | ||
Nested = nested, | ||
}; | ||
|
||
var expected = | ||
"{\n \"nested\": {\n \"id\": \"id_expanded\",\n \"bar\": 42\n }\n}"; | ||
Assert.Equal(expected, obj.ToJson().Replace("\r\n", "\n")); | ||
} | ||
|
||
[Fact] | ||
public void SerializeNull() | ||
{ | ||
var obj = new TestTopLevelObject | ||
{ | ||
NestedId = null, | ||
Nested = null, | ||
}; | ||
|
||
var expected = "{\n \"nested\": null\n}"; | ||
Assert.Equal(expected, obj.ToJson().Replace("\r\n", "\n")); | ||
} | ||
|
||
private class TestNestedObject : StripeEntity, IHasId | ||
{ | ||
[JsonProperty("id")] | ||
public string Id { get; set; } | ||
|
||
[JsonProperty("bar")] | ||
public int Bar { get; set; } | ||
} | ||
|
||
private class TestTopLevelObject : StripeEntity | ||
{ | ||
[JsonIgnore] | ||
public string NestedId { get; set; } | ||
|
||
[JsonIgnore] | ||
public TestNestedObject Nested { get; set; } | ||
|
||
[JsonProperty("nested")] | ||
internal object InternalNested | ||
{ | ||
get | ||
{ | ||
return this.Nested ?? (object)this.NestedId; | ||
} | ||
|
||
set | ||
{ | ||
StringOrObject<TestNestedObject>.Map(value, s => this.NestedId = s, o => this.Nested = o); | ||
} | ||
} | ||
} | ||
} | ||
} |