forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractByInterfaceTests.cs
More file actions
98 lines (85 loc) · 3.2 KB
/
ContractByInterfaceTests.cs
File metadata and controls
98 lines (85 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.JsonTests
{
/// <summary>
/// Service Bus messaging works best if processes can share interface message contracts
/// but not have to share concrete types.
/// </summary>
[TestFixture]
public class ContractByInterfaceTests
{
[Test]
public void Prefer_interfaces_should_work_on_top_level_object_using_extension_method()
{
using (JsConfig.With(preferInterfaces:true))
{
var json = new Concrete("boo", 1).ToJson();
Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
}
}
[Test]
public void Should_be_able_to_serialise_based_on_an_interface()
{
using (JsConfig.With(preferInterfaces: true))
{
IContract myConcrete = new Concrete("boo", 1);
var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
Console.WriteLine(json);
Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.IContract, ServiceStack.Text.Tests\""));
}
}
[Test]
public void Should_not_use_interface_type_if_concrete_specified()
{
using (JsConfig.With(preferInterfaces: false))
{
IContract myConcrete = new Concrete("boo", 1);
var json = JsonSerializer.SerializeToString(myConcrete, typeof(IContract));
Console.WriteLine(json);
Assert.That(json, Is.StringContaining("\"ServiceStack.Text.Tests.JsonTests.Concrete, ServiceStack.Text.Tests\""));
}
}
[Test]
public void Should_be_able_to_deserialise_based_on_an_interface_with_no_concrete()
{
using (JsConfig.With(preferInterfaces: true))
{
var json = new Concrete("boo", 42).ToJson();
// break the typing so we have to use the dynamic implementation
json = json.Replace("ServiceStack.Text.Tests.JsonTests.IContract", "ServiceStack.Text.Tests.JsonTests.IIdenticalContract");
var result = JsonSerializer.DeserializeFromString<IIdenticalContract>(json);
Assert.That(result.StringValue, Is.EqualTo("boo"));
Assert.That(result.ChildProp.IntValue, Is.EqualTo(42));
}
}
}
class Concrete : IContract
{
public Concrete(string boo, int i)
{
StringValue = boo;
ChildProp = new ConcreteChild { IntValue = i };
}
public string StringValue { get; set; }
public IChildInterface ChildProp { get; set; }
}
class ConcreteChild : IChildInterface
{
public int IntValue { get; set; }
}
public interface IChildInterface
{
int IntValue { get; set; }
}
public interface IContract
{
string StringValue { get; set; }
IChildInterface ChildProp { get; set; }
}
public interface IIdenticalContract
{
string StringValue { get; set; }
IChildInterface ChildProp { get; set; }
}
}