forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugTests.cs
More file actions
186 lines (156 loc) · 7.28 KB
/
DebugTests.cs
File metadata and controls
186 lines (156 loc) · 7.28 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Xunit;
namespace System.Diagnostics.Tests
{
public class DebugTests
{
[Fact]
public void Asserts()
{
VerifyLogged(() => { Debug.Assert(true); }, "");
VerifyLogged(() => { Debug.Assert(true, "assert passed"); }, "");
VerifyLogged(() => { Debug.Assert(true, "assert passed", "nothing is wrong"); }, "");
VerifyLogged(() => { Debug.Assert(true, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'); }, "");
VerifyAssert(() => { Debug.Assert(false); }, "");
VerifyAssert(() => { Debug.Assert(false, "assert passed"); }, "assert passed");
VerifyAssert(() => { Debug.Assert(false, "assert passed", "nothing is wrong"); }, "assert passed", "nothing is wrong");
VerifyAssert(() => { Debug.Assert(false, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'); }, "assert passed", "nothing is wrong a b");
}
[Fact]
public void Fail()
{
VerifyAssert(() => { Debug.Fail("something bad happened"); }, "something bad happened");
VerifyAssert(() => { Debug.Fail("something bad happened", "really really bad"); }, "something bad happened", "really really bad");
}
[Fact]
public void Write()
{
VerifyLogged(() => { Debug.Write(5); }, "5");
VerifyLogged(() => { Debug.Write((string)null); }, "");
VerifyLogged(() => { Debug.Write((object)null); }, "");
VerifyLogged(() => { Debug.Write(5, "category"); }, "category:5");
VerifyLogged(() => { Debug.Write((object)null, "category"); }, "category:");
VerifyLogged(() => { Debug.Write("logged"); }, "logged");
VerifyLogged(() => { Debug.Write("logged", "category"); }, "category:logged");
VerifyLogged(() => { Debug.Write("logged", (string)null); }, "logged");
string longString = new string('d', 8192);
VerifyLogged(() => { Debug.Write(longString); }, longString);
}
[Fact]
public void WriteLine()
{
VerifyLogged(() => { Debug.WriteLine(5); }, "5" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine((string)null); }, Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine((object)null); }, Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine(5, "category"); }, "category:5" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine((object)null, "category"); }, "category:" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine("logged"); }, "logged" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine("logged", "category"); }, "category:logged" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine("logged", (string)null); }, "logged" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLine("{0} {1}", 'a', 'b'); }, "a b" + Environment.NewLine);
}
[Fact]
public void WriteIf()
{
VerifyLogged(() => { Debug.WriteIf(true, 5); }, "5");
VerifyLogged(() => { Debug.WriteIf(false, 5); }, "");
VerifyLogged(() => { Debug.WriteIf(true, 5, "category"); }, "category:5");
VerifyLogged(() => { Debug.WriteIf(false, 5, "category"); }, "");
VerifyLogged(() => { Debug.WriteIf(true, "logged"); }, "logged");
VerifyLogged(() => { Debug.WriteIf(false, "logged"); }, "");
VerifyLogged(() => { Debug.WriteIf(true, "logged", "category"); }, "category:logged");
VerifyLogged(() => { Debug.WriteIf(false, "logged", "category"); }, "");
}
[Fact]
public void WriteLineIf()
{
VerifyLogged(() => { Debug.WriteLineIf(true, 5); }, "5" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLineIf(false, 5); }, "");
VerifyLogged(() => { Debug.WriteLineIf(true, 5, "category"); }, "category:5" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLineIf(false, 5, "category"); }, "");
VerifyLogged(() => { Debug.WriteLineIf(true, "logged"); }, "logged" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLineIf(false, "logged"); }, "");
VerifyLogged(() => { Debug.WriteLineIf(true, "logged", "category"); }, "category:logged" + Environment.NewLine);
VerifyLogged(() => { Debug.WriteLineIf(false, "logged", "category"); }, "");
}
static void VerifyLogged(Action test, string expectedOutput)
{
// First use our test logger to verify the output
Debug.IDebugLogger oldLogger = Debug.s_logger;
Debug.s_logger = WriteLogger.Instance;
try
{
WriteLogger.Instance.Clear();
test();
#if DEBUG
Assert.Equal(expectedOutput, WriteLogger.Instance.LoggedOutput);
#else
Assert.Equal(string.Empty, WriteLogger.Instance.LoggedOutput);
#endif
}
finally
{
Debug.s_logger = oldLogger;
}
// Then also use the actual logger for this platform, just to verify
// that nothing fails.
test();
}
static void VerifyAssert(Action test, params string[] expectedOutputStrings)
{
Debug.IDebugLogger oldLogger = Debug.s_logger;
Debug.s_logger = WriteLogger.Instance;
try
{
WriteLogger.Instance.Clear();
test();
#if DEBUG
for (int i = 0; i < expectedOutputStrings.Length; i++)
{
Assert.Contains(expectedOutputStrings[i], WriteLogger.Instance.LoggedOutput);
Assert.Contains(expectedOutputStrings[i], WriteLogger.Instance.AssertUIOutput);
}
#else
Assert.Equal(string.Empty, WriteLogger.Instance.LoggedOutput);
Assert.Equal(string.Empty, WriteLogger.Instance.AssertUIOutput);
#endif
}
finally
{
Debug.s_logger = oldLogger;
}
}
class WriteLogger : Debug.IDebugLogger
{
public readonly static WriteLogger Instance = new WriteLogger();
private WriteLogger()
{
}
public string LoggedOutput
{
get;
private set;
}
public string AssertUIOutput
{
get;
private set;
}
public void Clear()
{
LoggedOutput = string.Empty;
AssertUIOutput = string.Empty;
}
public void ShowAssertDialog(string stackTrace, string message, string detailMessage)
{
AssertUIOutput += stackTrace + message + detailMessage;
}
public void WriteCore(string message)
{
Assert.NotNull(message);
LoggedOutput += message;
}
}
}
}