-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeConsole.cs
More file actions
82 lines (62 loc) · 2.32 KB
/
FakeConsole.cs
File metadata and controls
82 lines (62 loc) · 2.32 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Help you to better test dotnet console apps
/// by providing all lines which will be read from user at runtime,
/// and all lines which will be written to user at runtime
/// then validate the actual lines written by the program by the expected one
/// </summary>
public static class FakeConsole
{
/// <summary>
/// Paste the text which should be splitted by lines at runtime, to be used in FakeConsole.ReadLine
/// </summary>
public static string ReadLines = @"";
/// <summary>
/// Paste the text which should be splitted by lines at runtime, to compare it with the ones written through FakeConsole.WriteLine
/// </summary>
public static string ExpectedWriteLines = @"";
/// <summary>
/// Reads next line from the ones set at <see cref="ReadLines"/>
/// </summary>
/// <returns></returns>
public static string ReadLine()
{
if (_readLinesQueue == null)
{
_readLinesQueue = new Queue<string>();
var lines = ReadLines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (var line in lines)
_readLinesQueue.Enqueue(line);
}
return _readLinesQueue.Dequeue();
}
/// <summary>
/// Store the passed line in <see cref="FakeConsole"/>, then emit it to <see cref="System.Console.WriteLine"/>
/// </summary>
/// <param name="line"></param>
public static void WriteLine(object line)
{
_writeLines.Add(line.ToString());
System.Console.WriteLine(line);
}
/// <summary>
/// Compares expected lines with the actual ones written at runtime
/// </summary>
/// <returns></returns>
public static bool Validate()
{
var expectedLines = ExpectedWriteLines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
if (_writeLines.Count != expectedLines.Length)
return false;
bool isValid = true;
for (var i = 0; i < _writeLines.Count; i++)
isValid &= _writeLines[i] == expectedLines[i];
return isValid;
}
static List<string> _writeLines = new List<string>();
static Queue<string> _readLinesQueue = null;
}