-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTestConsole.cs
201 lines (156 loc) · 6.2 KB
/
TestConsole.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// copied & adapted from System.CommandLine
using System;
using System.Collections.Generic;
using System.Linq;
using CommandDotNet.Logging;
using CommandDotNet.Rendering;
namespace CommandDotNet.TestTools
{
/// <summary>
/// A test console that can be used to <br/>
/// - capture output <br/>
/// - provide piped input <br/>
/// - handle ReadLine and ReadToEnd requests
/// </summary>
public partial class TestConsole : IConsole
{
private readonly Action<TestConsole> _onClear;
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
public static class Default
{
public static ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
public static ConsoleColor ForegroundColor { get; set; } = ConsoleColor.White;
public static int WindowLeft { get; set; } = 0;
public static int WindowTop { get; set; } = 0;
public static int WindowWidth { get; set; } = 120;
public static int WindowHeight { get; set; } = 30;
public static int BufferWidth { get; set; } = WindowWidth;
public static int BufferHeight { get; set; } = WindowHeight;
}
public TestConsole(
Func<TestConsole, string?>? onReadLine = null,
IEnumerable<string>? pipedInput = null,
Func<TestConsole, ConsoleKeyInfo>? onReadKey = null,
Action<TestConsole> onClear = null)
{
_onClear = onClear;
IsInputRedirected = pipedInput != null;
if (pipedInput != null)
{
if (onReadLine != null)
{
throw new Exception($"{nameof(onReadLine)} and {nameof(pipedInput)} cannot both be specified. " +
"Windows will throw 'System.IO.IOException: The handle is invalid' on an attempt to ");
}
if (pipedInput is ICollection<string> inputs)
{
var queue = new Queue<string>(inputs);
onReadLine = console => queue.Count == 0 ? null : queue.Dequeue();
}
else
{
onReadLine = console => pipedInput.Take(1).FirstOrDefault();
}
}
var all = new TestConsoleWriter();
All = all;
Out = new TestConsoleWriter(all);
Error = new TestConsoleWriter(all);
In = new TestConsoleReader(Out,
() =>
{
var input = onReadLine?.Invoke(this);
Log.Info($"IConsole.ReadLine > {input}");
return input;
},
onReadKey switch
{
{ } _ => () => onReadKey!.Invoke(this),
_ => null
});
}
public string Title { get; set; }
#region IStandardError
public IConsoleWriter Error { get; }
public bool IsErrorRedirected { get; } = false;
#endregion
#region IStandardOut
public IConsoleWriter Out { get; }
public bool IsOutputRedirected { get; } = false;
#endregion
#region IStandardIn
public IConsoleReader In { get; }
public bool IsInputRedirected { get; }
#endregion
/// <summary>
/// This is the combined output for <see cref="Error"/> and <see cref="Out"/> in the order the lines were output.
/// </summary>
public IConsoleWriter All { get; }
/// <summary>
/// The combination of <see cref="Console.Error"/> and <see cref="Console.Out"/>
/// in the order they were written from the app.<br/>
/// This is how the output would appear in the shell.
/// </summary>
public string AllText() => All.ToString();
/// <summary>
/// The accumulated text of the <see cref="Console.Out"/> stream.
/// </summary>
public string OutText() => Out.ToString();
/// <summary>
/// The accumulated text of the <see cref="Console.Error"/> stream.
/// </summary>
public string ErrorText() => Error.ToString();
public string OutLastLine => Out.ToString().SplitIntoLines().Last();
#region IConsoleColor
public ConsoleColor BackgroundColor { get; set; } = Default.BackgroundColor;
public ConsoleColor ForegroundColor { get; set; } = Default.ForegroundColor;
public void ResetColor()
{
BackgroundColor = Default.BackgroundColor;
ForegroundColor = Default.ForegroundColor;
}
#endregion
#region IConsoleBuffer
public int BufferWidth { get; set; } = Default.BufferWidth;
public int BufferHeight { get; set; } = Default.BufferHeight;
public void SetBufferSize(int width, int height)
{
BufferWidth = width;
BufferHeight = height;
}
public void Clear()
{
_onClear?.Invoke(this);
}
#endregion
#region IConsoleWindow
public int WindowLeft { get; set; } = Default.WindowLeft;
public int WindowTop { get; set; } = Default.WindowTop;
public int WindowWidth { get; set; } = Default.WindowWidth;
public int WindowHeight { get; set; } = Default.WindowHeight;
public void SetWindowPosition(int left, int top)
{
WindowLeft = left;
WindowTop = top;
}
public void SetWindowSize(int width, int height)
{
WindowWidth = width;
WindowHeight = height;
}
#endregion
#region IConsoleCursor
public int CursorSize { get; set; }
public bool CursorVisible { get; set; }
public int CursorLeft { get; set; }
public int CursorTop { get; set; }
public void SetCursorPosition(int left, int top)
{
CursorLeft = left;
CursorTop = top;
}
#endregion
}
}