-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Serialization.cs
117 lines (109 loc) · 5.27 KB
/
Serialization.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
namespace Towel_Testing;
[TestClass]
public class Serialization_Testing
{
internal class ExpectedException : Exception { }
public static decimal StaticFunction(int a, string? b, object? c) => throw new ExpectedException();
public static void StaticAction(int a, string? b, object? c) => throw new ExpectedException();
#pragma warning disable CA1822 // Mark members as static
public void NonStaticMethod() => throw new ExpectedException();
#pragma warning restore CA1822 // Mark members as static
[TestMethod]
public void StaticDelegateToXml_Testing()
{
{ // Should Succeed
Func<int, string?, object?, decimal> functionToSerialize = StaticFunction;
string serialization = Serialization.StaticDelegateToXml(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
}
{ // Should Succeed
Action<int, string?, object?> functionToSerialize = StaticAction;
string serialization = Serialization.StaticDelegateToXml(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
}
{ // Should Fail (Non-Static)
Action nonStaticDelegate = NonStaticMethod;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToXml(nonStaticDelegate));
}
{ // Should Fail (Local Function)
Exception notImplemented = new NotImplementedException();
void LocalFunctionToSerializeFail() => throw notImplemented;
Action nonStaticDelegate = LocalFunctionToSerializeFail;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToXml(nonStaticDelegate));
}
{ // Should Fail (Static Local Function)
static void StaticLocalFunctionToSerialize() => throw new NotImplementedException();
Action nonStaticDelegate = StaticLocalFunctionToSerialize;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToXml(nonStaticDelegate));
}
}
[TestMethod]
public void StaticDelegateFromXml_Testing()
{
{ // Should Succeed
Func<int, string?, object?, decimal> functionToSerialize = StaticFunction;
string serialization = Serialization.StaticDelegateToXml(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
var deserialization = Serialization.StaticDelegateFromXml<Func<int, string?, object?, decimal>>(serialization);
Assert.IsFalse(deserialization is null);
Assert.ThrowsException<ExpectedException>(() => deserialization!(default, default, default));
}
{ // Should Succeed
Action<int, string?, object?> actionToSerialize = StaticAction;
string serialization = Serialization.StaticDelegateToXml(actionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
var deserialization = Serialization.StaticDelegateFromXml<Action<int, string?, object?>>(serialization);
Assert.IsFalse(deserialization is null);
Assert.ThrowsException<ExpectedException>(() => deserialization!(default, default, default));
}
}
[TestMethod]
public void StaticDelegateToJson_Testing()
{
{ // Should Succeed
Func<int, string?, object?, decimal> functionToSerialize = StaticFunction;
string serialization = Serialization.StaticDelegateToJson(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
}
{ // Should Succeed
Action<int, string?, object?> functionToSerialize = StaticAction;
string serialization = Serialization.StaticDelegateToJson(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
}
{ // Should Fail (Non-Static)
Action nonStaticDelegate = NonStaticMethod;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToJson(nonStaticDelegate));
}
{ // Should Fail (Local Function)
Exception notImplemented = new NotImplementedException();
void LocalFunctionToSerializeFail() => throw notImplemented;
Action nonStaticDelegate = LocalFunctionToSerializeFail;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToJson(nonStaticDelegate));
}
{ // Should Fail (Static Local Function)
static void StaticLocalFunctionToSerialize() => throw new NotImplementedException();
Action nonStaticDelegate = StaticLocalFunctionToSerialize;
Assert.ThrowsException<NotSupportedException>(() => Serialization.StaticDelegateToJson(nonStaticDelegate));
}
}
[TestMethod]
public void StaticDelegateFromJson_Testing()
{
{ // Should Succeed
Func<int, string?, object?, decimal> functionToSerialize = StaticFunction;
string serialization = Serialization.StaticDelegateToJson(functionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
var deserialization = Serialization.StaticDelegateFromJson<Func<int, string?, object?, decimal>>(serialization);
Assert.IsFalse(deserialization is null);
Assert.ThrowsException<ExpectedException>(() => deserialization!(default, default, default));
}
{ // Should Succeed
Action<int, string?, object?> actionToSerialize = StaticAction;
string serialization = Serialization.StaticDelegateToJson(actionToSerialize);
Assert.IsFalse(string.IsNullOrWhiteSpace(serialization));
var deserialization = Serialization.StaticDelegateFromJson<Action<int, string?, object?>>(serialization);
Assert.IsFalse(deserialization is null);
Assert.ThrowsException<ExpectedException>(() => deserialization!(default, default, default));
}
}
}