/
Id_for_Int64_specs.cs
192 lines (169 loc) · 4.67 KB
/
Id_for_Int64_specs.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace Identifiers.Id_for_Int64_specs;
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.Int64Id.CompareTo(Nil.Object).Should().Be(1);
[Test]
public void to_Int64Id_as_object()
{
object obj = Svo.Int64Id;
Svo.Int64Id.CompareTo(obj).Should().Be(0);
}
[Test]
public void to_Int64Id_only()
=> new object().Invoking(Svo.Int64Id.CompareTo).Should().Throw<ArgumentException>();
[Test]
public void can_be_sorted_using_compare()
{
var sorted = new[]
{
Int64Id.Empty,
Int64Id.Create(1L),
Int64Id.Create(3L),
Int64Id.Create(7L),
Int64Id.Create(11L),
Int64Id.Create(17L),
};
var list = new List<Int64Id> { sorted[3], sorted[4], sorted[5], sorted[2], sorted[0], sorted[1] };
list.Sort();
list.Should().BeEquivalentTo(sorted);
}
}
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase("", null)]
[TestCase(null, null)]
[TestCase(123456789L, 123456789L)]
[TestCase("123456789", 123456789L)]
public void System_Text_JSON_deserialization(object json, Int64Id svo)
=> JsonTester.Read_System_Text_JSON<Int64Id>(json).Should().Be(svo);
[TestCase(null, null)]
[TestCase(123456789L, "123456789")]
public void System_Text_JSON_serialization(Int64Id svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
[TestCase(-2)]
[TestCase(17)]
[TestCase("17")]
public void taking_constrains_into_account(object json)
{
json.Invoking(JsonTester.Read_System_Text_JSON<Id<ForEven>>)
.Should().Throw<System.Text.Json.JsonException>()
.WithMessage("Not a valid identifier.");
}
private sealed class ForEven : Int64IdBehavior
{
public override bool TryCreate(object? obj, out object? id)
{
if (obj is long even && even % 2 == 0)
{
id = even;
return true;
}
else
{
id = null;
return false;
}
}
public override bool TryParse(string? str, out object? id)
=> TryCreate(long.TryParse(str, out long even) ? even : null, out id);
}
#endif
[TestCase("", null)]
[TestCase(123456789L, 123456789L)]
[TestCase("123456789", 123456789L)]
public void convention_based_deserialization(object json, Int64Id svo)
=> JsonTester.Read<Int64Id>(json).Should().Be(svo);
[TestCase(null, null)]
[TestCase(123456789L, "123456789")]
public void convention_based_serialization(Int64Id svo, object json)
=> JsonTester.Write(svo).Should().Be(json);
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(Int64Id).Should().HaveTypeConverterDefined();
[Test]
public void from_null_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.FromNull<string>().To<Int64Id>().Should().Be(Int64Id.Empty);
}
}
[Test]
public void from_empty_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From(string.Empty).To<Int64Id>().Should().Be(Int64Id.Empty);
}
}
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("PREFIX987654321").To<Int64Id>().Should().Be(Svo.Int64Id);
}
}
[Test]
public void to_string()
=> Converting.ToString().From(Svo.Int64Id).Should().Be("987654321");
[Test]
public void from_long()
=> Converting.From(987654321L).To<Int64Id>().Should().Be(Svo.Int64Id);
[Test]
public void to_long()
=> Converting.To<long>().From(Svo.Int64Id).Should().Be(987654321L);
[TestCase("666")]
[TestCase("PREF17")]
public void from_invalid_string(string str)
{
Func<Int64Id> convert = () => Converting.From(str).To<Int64Id>();
convert.Should().Throw<InvalidCastException>()
.WithMessage("Cast from string to Qowaiv.Identifiers.Id<Qowaiv.TestTools.ForInt64> is not valid.");
}
[TestCase(-18)]
[TestCase(666)]
public void from_invalid_number(long number)
{
Func<Int64Id> convert = () => Converting.From(number).To<Int64Id>();
convert.Should().Throw<InvalidCastException>()
.WithMessage("Cast from long to Qowaiv.Identifiers.Id<Qowaiv.TestTools.ForInt64> is not valid.");
}
}
#if NET8_0_OR_GREATER
#else
public class Supports_binary_serialization
{
[Test]
[Obsolete("Usage of the binary formatter is considered harmful.")]
public void using_BinaryFormatter()
{
var round_tripped = SerializeDeserialize.Binary(Svo.Int64Id);
round_tripped.Should().Be(Svo.Int64Id);
}
[Test]
public void storing_value_in_SerializationInfo()
{
var info = Serialize.GetInfo(Svo.Int64Id);
info.GetInt64("Value").Should().Be(987654321L);
}
}
#endif
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(ForInt64))
.Should().Be(new OpenApiDataType(
dataType: typeof(Int64Id),
description: "Int64 based identifier",
example: 17,
type: "integer",
format: "identifier",
nullable: true));
}