Skip to content

Commit e9ed2b0

Browse files
committed
reorganize IsSerializable
1 parent fada14e commit e9ed2b0

File tree

2 files changed

+183
-16
lines changed

2 files changed

+183
-16
lines changed

src/Smdn.Test.NUnit.Utils/Smdn.Test.NUnit/Assert.Serialization.cs

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,109 @@
11
// SPDX-FileCopyrightText: 2020 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3+
#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NETCOREAPP2_0_OR_GREATER || NET5_0_OR_GREATER
4+
#define SYSTEM_RUNTIME_SERIALIZATION_ISERIALIZABLE
5+
#define SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
6+
#endif
7+
38
using System;
49
using System.IO;
10+
#if SYSTEM_RUNTIME_SERIALIZATION_ISERIALIZABLE || SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
11+
using System.Runtime.Serialization;
12+
#endif
513
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
614
using System.Runtime.Serialization.Formatters.Binary;
715
#endif
816

917
namespace Smdn.Test.NUnit;
1018

1119
public partial class Assert {
12-
public static void IsSerializableBinaryFormat<TSerializable>(TSerializable obj)
13-
/*where TSerializable : ISerializable*/
14-
=> IsSerializableBinaryFormat(obj, null);
20+
#if SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
21+
private static IFormatter CreateDefaultSerializationFormatter()
22+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
23+
// TODO: use JsonSerializer instead
24+
// https://docs.microsoft.com/ja-jp/dotnet/fundamentals/syslib-diagnostics/syslib0011
25+
=> new BinaryFormatter();
26+
#else
27+
=> null;
28+
#endif
29+
30+
private static IFormatter CreateDefaultDeserializationFormatter()
31+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY && SYSTEM_RUNTIME_SERIALIZATION_SERIALIZATIONBINDER
32+
=> new BinaryFormatter() {
33+
Binder = new DeserializationBinder(),
34+
};
35+
#else
36+
=> null;
37+
#endif
38+
#endif // SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
39+
40+
public static void IsSerializable<TSerializable>(
41+
TSerializable obj,
42+
Action<TSerializable> testDeserializedObject = null
43+
)
44+
#if SYSTEM_RUNTIME_SERIALIZATION_ISERIALIZABLE
45+
where TSerializable : ISerializable
46+
#endif
47+
=> IsSerializableCore(
48+
obj,
49+
#if SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
50+
CreateDefaultSerializationFormatter(),
51+
CreateDefaultDeserializationFormatter(),
52+
#endif
53+
testDeserializedObject
54+
);
55+
56+
#if SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
57+
public static void IsSerializable<TSerializable>(
58+
TSerializable obj,
59+
IFormatter serializationFormatter,
60+
IFormatter deserializationFormatter,
61+
Action<TSerializable> testDeserializedObject = null
62+
)
63+
#if SYSTEM_RUNTIME_SERIALIZATION_ISERIALIZABLE
64+
where TSerializable : ISerializable
65+
#endif
66+
=> IsSerializableCore(
67+
obj,
68+
serializationFormatter ?? throw new ArgumentNullException(nameof(serializationFormatter)),
69+
deserializationFormatter ?? throw new ArgumentNullException(nameof(deserializationFormatter)),
70+
testDeserializedObject
71+
);
72+
#endif
1573

16-
public static void IsSerializableBinaryFormat<TSerializable>(
74+
private static void IsSerializableCore<TSerializable>(
1775
TSerializable obj,
76+
#if SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
77+
IFormatter serializationFormatter,
78+
IFormatter deserializationFormatter,
79+
#endif
1880
Action<TSerializable> testDeserializedObject
1981
)
20-
/*where TSerializable : ISerializable*/
82+
#if SYSTEM_RUNTIME_SERIALIZATION_ISERIALIZABLE
83+
where TSerializable : ISerializable
84+
#endif
2185
{
22-
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY && SYSTEM_RUNTIME_SERIALIZATION_SERIALIZATIONBINDER
23-
// TODO: use JsonSerializer instead
24-
// https://docs.microsoft.com/ja-jp/dotnet/fundamentals/syslib-diagnostics/syslib0011
25-
var serializeFormatter = new BinaryFormatter();
86+
#if SYSTEM_RUNTIME_SERIALIZATION_IFORMATTER
87+
if (serializationFormatter is null || deserializationFormatter is null)
88+
return; // do nothing
2689

2790
using var stream = new MemoryStream();
2891

2992
#pragma warning disable SYSLIB0011
30-
serializeFormatter.Serialize(stream, obj);
93+
serializationFormatter.Serialize(stream, obj);
3194
#pragma warning restore SYSLIB0011
3295

3396
stream.Position = 0L;
3497

35-
var deserializeFormatter = new BinaryFormatter() {
36-
Binder = new DeserializationBinder()
37-
};
38-
3998
#pragma warning disable SYSLIB0011
40-
var deserialized = deserializeFormatter.Deserialize(stream);
99+
var deserialized = deserializationFormatter.Deserialize(stream);
41100
#pragma warning restore SYSLIB0011
42101

43102
IsNotNull(deserialized);
44103
AreNotSame(obj, deserialized);
45104
IsInstanceOf<TSerializable>(deserialized);
46105

47-
if (testDeserializedObject != null)
106+
if (testDeserializedObject is not null)
48107
testDeserializedObject((TSerializable)deserialized);
49108
#else
50109
// do nothing
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
5+
using System.Runtime.Serialization.Formatters.Binary;
6+
#endif
7+
using NUnit.Framework;
8+
9+
namespace Smdn.Test.NUnit;
10+
11+
[TestFixture]
12+
public class AssertSerializationTests {
13+
[Test] public void IsSerializable_Uri() => Assert.IsSerializable(new Uri("http://example.com/example/"));
14+
[Test] public void IsSerializable_IntPtr() => Assert.IsSerializable(IntPtr.Zero);
15+
[Test] public void IsSerializable_DateTime() => Assert.IsSerializable(DateTime.MinValue);
16+
[Test] public void IsSerializable_Exception() => Assert.IsSerializable(new InvalidOperationException());
17+
18+
[Test]
19+
public void IsSerializable_WithTestAction_Uri()
20+
{
21+
var url = "http://example.com/example/";
22+
var testActionCalled = false;
23+
24+
Assert.IsSerializable(new Uri(url), obj => {
25+
Assert.IsNotNull(obj);
26+
Assert.IsInstanceOf<Uri>(obj);
27+
Assert.AreEqual(url, (obj as Uri).AbsoluteUri);
28+
29+
testActionCalled = true;
30+
});
31+
32+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
33+
Assert.IsTrue(testActionCalled, "test action called");
34+
#else
35+
Assert.IsFalse(testActionCalled, "test action called");
36+
#endif
37+
}
38+
39+
[Test]
40+
public void IsSerializable_WithTestAction_IntPtr()
41+
{
42+
var testActionCalled = false;
43+
44+
Assert.IsSerializable(new IntPtr(1), obj => {
45+
Assert.IsNotNull(obj);
46+
Assert.IsInstanceOf<IntPtr>(obj);
47+
Assert.AreEqual(new IntPtr(1), obj);
48+
49+
testActionCalled = true;
50+
});
51+
52+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
53+
Assert.IsTrue(testActionCalled, "test action called");
54+
#else
55+
Assert.IsFalse(testActionCalled, "test action called");
56+
#endif
57+
}
58+
59+
[Test]
60+
public void IsSerializable_WithTestAction_DateTime()
61+
{
62+
var now = DateTime.Now;
63+
var testActionCalled = false;
64+
65+
Assert.IsSerializable(now, obj => {
66+
Assert.IsNotNull(obj);
67+
Assert.IsInstanceOf<DateTime>(obj);
68+
Assert.AreEqual(now, obj);
69+
70+
testActionCalled = true;
71+
});
72+
73+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
74+
Assert.IsTrue(testActionCalled, "test action called");
75+
#else
76+
Assert.IsFalse(testActionCalled, "test action called");
77+
#endif
78+
}
79+
80+
[Test]
81+
public void IsSerializable_WithTestAction_Exception()
82+
{
83+
var innerException = new NotImplementedException("inner exception");
84+
var message = "is serializable";
85+
var testActionCalled = false;
86+
87+
Assert.IsSerializable(new NotSupportedException(message, innerException), obj => {
88+
Assert.IsNotNull(obj);
89+
Assert.IsInstanceOf<NotSupportedException>(obj);
90+
91+
var ex = (obj as NotSupportedException);
92+
93+
Assert.AreEqual(message, ex.Message);
94+
95+
Assert.IsNotNull(ex.InnerException);
96+
Assert.IsInstanceOf<NotImplementedException>(ex.InnerException);
97+
Assert.AreEqual(innerException.Message, ex.InnerException.Message);
98+
99+
testActionCalled = true;
100+
});
101+
102+
#if SYSTEM_RUNTIME_SERIALIZATION_FORMATTER_BINARY
103+
Assert.IsTrue(testActionCalled, "test action called");
104+
#else
105+
Assert.IsFalse(testActionCalled, "test action called");
106+
#endif
107+
}
108+
}

0 commit comments

Comments
 (0)