1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
@@ -15,12 +15,13 @@ public class TestConsole : IConsole
15
15
{
16
16
private event ConsoleCancelEventHandler _cancelKeyPress ;
17
17
private readonly TaskCompletionSource < bool > _cancelKeySubscribed = new TaskCompletionSource < bool > ( ) ;
18
+ private readonly TestOutputWriter _testWriter ;
18
19
19
20
public TestConsole ( ITestOutputHelper output )
20
21
{
21
- var writer = new TestOutputWriter ( output ) ;
22
- Error = writer ;
23
- Out = writer ;
22
+ _testWriter = new TestOutputWriter ( output ) ;
23
+ Error = _testWriter ;
24
+ Out = _testWriter ;
24
25
}
25
26
26
27
public event ConsoleCancelEventHandler CancelKeyPress
@@ -35,8 +36,8 @@ public event ConsoleCancelEventHandler CancelKeyPress
35
36
36
37
public Task CancelKeyPressSubscribed => _cancelKeySubscribed . Task ;
37
38
38
- public TextWriter Error { get ; set ; }
39
- public TextWriter Out { get ; set ; }
39
+ public TextWriter Error { get ; }
40
+ public TextWriter Out { get ; }
40
41
public TextReader In { get ; set ; } = new StringReader ( string . Empty ) ;
41
42
public bool IsInputRedirected { get ; set ; } = false ;
42
43
public bool IsOutputRedirected { get ; } = false ;
@@ -58,10 +59,21 @@ public void ResetColor()
58
59
{
59
60
}
60
61
62
+ public string GetOutput ( )
63
+ {
64
+ return _testWriter . GetOutput ( ) ;
65
+ }
66
+
67
+ public void ClearOutput ( )
68
+ {
69
+ _testWriter . ClearOutput ( ) ;
70
+ }
71
+
61
72
private class TestOutputWriter : TextWriter
62
73
{
63
74
private readonly ITestOutputHelper _output ;
64
75
private readonly StringBuilder _sb = new StringBuilder ( ) ;
76
+ private readonly StringBuilder _currentOutput = new StringBuilder ( ) ;
65
77
66
78
public TestOutputWriter ( ITestOutputHelper output )
67
79
{
@@ -83,8 +95,19 @@ public override void Write(char value)
83
95
else
84
96
{
85
97
_sb . Append ( value ) ;
98
+ _currentOutput . Append ( value ) ;
86
99
}
87
100
}
101
+
102
+ public string GetOutput ( )
103
+ {
104
+ return _currentOutput . ToString ( ) ;
105
+ }
106
+
107
+ public void ClearOutput ( )
108
+ {
109
+ _currentOutput . Clear ( ) ;
110
+ }
88
111
}
89
112
}
90
113
}
0 commit comments