-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleSimile.cs
More file actions
358 lines (316 loc) · 9.83 KB
/
ConsoleSimile.cs
File metadata and controls
358 lines (316 loc) · 9.83 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleCompare
{
/// <summary>
/// Represents a set of console IO for comparison
/// </summary>
internal class ConsoleSimile
{
// Simple list of all lines in order
private List<SimileLine> allLines;
/// <summary>
/// Gets the count of all lines (input and output) in the simile
/// </summary>
public int Count => allLines.Count;
/// <summary>
/// Gets a line, either input or output, from the simile
/// </summary>
/// <param name="index">Index of the line to retrieve</param>
/// <returns>A simile line, either input or output</returns>
public SimileLine this[int index] => allLines[index];
/// <summary>
/// Creates a new, empty console simile
/// </summary>
public ConsoleSimile()
{
allLines = new List<SimileLine>();
}
/// <summary>
/// Adds a simple text line to the list of output
/// </summary>
/// <param name="text">Output text</param>
/// <param name="lineEnding">Type of line ending for this output</param>
public void AddOutput(string text, LineEndingType lineEnding = LineEndingType.NewLine)
{
SimileLineOutput output = new SimileLineOutput(text, lineEnding);
allLines.Add(output);
}
/// <summary>
/// Adds the given output line to the simile
/// </summary>
/// <param name="output">Output line object to add</param>
public void AddOutput(SimileLineOutput output)
{
allLines.Add(output);
}
/// <summary>
/// Adds a simple string for input
/// </summary>
/// <param name="text">Input text</param>
public void AddInput(string text)
{
SimileLineInput input = new SimileLineInput(text);
allLines.Add(input);
}
/// <summary>
/// Static helper for loading an entire simile from a file
/// </summary>
/// <param name="filePath">The file path for the simile file</param>
/// <returns>A new simile loaded from the file</returns>
public static ConsoleSimile LoadFromFile(string filePath)
{
// Read the lines from the file
string[] lines = null;
try
{
lines = File.ReadAllLines(filePath);
}
catch (Exception e)
{
throw new InvalidOperationException("Unable to load simile from file", e);
}
// Parse whatever we read
return SimileParser.Parse(lines);
}
}
/// <summary>
/// Possible line endings for simile output
/// </summary>
public enum LineEndingType
{
NewLine,
SameLine
}
/// <summary>
/// Base class for lines of IO
/// </summary>
internal abstract class SimileLine { }
/// <summary>
/// Simple line of input
/// </summary>
internal class SimileLineInput : SimileLine
{
public string Text { get; }
public SimileLineInput(string text) => Text = text;
}
/// <summary>
/// A single line of output
/// </summary>
internal class SimileLineOutput : SimileLine
{
// Overall output is made up of multiple output elements
private List<SimileOutputElement> outputElements;
/// <summary>
/// Gets the type of line ending for this output
/// </summary>
public LineEndingType LineEnding { get; }
/// <summary>
/// Gets the raw text of this line, containing any
/// numeric output tags
/// </summary>
public string RawText { get; }
/// <summary>
/// Creates a line of output for the simile
/// </summary>
/// <param name="rawText">The raw text of the line</param>
/// <param name="lineEnding">The type of line ending</param>
public SimileLineOutput(string rawText, LineEndingType lineEnding)
{
LineEnding = lineEnding;
RawText = rawText;
outputElements = new List<SimileOutputElement>();
}
/// <summary>
/// Adds a simple text element to the line
/// </summary>
/// <param name="text">Simple text data</param>
public void AddTextElement(string text)
{
outputElements.Add(new SimileOutputText(text));
}
/// <summary>
/// Adds a numeric element to the line
/// </summary>
/// <typeparam name="T">The type of data</typeparam>
/// <param name="type">The type of numeric data</param>
/// <param name="min">The inclusive minimum expected value, or null for no minimum</param>
/// <param name="max">The inclusive maximum expected value, or null for no maximum</param>
/// <param name="set">The set of valid values, or null for no expected values</param>
/// <param name="precision">The amount of precision for rounding, or null for no rounding</param>
public void AddNumericElement<T>(SimileNumericType type, T? min = null, T? max = null, List<T> set = null, int? precision = null)
where T : struct, IComparable
{
SimileOutputNumeric<T> number = new SimileOutputNumeric<T>(type)
{
Minimum = min,
Maximum = max,
Precision = precision
};
if(set != null)
number.ValueSet.AddRange(set);
outputElements.Add(number);
}
/// <summary>
/// Adds a complete numeric element to the line
/// </summary>
/// <typeparam name="T">The type of data</typeparam>
/// <param name="numericElement">The complete numeric element to add</param>
public void AddNumericElement<T>(SimileOutputNumeric<T> numericElement)
where T:struct, IComparable
{
outputElements.Add(numericElement);
}
/// <summary>
/// Compares the overall line (made up of all output elements in order)
/// to the given string
/// </summary>
/// <param name="comparison">String for comparison</param>
/// <returns>True if the lines are equivalent, false otherwise</returns>
public bool CompareLine(string comparison)
{
// Null is always incorrect
if (comparison == null)
return false;
// Loop through the elements in order and verify matches
foreach (SimileOutputElement element in outputElements)
{
if (!element.AtBeginningOf(comparison, out comparison))
return false;
}
// All elements matched
return true;
}
}
/// <summary>
/// Represents a single element of a larger output line
/// </summary>
internal abstract class SimileOutputElement {
/// <summary>
/// Determines if this element is at the beginning of the
/// given string and sends out the remainder of the string
/// </summary>
/// <param name="line">The line to check</param>
/// <param name="remainder">What's left after this element is removed</param>
/// <returns>True if this elements starts the line, false otherwise</returns>
public abstract bool AtBeginningOf(string line, out string remainder);
}
/// <summary>
/// Represents a single text element of a larger output line
/// </summary>
internal class SimileOutputText : SimileOutputElement
{
public string Text { get; }
public SimileOutputText(string text) => Text = text;
/// <summary>
/// Determines if this element is at the beginning of the
/// given string and sends out the remainder of the string
/// </summary>
/// <param name="line">The line to check</param>
/// <param name="remainder">What's left after this element is removed</param>
/// <returns>True if this elements starts the line, false otherwise</returns>
public override bool AtBeginningOf(string line, out string remainder)
{
bool starts = line.StartsWith(Text);
if (starts)
{
remainder = line.Substring(Text.Length);
return true;
}
else
{
remainder = line;
return false;
}
}
}
/// <summary>
/// Represents allowable numeric types for numeric simile elements
/// </summary>
internal enum SimileNumericType
{
Byte,
SignedByte,
Char,
Short,
UnsignedShort,
Int,
UnsignedInt,
Long,
UnsignedLong,
Float,
Double,
Unknown
}
/// <summary>
/// Represents a single numeric element of a larger output line
/// </summary>
/// <typeparam name="T">The numeric data type of the element</typeparam>
internal class SimileOutputNumeric<T> : SimileOutputElement
where T : struct, IComparable
{
public SimileNumericType NumericType { get; }
public T? Minimum { get; set; }
public T? Maximum { get; set; }
public List<T> ValueSet { get; }
public int? Precision { get; set; }
/// <summary>
/// Creates a numeric output element
/// </summary>
/// <param name="type">The data type of this element</param>
public SimileOutputNumeric(SimileNumericType type)
{
NumericType = type;
ValueSet = new List<T>();
}
/// <summary>
/// Determines if this element is at the beginning of the
/// given string and sends out the remainder of the string
/// </summary>
/// <param name="line">The line to check</param>
/// <param name="remainder">What's left after this element is removed</param>
/// <returns>True if this elements starts the line, false otherwise</returns>
public override bool AtBeginningOf(string line, out string remainder)
{
// Go up to the next space or the end (or a single character for characters)
int length = line.Length;
int space = line.IndexOf(' ');
if (NumericType == SimileNumericType.Char)
{
length = 1;
}
else if(space != -1)
{
length = space;
}
// Chop up the string and attempt a parse
string valString = line.Substring(0, length);
remainder = line.Substring(length);
T val = default;
try
{
val = (T)Convert.ChangeType(valString, typeof(T));
}
catch
{
remainder = line;
return false;
}
// Successful parse, so verify other options starting with precision (which will cascade to the others)
if (Precision.HasValue && (NumericType == SimileNumericType.Float || NumericType == SimileNumericType.Double))
{
// Process both floats and doubles the same (since the Math class is all doubles)
double dVal = Convert.ToDouble(val);
val = (T)Convert.ChangeType(Math.Round(dVal, Precision.Value), typeof(T));
}
// Other options
if (Minimum.HasValue && val.CompareTo(Minimum.Value) < 0) return false;
if (Maximum.HasValue && val.CompareTo(Maximum.Value) > 0) return false;
if (ValueSet != null && ValueSet.Count > 0 && !ValueSet.Contains(val)) return false;
// Adjust the remainder and success
remainder = line.Substring(length);
return true;
}
}
}