|
6 | 6 | using TestStack.BDDfy.Core;
|
7 | 7 | using TestStack.BDDfy.Scanners.ScenarioScanners;
|
8 | 8 |
|
| 9 | +#if !NET35 |
| 10 | +using System.Threading.Tasks; |
| 11 | +#endif |
| 12 | + |
9 | 13 | namespace TestStack.BDDfy.Scanners.StepScanners.Fluent
|
10 | 14 | {
|
11 | 15 | /// <summary>
|
@@ -103,6 +107,139 @@ IAndGiven<TScenario> IGiven<TScenario>.And(Expression<Action<TScenario>> andGive
|
103 | 107 | {
|
104 | 108 | return ((IGiven<TScenario>)this).And(andGivenStep, null);
|
105 | 109 | }
|
| 110 | +#else |
| 111 | + IGiven<TScenario> IInitialStep<TScenario>.Given(Expression<Func<TScenario, Task>> givenStep, string stepTextTemplate) |
| 112 | + { |
| 113 | + AddStep(givenStep, stepTextTemplate, false, ExecutionOrder.SetupState); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + IWhen<TScenario> IInitialStep<TScenario>.When(Expression<Func<TScenario, Task>> whenStep, string stepTextTemplate) |
| 118 | + { |
| 119 | + AddStep(whenStep, stepTextTemplate, false, ExecutionOrder.Transition); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + IGiven<TScenario> IInitialStep<TScenario>.Given(Expression<Func<TScenario, Task>> givenStep, bool includeInputsInStepTitle) |
| 124 | + { |
| 125 | + AddStep(givenStep, null, false, ExecutionOrder.SetupState, includeInputsInStepTitle: includeInputsInStepTitle); |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + IWhen<TScenario> IInitialStep<TScenario>.When(Expression<Func<TScenario, Task>> whenStep, bool includeInputsInStepTitle) |
| 130 | + { |
| 131 | + AddStep(whenStep, null, false, ExecutionOrder.Transition, includeInputsInStepTitle: includeInputsInStepTitle); |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + IThen<TScenario> IWhen<TScenario>.Then(Expression<Func<TScenario, Task>> thenStep, string stepTextTemplate) |
| 136 | + { |
| 137 | + AddStep(thenStep, stepTextTemplate, true, ExecutionOrder.Assertion); |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + IAndThen<TScenario> IThen<TScenario>.And(Expression<Func<TScenario, Task>> andThenStep, string stepTextTemplate) |
| 142 | + { |
| 143 | + AddStep(andThenStep, stepTextTemplate, true, ExecutionOrder.ConsecutiveAssertion); |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + IAndThen<TScenario> IThen<TScenario>.And(Expression<Func<TScenario, Task>> andThenStep, bool includeInputsInStepTitle) |
| 148 | + { |
| 149 | + AddStep(andThenStep, null, true, ExecutionOrder.ConsecutiveAssertion, includeInputsInStepTitle: includeInputsInStepTitle); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + IAndWhen<TScenario> IWhen<TScenario>.And(Expression<Func<TScenario, Task>> andWhenStep, bool includeInputsInStepTitle) |
| 154 | + { |
| 155 | + AddStep(andWhenStep, null, false, ExecutionOrder.ConsecutiveTransition, includeInputsInStepTitle: includeInputsInStepTitle); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + IThen<TScenario> IWhen<TScenario>.Then(Expression<Func<TScenario, Task>> thenStep, bool includeInputsInStepTitle) |
| 160 | + { |
| 161 | + AddStep(thenStep, null, true, ExecutionOrder.Assertion, includeInputsInStepTitle: includeInputsInStepTitle); |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + IAndGiven<TScenario> IGiven<TScenario>.And(Expression<Func<TScenario, Task>> andGivenStep, bool includeInputsInStepTitle) |
| 166 | + { |
| 167 | + AddStep(andGivenStep, null, false, ExecutionOrder.ConsecutiveSetupState, includeInputsInStepTitle: includeInputsInStepTitle); |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + IThen<TScenario> IGiven<TScenario>.Then(Expression<Func<TScenario, Task>> thenStep, bool includeInputsInStepTitle) |
| 172 | + { |
| 173 | + AddStep(thenStep, null, true, ExecutionOrder.Assertion, includeInputsInStepTitle: includeInputsInStepTitle); |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + IAndGiven<TScenario> IGiven<TScenario>.And(Expression<Func<TScenario, Task>> andGivenStep, string stepTextTemplate) |
| 178 | + { |
| 179 | + AddStep(andGivenStep, stepTextTemplate, false, ExecutionOrder.ConsecutiveSetupState); |
| 180 | + return this; |
| 181 | + } |
| 182 | + |
| 183 | + IAndWhen<TScenario> IWhen<TScenario>.And(Expression<Func<TScenario, Task>> andWhenStep, string stepTextTemplate) |
| 184 | + { |
| 185 | + AddStep(andWhenStep, stepTextTemplate, false, ExecutionOrder.ConsecutiveTransition); |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + IThen<TScenario> IGiven<TScenario>.Then(Expression<Func<TScenario, Task>> thenStep, string stepTextTemplate) |
| 190 | + { |
| 191 | + AddStep(thenStep, stepTextTemplate, true, ExecutionOrder.Assertion); |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
| 195 | + IWhen<TScenario> IGiven<TScenario>.When(Expression<Func<TScenario, Task>> whenStep, bool includeInputsInStepTitle) |
| 196 | + { |
| 197 | + AddStep(whenStep, null, false, ExecutionOrder.Transition, includeInputsInStepTitle: includeInputsInStepTitle); |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + IWhen<TScenario> IGiven<TScenario>.When(Expression<Func<TScenario, Task>> whenStep, string stepTextTemplate) |
| 202 | + { |
| 203 | + AddStep(whenStep, stepTextTemplate, false, ExecutionOrder.Transition); |
| 204 | + return this; |
| 205 | + } |
| 206 | + |
| 207 | + IFluentScanner<TScenario> IFluentScanner<TScenario>.TearDownWith(Expression<Func<TScenario, Task>> tearDownStep) |
| 208 | + { |
| 209 | + AddStep(tearDownStep, null, false, ExecutionOrder.TearDown, false); |
| 210 | + return this; |
| 211 | + } |
| 212 | + |
| 213 | + private void AddStep(Expression<Func<TScenario, Task>> stepAction, string stepTextTemplate, bool asserts, ExecutionOrder executionOrder, bool reports = true, bool includeInputsInStepTitle = true) |
| 214 | + { |
| 215 | + var methodInfo = GetMethodInfo(stepAction); |
| 216 | + var inputArguments = new object[0]; |
| 217 | + if (includeInputsInStepTitle) |
| 218 | + { |
| 219 | + inputArguments = stepAction.ExtractConstants().ToArray(); |
| 220 | + } |
| 221 | + |
| 222 | + var flatInputArray = inputArguments.FlattenArrays(); |
| 223 | + var stepTitle = NetToString.Convert(methodInfo.Name); |
| 224 | + |
| 225 | + if (!string.IsNullOrEmpty(stepTextTemplate)) |
| 226 | + stepTitle = string.Format(stepTextTemplate, flatInputArray); |
| 227 | + else if (includeInputsInStepTitle) |
| 228 | + { |
| 229 | + var stringFlatInputs = flatInputArray.Select(i => i.ToString()).ToArray(); |
| 230 | + stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs); |
| 231 | + } |
| 232 | + |
| 233 | + stepTitle = stepTitle.Trim(); |
| 234 | + var action = stepAction.Compile(); |
| 235 | + _steps.Add(new ExecutionStep(StepActionFactory.GetStepAction(action), stepTitle, asserts, executionOrder, reports)); |
| 236 | + } |
| 237 | + |
| 238 | + private static MethodInfo GetMethodInfo(Expression<Func<TScenario, Task>> stepAction) |
| 239 | + { |
| 240 | + var methodCall = (MethodCallExpression)stepAction.Body; |
| 241 | + return methodCall.Method; |
| 242 | + } |
106 | 243 | #endif
|
107 | 244 |
|
108 | 245 | private void AddStep(Expression<Action<TScenario>> stepAction, string stepTextTemplate, bool asserts, ExecutionOrder executionOrder, bool reports = true, bool includeInputsInStepTitle = true)
|
|
0 commit comments