This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
StringExtensions.cs
252 lines (234 loc) · 9.86 KB
/
StringExtensions.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
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
// <copyright file="StringExtensions.cs" company="xBehave.net contributors">
// Copyright (c) xBehave.net contributors. All rights reserved.
// </copyright>
namespace Xbehave
{
using System;
using System.Diagnostics.CodeAnalysis;
using Xbehave.Fluent;
/// <summary>
/// Provides access to step definition methods.
/// </summary>
public static partial class StringExtensions
{
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep Given(this string text, Action body)
{
return new Step(text, body, StepType.Given);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep When(this string text, Action body)
{
return new Step(text, body, StepType.When);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep Then(this string text, Action body)
{
return new Step(text, body, StepType.Then);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep And(this string text, Action body)
{
return new Step(text, body, StepType.And);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep But(this string text, Action body)
{
return new Step(text, body, StepType.But);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "f", Justification = "Fluent API")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "f", Justification = "Fluent API")]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Fluent API")]
public static IStep f(this string text, Action body)
{
var stepType = GetStepType(text);
return new Step(text, body, stepType);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "_", Justification = "Fluent API")]
[SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Fluent API")]
[CLSCompliant(false)]
public static IStep _(this string text, Action body)
{
var stepType = GetStepType(text);
return new Step(text, body, stepType);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep Given(this string text, Action<IStepContext> body)
{
return new Step(text, body, StepType.Given);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep When(this string text, Action<IStepContext> body)
{
return new Step(text, body, StepType.When);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep Then(this string text, Action<IStepContext> body)
{
return new Step(text, body, StepType.Then);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep And(this string text, Action<IStepContext> body)
{
return new Step(text, body, StepType.And);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
public static IStep But(this string text, Action<IStepContext> body)
{
return new Step(text, body, StepType.But);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "f", Justification = "Fluent API")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "f", Justification = "Fluent API")]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Fluent API")]
public static IStep f(this string text, Action<IStepContext> body)
{
var stepType = GetStepType(text);
return new Step(text, body, stepType);
}
/// <summary>
/// Defines a step in the current scenario.
/// </summary>
/// <param name="text">The step text.</param>
/// <param name="body">The action that will perform the step.</param>
/// <returns>
/// An instance of <see cref="IStep"/>.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "_", Justification = "Fluent API")]
[SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Fluent API")]
[CLSCompliant(false)]
public static IStep _(this string text, Action<IStepContext> body)
{
var stepType = GetStepType(text);
return new Step(text, body, stepType);
}
/// <summary>
/// Get the appropriate step type based on the the text.
/// </summary>
/// <param name="text">The step text.</param>
/// <returns>The appropriate step type based on the text.</returns>
public static Xbehave.StepType GetStepType(string text)
{
var stepType = StepType.Any;
if (text == null)
{
return stepType;
}
var upperText = text.ToUpperInvariant();
if (upperText.StartsWith("GIVEN", StringComparison.Ordinal))
{
stepType = StepType.Given;
}
else if (upperText.StartsWith("WHEN", StringComparison.Ordinal))
{
stepType = StepType.When;
}
else if (upperText.StartsWith("THEN", StringComparison.Ordinal))
{
stepType = StepType.Then;
}
else if (upperText.StartsWith("AND", StringComparison.Ordinal))
{
stepType = StepType.And;
}
else if (upperText.StartsWith("BUT", StringComparison.Ordinal))
{
stepType = StepType.But;
}
return stepType;
}
}
}