-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DebugTestsNoListeners.Interpolation.cs
215 lines (166 loc) · 9.21 KB
/
DebugTestsNoListeners.Interpolation.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#define DEBUG
using System.Text;
using Xunit;
namespace System.Diagnostics.Tests
{
public partial class DebugTestsNoListeners : DebugTests
{
[Fact]
public void Asserts_Interpolation()
{
Debug.AssertInterpolatedStringHandler message;
Debug.AssertInterpolatedStringHandler detailedMessage;
bool shouldAppend;
message = new Debug.AssertInterpolatedStringHandler(0, 0, true, out shouldAppend);
VerifyLogged(() => Debug.Assert(true, ref message), "");
message = new Debug.AssertInterpolatedStringHandler(0, 0, true, out shouldAppend);
detailedMessage = new Debug.AssertInterpolatedStringHandler(0, 0, true, out shouldAppend);
VerifyLogged(() => Debug.Assert(true, ref message, ref detailedMessage), "");
message = new Debug.AssertInterpolatedStringHandler(0, 0, false, out shouldAppend);
message.AppendLiteral("uh oh");
VerifyAssert(() => Debug.Assert(false, ref message), "uh oh");
message = new Debug.AssertInterpolatedStringHandler(0, 0, false, out shouldAppend);
message.AppendLiteral("uh oh");
detailedMessage = new Debug.AssertInterpolatedStringHandler(0, 0, false, out shouldAppend);
detailedMessage.AppendLiteral("something went wrong");
VerifyAssert(() => Debug.Assert(false, ref message, ref detailedMessage), "uh oh", "something went wrong");
}
[Fact]
public void Asserts_Interpolation_Syntax()
{
VerifyLogged(() => Debug.Assert(true, $"you won't see this {EmptyToString.Instance}"), "");
VerifyLogged(() => Debug.Assert(true, $"you won't see this {EmptyToString.Instance}", $"you won't see this {EmptyToString.Instance}"), "");
VerifyAssert(() => Debug.Assert(false, $"uh oh{EmptyToString.Instance}"), "uh oh");
VerifyAssert(() => Debug.Assert(false, $"uh oh{EmptyToString.Instance}", $"something went wrong{EmptyToString.Instance}"), "uh oh", "something went wrong");
}
[Fact]
public void WriteIf_Interpolation()
{
Debug.WriteIfInterpolatedStringHandler handler;
bool shouldAppend;
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, true, out shouldAppend);
handler.AppendLiteral("logged");
VerifyLogged(() => Debug.WriteIf(true, ref handler), "logged");
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, false, out shouldAppend);
VerifyLogged(() => Debug.WriteIf(false, ref handler), "");
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, true, out shouldAppend);
handler.AppendLiteral("logged");
VerifyLogged(() => Debug.WriteIf(true, ref handler, "category"), "category: logged");
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, false, out shouldAppend);
VerifyLogged(() => Debug.WriteIf(false, ref handler, "category"), "");
GoToNextLine();
}
[Fact]
public void WriteIf_Interpolation_Syntax()
{
VerifyLogged(() => Debug.WriteIf(true, $"{EmptyToString.Instance}logged"), "logged");
VerifyLogged(() => Debug.WriteIf(false, $"{EmptyToString.Instance}logged"), "");
VerifyLogged(() => Debug.WriteIf(true, $"{EmptyToString.Instance}logged", "category"), "category: logged");
VerifyLogged(() => Debug.WriteIf(false, $"{EmptyToString.Instance}logged", "category"), "");
GoToNextLine();
}
[Fact]
public void WriteLineIf_Interpolation()
{
Debug.WriteIfInterpolatedStringHandler handler;
bool shouldAppend;
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, true, out shouldAppend);
handler.AppendLiteral("logged");
VerifyLogged(() => Debug.WriteLineIf(true, ref handler), "logged" + Environment.NewLine);
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, false, out shouldAppend);
VerifyLogged(() => Debug.WriteLineIf(false, ref handler), "");
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, true, out shouldAppend);
handler.AppendLiteral("logged");
VerifyLogged(() => Debug.WriteLineIf(true, ref handler, "category"), "category: logged" + Environment.NewLine);
handler = new Debug.WriteIfInterpolatedStringHandler(0, 0, false, out shouldAppend);
VerifyLogged(() => Debug.WriteLineIf(false, ref handler, "category"), "");
}
[Fact]
public void WriteLineIf_Interpolation_Syntax()
{
VerifyLogged(() => Debug.WriteLineIf(true, $"{EmptyToString.Instance}logged"), "logged" + Environment.NewLine);
VerifyLogged(() => Debug.WriteLineIf(false, $"{EmptyToString.Instance}logged"), "");
VerifyLogged(() => Debug.WriteLineIf(true, $"{EmptyToString.Instance}logged", "category"), "category: logged" + Environment.NewLine);
VerifyLogged(() => Debug.WriteLineIf(false, $"{EmptyToString.Instance}logged", "category"), "");
GoToNextLine();
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Condition_ShouldAppend_Matches(bool condition)
{
bool shouldAppend;
new Debug.AssertInterpolatedStringHandler(1, 2, condition, out shouldAppend);
Assert.Equal(!condition, shouldAppend);
new Debug.WriteIfInterpolatedStringHandler(1, 2, condition, out shouldAppend);
Assert.Equal(condition, shouldAppend);
}
[Fact]
public void DebugHandler_AppendOverloads_MatchStringBuilderHandler()
{
var actual = new Debug.AssertInterpolatedStringHandler(0, 0, condition: false, out bool shouldAppend);
Assert.True(shouldAppend);
var sb = new StringBuilder();
var expected = new StringBuilder.AppendInterpolatedStringHandler(0, 0, sb);
actual.AppendLiteral("abcd");
expected.AppendLiteral("abcd");
actual.AppendFormatted(123);
expected.AppendFormatted(123);
actual.AppendFormatted(45.6, 10);
expected.AppendFormatted(45.6, 10);
actual.AppendFormatted(default(Guid), "X");
expected.AppendFormatted(default(Guid), "X");
DateTime dt = DateTime.UtcNow;
actual.AppendFormatted(dt, -100, "r");
expected.AppendFormatted(dt, -100, "r");
actual.AppendFormatted("hello");
expected.AppendFormatted("hello");
actual.AppendFormatted("world", -10, null);
expected.AppendFormatted("world", -10, null);
actual.AppendFormatted((ReadOnlySpan<char>)"nice to");
expected.AppendFormatted((ReadOnlySpan<char>)"nice to");
actual.AppendFormatted((ReadOnlySpan<char>)"nice to", 0, "anything");
expected.AppendFormatted((ReadOnlySpan<char>)"nice to", 0, "anything");
actual.AppendFormatted((object)DayOfWeek.Monday, 42, null);
expected.AppendFormatted((object)DayOfWeek.Monday, 42, null);
VerifyAssert(() => Debug.Assert(false, ref actual), sb.ToString());
}
[Fact]
public void WriteIfHandler_AppendOverloads_MatchStringBuilderHandler()
{
var actual = new Debug.WriteIfInterpolatedStringHandler(0, 0, condition: true, out bool shouldAppend);
Assert.True(shouldAppend);
var sb = new StringBuilder();
var expected = new StringBuilder.AppendInterpolatedStringHandler(0, 0, sb);
actual.AppendLiteral("abcd");
expected.AppendLiteral("abcd");
actual.AppendFormatted(123);
expected.AppendFormatted(123);
actual.AppendFormatted(45.6, 10);
expected.AppendFormatted(45.6, 10);
actual.AppendFormatted(default(Guid), "X");
expected.AppendFormatted(default(Guid), "X");
DateTime dt = DateTime.UtcNow;
actual.AppendFormatted(dt, -100, "r");
expected.AppendFormatted(dt, -100, "r");
actual.AppendFormatted("hello");
expected.AppendFormatted("hello");
actual.AppendFormatted("world", -10, null);
expected.AppendFormatted("world", -10, null);
actual.AppendFormatted((ReadOnlySpan<char>)"nice to");
expected.AppendFormatted((ReadOnlySpan<char>)"nice to");
actual.AppendFormatted((ReadOnlySpan<char>)"nice to", 0, "anything");
expected.AppendFormatted((ReadOnlySpan<char>)"nice to", 0, "anything");
actual.AppendFormatted((object)DayOfWeek.Monday, 42, null);
expected.AppendFormatted((object)DayOfWeek.Monday, 42, null);
VerifyLogged(() => Debug.WriteIf(true, ref actual), sb.ToString());
}
private sealed class EmptyToString
{
public static EmptyToString Instance { get; } = new EmptyToString();
public override string ToString() => "";
}
}
}