-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathVerifierSettings.cs
201 lines (177 loc) · 5.92 KB
/
VerifierSettings.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
193
194
195
196
197
198
199
200
201
// ReSharper disable RedundantSuppressNullableWarningExpression
// ReSharper disable UnusedParameter.Local
namespace VerifyTests;
public static partial class VerifierSettings
{
internal static SerializationSettings serialization = new();
public static bool TryGetToString(
object target,
[NotNullWhen(true)] out Func<object, IReadOnlyDictionary<string, object>, AsStringResult>? toString)
{
if (target is Encoding encoding)
{
toString = (_, _) => encoding.EncodingName;
return true;
}
if (target is Expression expression)
{
toString = (_, _) => expression.ToString();
return true;
}
if (TypeNameConverter.TryGetSimpleName(target, out var name))
{
toString = (_, _) => name;
return true;
}
return typeToString.TryGetValue(target.GetType(), out toString);
}
static Dictionary<Type, Func<object, IReadOnlyDictionary<string, object>, AsStringResult>> typeToString = new()
{
#region typeToStringMapping
{
typeof(StringBuilder), (target, _) => ((StringBuilder) target).ToString()
},
{
typeof(StringWriter), (target, _) => ((StringWriter) target).ToString()
},
{
typeof(bool), (target, _) => ((bool) target).ToString(Culture.InvariantCulture)
},
{
typeof(short), (target, _) => ((short) target).ToString(Culture.InvariantCulture)
},
{
typeof(ushort), (target, _) => ((ushort) target).ToString(Culture.InvariantCulture)
},
{
typeof(int), (target, _) => ((int) target).ToString(Culture.InvariantCulture)
},
{
typeof(uint), (target, _) => ((uint) target).ToString(Culture.InvariantCulture)
},
{
typeof(long), (target, _) => ((long) target).ToString(Culture.InvariantCulture)
},
{
typeof(ulong), (target, _) => ((ulong) target).ToString(Culture.InvariantCulture)
},
{
typeof(decimal), (target, _) => ((decimal) target).ToString(Culture.InvariantCulture)
},
{
typeof(BigInteger), (target, _) => ((BigInteger) target).ToString(Culture.InvariantCulture)
},
#if NET6_0_OR_GREATER
{
typeof(Half), (target, _) => ((Half) target).ToString(Culture.InvariantCulture)
},
#endif
#if NET6_0_OR_GREATER
{
typeof(Date), (target, _) =>
{
var date = (Date) target;
return date.ToString("yyyy-MM-dd", Culture.InvariantCulture);
}
},
{
typeof(Time), (target, _) =>
{
var time = (Time) target;
return time.ToString("h:mm tt", Culture.InvariantCulture);
}
},
#endif
{
typeof(float), (target, _) => ((float) target).ToString(Culture.InvariantCulture)
},
{
typeof(double), (target, _) => ((double) target).ToString(Culture.InvariantCulture)
},
{
typeof(Guid), (target, _) => ((Guid) target).ToString()
},
{
typeof(DateTime), (target, _) => DateFormatter.ToJsonString((DateTime) target)
},
{
typeof(DateTimeOffset), (target, _) => DateFormatter.ToJsonString((DateTimeOffset) target)
},
{
typeof(XmlNode), (target, _) =>
{
var converted = (XmlNode) target;
var document = XDocument.Parse(converted.OuterXml);
return new(document.ToString(), "xml");
}
},
{
typeof(XElement), (target, settings) =>
{
var converted = (XElement) target;
return new(converted.ToString(), "xml");
}
},
#endregion
};
public static void TreatAsString<T>(AsString<T>? toString = null)
where T : notnull
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
toString ??= (target, _) => new(target.ToString()!);
typeToString[typeof(T)] = (target, settings) => toString((T) target, settings);
}
internal static void Reset()
{
InnerVerifier.verifyHasBeenRun = false;
DateCountingEnabled = true;
StrictJson = false;
scrubProjectDir = true;
scrubSolutionDir = true;
orderPropertiesAlphabetically = false;
orderJsonObjects = false;
scrubUserProfile = true;
autoVerify = null;
UniquePrefixDisabled = false;
UseUniqueDirectorySplitMode = false;
omitContentFromException = false;
encoding = new UTF8Encoding(true, true);
GlobalScrubbers.Clear();
}
public static void UseStrictJson()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
StrictJson = true;
}
public static bool StrictJson { get; private set; }
internal static bool scrubProjectDir = true;
public static void DontScrubProjectDirectory()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
scrubProjectDir = false;
}
internal static bool scrubSolutionDir = true;
public static void DontScrubSolutionDirectory()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
scrubSolutionDir = false;
}
internal static bool scrubUserProfile = true;
public static void DontScrubUserProfile()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
scrubUserProfile = false;
}
internal static bool orderPropertiesAlphabetically;
public static void SortPropertiesAlphabetically()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
orderPropertiesAlphabetically = true;
}
internal static bool orderJsonObjects;
public static void SortJsonObjects()
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
orderJsonObjects = true;
}
}