/
Id_for_Guid_specs.cs
174 lines (150 loc) · 4.28 KB
/
Id_for_Guid_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
namespace Identifiers.Id_for_Guid_specs;
public class With_domain_logic
{
[TestCase(true, "33ef5805-c472-4b1f-88bb-2f0723c43889")]
[TestCase(false, "")]
public void HasValue_is(bool result, CustomGuid svo) => svo.HasValue.Should().Be(result);
}
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.CustomGuid.CompareTo(Nil.Object).Should().Be(1);
[Test]
public void to_CustomGuid_as_object()
{
object obj = Svo.CustomGuid;
Svo.CustomGuid.CompareTo(obj).Should().Be(0);
}
[Test]
public void to_CustomGuid_only()
=> new object().Invoking(Svo.CustomGuid.CompareTo).Should().Throw<ArgumentException>();
[Test]
public void can_be_sorted_using_compare()
{
var sorted = new[]
{
CustomGuid.Empty,
CustomGuid.Parse("33ef5805-c472-4b1f-88bb-2f0723c43889"),
CustomGuid.Parse("58617a65-2a14-4a9a-82a8-c1a82c956c25"),
CustomGuid.Parse("853634b4-e474-4b0f-b9ba-01fc732b56d8"),
CustomGuid.Parse("93ca7b43-8fb3-44e5-a21f-feeebb8e0f6f"),
CustomGuid.Parse("f5e6c39a-adcf-4eca-bcf2-6b8317ac502c"),
};
var list = new List<CustomGuid> { sorted[3], sorted[4], sorted[5], sorted[2], sorted[0], sorted[1] };
list.Sort();
list.Should().BeEquivalentTo(sorted);
}
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(CustomGuid).Should().HaveTypeConverterDefined();
[Test]
public void from_null_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.FromNull<string>().To<CustomGuid>().Should().Be(CustomGuid.Empty);
}
}
[Test]
public void from_empty_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From(string.Empty).To<CustomGuid>().Should().Be(CustomGuid.Empty);
}
}
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("8A1A8C42-D2FF-E254-E26E-B6ABCBF19420").To<CustomGuid>().Should().Be(Svo.CustomGuid);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.ToString().From(Svo.CustomGuid).Should().Be("8a1a8c42-d2ff-e254-e26e-b6abcbf19420");
}
}
[Test]
public void from_Guid()
=> Converting.From(Svo.Guid).To<CustomGuid>().Should().Be(Svo.CustomGuid);
[Test]
public void from_Uuid()
=> Converting.From(Svo.Uuid).To<CustomGuid>().Should().Be(Svo.CustomGuid);
[Test]
public void to_Guid()
=> Converting.To<Guid>().From(Svo.CustomGuid).Should().Be(Svo.Guid);
[Test]
public void to_Uuid()
=> Converting.To<Uuid>().From(Svo.CustomGuid).Should().Be(Svo.Uuid);
}
public class Supports_JSON_serialization
{
[Test]
public void writes_null_for_default_value()
=> JsonTester.Write(default(CustomGuid)).Should().BeNull();
[Test]
public void writes_GUID_for_non_default_value()
=> JsonTester.Write(Svo.CustomGuid).Should().Be("8a1a8c42-d2ff-e254-e26e-b6abcbf19420");
#if NET6_0_OR_GREATER
[Test]
public void System_Text_JSON_deserialization_of_dictionary_keys()
{
System.Text.Json.JsonSerializer.Deserialize<Dictionary<CustomGuid, int>>(@"{""8a1a8c42-d2ff-e254-e26e-b6abcbf19420"":42}")
.Should().BeEquivalentTo(new Dictionary<CustomGuid, int>()
{
[Svo.CustomGuid] = 42,
});
}
[Test]
public void System_Text_JSON_serialization_of_dictionary_keys()
{
var dictionary = new Dictionary<CustomGuid, int>()
{
[default] = 17,
[Svo.CustomGuid] = 42,
};
System.Text.Json.JsonSerializer.Serialize(dictionary)
.Should().Be(@"{"""":17,""8a1a8c42-d2ff-e254-e26e-b6abcbf19420"":42}");
}
#endif
}
#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.CustomGuid);
round_tripped.Should().Be(Svo.CustomGuid);
}
[Test]
public void storing_value_in_SerializationInfo()
{
var info = Serialize.GetInfo(Svo.CustomGuid);
info.GetValue("Value", typeof(Guid)).Should().Be(Guid.Parse("8A1A8C42-D2FF-E254-E26E-B6ABCBF19420"));
}
}
#endif
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(ForGuid))
.Should().Be(new OpenApiDataType(
dataType: typeof(CustomGuid),
description: "GUID based identifier",
example: "8a1a8c42-d2ff-e254-e26e-b6abcbf19420",
type: "string",
format: "guid",
nullable: true));
}