forked from FakeItEasy/FakeItEasy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OutputWriterExtensions.cs
106 lines (93 loc) · 3.73 KB
/
OutputWriterExtensions.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
namespace FakeItEasy
{
using System;
using System.Collections;
using System.Linq;
/// <summary>
/// Provides extensions for <see cref="IOutputWriter"/>.
/// </summary>
public static class OutputWriterExtensions
{
/// <summary>
/// Writes a new line to the writer.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <returns>The writer.</returns>
public static IOutputWriter WriteLine(this IOutputWriter writer)
{
Guard.AgainstNull(writer, nameof(writer));
writer.Write(Environment.NewLine);
return writer;
}
/// <summary>
/// Writes the format string to the writer.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="format">The format string to write.</param>
/// <param name="args">Replacements for the format string.</param>
/// <returns>The writer.</returns>
public static IOutputWriter Write(this IOutputWriter writer, string format, params object[] args)
{
Guard.AgainstNull(writer, nameof(writer));
writer.Write(string.Format(format, args));
return writer;
}
/// <summary>
/// Writes the specified object to the writer (using the ToString-method of the object).
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="value">The value to write to the writer.</param>
/// <returns>The writer.</returns>
public static IOutputWriter Write(this IOutputWriter writer, object value)
{
Guard.AgainstNull(writer, nameof(writer));
Guard.AgainstNull(value, nameof(value));
writer.Write(value.ToString());
return writer;
}
/// <summary>
/// Formats the specified argument values as strings and writes them to the output.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="values">The values to write to the writer.</param>
/// <returns>The writer.</returns>
internal static IOutputWriter WriteArgumentValues(this IOutputWriter writer, IEnumerable values)
{
Guard.AgainstNull(writer, nameof(writer));
Guard.AgainstNull(values, nameof(values));
var list = values.AsList();
if (list.Count <= 5)
{
writer.WriteArgumentValuesImpl(list);
}
else
{
writer.WriteArgumentValuesImpl(list.Take(2));
int remainingCount = list.Count - 4;
writer.Write($", … ({remainingCount} more elements) …, ");
writer.WriteArgumentValuesImpl(list.Skip(list.Count - 2));
}
return writer;
}
/// <summary>
/// Formats the specified argument values as strings and writes them to the output.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="values">The values to write to the writer.</param>
/// <returns>The writer.</returns>
private static IOutputWriter WriteArgumentValuesImpl(this IOutputWriter writer, IEnumerable values)
{
bool first = true;
foreach (var value in values)
{
if (!first)
{
writer.Write(", ");
}
writer.WriteArgumentValue(value);
first = false;
}
return writer;
}
}
}