1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
@@ -15,12 +15,13 @@ public class TestConsole : IConsole
1515 {
1616 private event ConsoleCancelEventHandler _cancelKeyPress ;
1717 private readonly TaskCompletionSource < bool > _cancelKeySubscribed = new TaskCompletionSource < bool > ( ) ;
18+ private readonly TestOutputWriter _testWriter ;
1819
1920 public TestConsole ( ITestOutputHelper output )
2021 {
21- var writer = new TestOutputWriter ( output ) ;
22- Error = writer ;
23- Out = writer ;
22+ _testWriter = new TestOutputWriter ( output ) ;
23+ Error = _testWriter ;
24+ Out = _testWriter ;
2425 }
2526
2627 public event ConsoleCancelEventHandler CancelKeyPress
@@ -35,8 +36,8 @@ public event ConsoleCancelEventHandler CancelKeyPress
3536
3637 public Task CancelKeyPressSubscribed => _cancelKeySubscribed . Task ;
3738
38- public TextWriter Error { get ; set ; }
39- public TextWriter Out { get ; set ; }
39+ public TextWriter Error { get ; }
40+ public TextWriter Out { get ; }
4041 public TextReader In { get ; set ; } = new StringReader ( string . Empty ) ;
4142 public bool IsInputRedirected { get ; set ; } = false ;
4243 public bool IsOutputRedirected { get ; } = false ;
@@ -58,10 +59,21 @@ public void ResetColor()
5859 {
5960 }
6061
62+ public string GetOutput ( )
63+ {
64+ return _testWriter . GetOutput ( ) ;
65+ }
66+
67+ public void ClearOutput ( )
68+ {
69+ _testWriter . ClearOutput ( ) ;
70+ }
71+
6172 private class TestOutputWriter : TextWriter
6273 {
6374 private readonly ITestOutputHelper _output ;
6475 private readonly StringBuilder _sb = new StringBuilder ( ) ;
76+ private readonly StringBuilder _currentOutput = new StringBuilder ( ) ;
6577
6678 public TestOutputWriter ( ITestOutputHelper output )
6779 {
@@ -83,8 +95,19 @@ public override void Write(char value)
8395 else
8496 {
8597 _sb . Append ( value ) ;
98+ _currentOutput . Append ( value ) ;
8699 }
87100 }
101+
102+ public string GetOutput ( )
103+ {
104+ return _currentOutput . ToString ( ) ;
105+ }
106+
107+ public void ClearOutput ( )
108+ {
109+ _currentOutput . Clear ( ) ;
110+ }
88111 }
89112 }
90113}
0 commit comments