-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShouldBeStringTestExtensions.cs
187 lines (163 loc) · 5.56 KB
/
ShouldBeStringTestExtensions.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
using System.Diagnostics;
using System.Text.RegularExpressions;
using NUnit.Framework;
namespace ShouldBe
{
/// <summary/>
[DebuggerStepThrough]
[ShouldBeMethods]
public static class ShouldBeStringTestExtensions
{
/// <summary>
/// Strip out whitespace (whitespace, tabs, line-endings, etc) and compare the two strings
/// </summary>
/// <param name="actual"></param>
/// <param name="expected"></param>
public static string ShouldBeCloseTo(this string actual, string expected)
{
var strippedActual = actual == null ? "null" : actual.Quotify().StripWhitespace();
var strippedExpected = expected == null ? "null" : expected.Quotify().StripWhitespace();
if (strippedActual != strippedExpected)
{
ShouldBeMessage.Fail(actual, expected);
}
return actual;
}
/// <summary>
/// Asserts that a string starts with the <paramref name="startsWith"/> substring.
/// </summary>
/// <param name="value"></param>
/// <param name="startsWith"></param>
/// <returns></returns>
public static string ShouldStartWith(this string value, string startsWith)
{
if (!value.StartsWith(startsWith))
{
ShouldBeMessage.Fail(value, startsWith);
}
return value;
}
/// <summary>
/// Asserts that a string ends with the <paramref name="endsWith"/> substring.
/// </summary>
/// <param name="value"></param>
/// <param name="endsWith"></param>
/// <returns></returns>
public static string ShouldEndWith(this string value, string endsWith)
{
if (!value.EndsWith(endsWith))
{
ShouldBeMessage.Fail(value, endsWith);
}
return value;
}
/// <summary>
/// Asserts that a string contains a given substring. Matching is case sensitive.
/// </summary>
/// <param name="value"></param>
/// <param name="contains"></param>
/// <returns></returns>
public static string ShouldContain(this string value, string contains)
{
if (!value.Contains(contains))
{
ShouldBeMessage.Fail(value, contains);
}
return value;
}
/// <summary>
/// Asserts that a string does not contains a given substring. Matching is case sensitive.
/// </summary>
/// <param name="value"></param>
/// <param name="notContains"></param>
/// <returns></returns>
public static string ShouldNotContain(this string value, string notContains)
{
if (value.Contains(notContains))
{
ShouldBeMessage.Fail(value, notContains);
}
return value;
}
/// <summary>
/// Asserts that a string matches a regular expression.
/// </summary>
/// <param name="value"></param>
/// <param name="regexPattern"></param>
/// <returns></returns>
public static string ShouldMatch(this string value, string regexPattern)
{
if (!Regex.IsMatch(value, regexPattern))
{
ShouldBeMessage.Fail(value, regexPattern);
}
return value;
}
/// <summary>
/// Asserts that a string does not match a regular expression.
/// </summary>
/// <param name="value"></param>
/// <param name="regexPattern"></param>
/// <returns></returns>
public static string ShouldNotMatch(this string value, string regexPattern)
{
if (Regex.IsMatch(value, regexPattern))
{
ShouldBeMessage.Fail(value, regexPattern);
}
return value;
}
/// <summary>
/// Asserts that a string should be null or empty
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ShouldBeNullOrEmpty(this string value)
{
if (!string.IsNullOrEmpty(value))
{
ShouldBeMessage.FailActual(value);
}
return value;
}
/// <summary>
/// Asserts that a string should not be null or empty
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ShouldNotBeNullOrEmpty(this string value)
{
if (string.IsNullOrEmpty(value))
{
ShouldBeMessage.FailActual(value);
}
return value;
}
/// <summary>
/// Asserts that a string should be null or empty
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ShouldBeNullOrWhiteSpace(this string value)
{
if (!string.IsNullOrWhiteSpace(value))
{
ShouldBeMessage.FailActual(value);
}
return value;
}
/// <summary>
/// Asserts that a string should not be null or empty
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ShouldNotBeNullOrWhiteSpace(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
ShouldBeMessage.FailActual(value);
}
return value;
}
}
}