Skip to content

Commit

Permalink
Add a test for serialization of expandable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Dec 17, 2018
1 parent ee9dd90 commit dfe52b3
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/StripeTests/Infrastructure/ExpandableSerializationTest.cs
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);
}
}
}
}
}

0 comments on commit dfe52b3

Please sign in to comment.