| 1 |
@sequential |
| 2 |
Feature: Parse data types in step parameters (type transformation) |
| 3 |
|
| 4 |
As a test writer |
| 5 |
I want write steps with parameter that should have floating-point data types |
| 6 |
So that I can just use the step parameter with the correct type |
| 7 |
|
| 8 |
behave uses the parse module (inverse of Python string.format). |
| 9 |
Therefore, the following ``parse types`` for floats are already supported: |
| 10 |
|
| 11 |
===== =========================================== ============= ================================ |
| 12 |
Type Characters Matched (regex class) Output Type Example(s) |
| 13 |
===== =========================================== ============= ================================ |
| 14 |
% Percentage (converted to value/100.0) float 51.2% |
| 15 |
f Fixed-point numbers float 1.23 -1.45 |
| 16 |
e Floating-point numbers with exponent float 1.1e-10 -12.3E+5 |
| 17 |
g General number format (either d, f or e) float 123 1.23 -1.45E+12 |
| 18 |
===== =========================================== ============= ================================ |
| 19 |
|
| 20 |
|
| 21 |
SEE ALSO: |
| 22 |
* http://pypi.python.org/pypi/parse |
| 23 |
* string.format: http://docs.python.org/library/string.html#format-string-syntax |
| 24 |
|
| 25 |
|
| 26 |
@setup |
| 27 |
Scenario: Feature Setup |
| 28 |
Given a new working directory |
| 29 |
And a file named "features/steps/float_param_steps.py" with: |
| 30 |
""" |
| 31 |
from behave import then, step |
| 32 |
class NotMatched(object): pass |
| 33 |
|
| 34 |
@step('a float param with "{value:g}"') |
| 35 |
def step_float_param_with(context, value): |
| 36 |
assert type(value) is float |
| 37 |
context.value = value |
| 38 |
|
| 39 |
@step('a float param with "{value}"') |
| 40 |
def step_float_param_otherwise(context, value): |
| 41 |
context.value = NotMatched |
| 42 |
|
| 43 |
@step('a generic float param with "{value:g}"') |
| 44 |
def step_generic_float_param_with(context, value): |
| 45 |
step_float_param_with(context, value) |
| 46 |
|
| 47 |
@step('a generic float param with "{value}"') |
| 48 |
def step_generic_float_param_otherwise(context, value): |
| 49 |
step_float_param_otherwise(context, value) |
| 50 |
|
| 51 |
@step('a float with exponent param with "{value:e}"') |
| 52 |
def step_float_with_exponent_param_with(context, value): |
| 53 |
step_float_param_with(context, value) |
| 54 |
|
| 55 |
@step('a float with exponent param with "{value}"') |
| 56 |
def step_float_with_exponent_param_otherwise(context, value): |
| 57 |
step_float_param_otherwise(context, value) |
| 58 |
|
| 59 |
@step('a percentage param with "{value:%}"') |
| 60 |
def step_percentage_param_with(context, value): |
| 61 |
step_float_param_with(context, value) |
| 62 |
|
| 63 |
@step('a percentage param with "{value}"') |
| 64 |
def step_percentage_param_otherwise(context, value): |
| 65 |
step_float_param_otherwise(context, value) |
| 66 |
|
| 67 |
@step('a fixed-point number param with "{value:f}"') |
| 68 |
def step_fixed_point_number_param_with(context, value): |
| 69 |
step_float_param_with(context, value) |
| 70 |
|
| 71 |
@step('a fixed-point number param with "{value}"') |
| 72 |
def step_fixed_point_number_param_otherwise(context, value): |
| 73 |
step_float_param_otherwise(context, value) |
| 74 |
|
| 75 |
@then('the value should be {outcome} as float number') |
| 76 |
def step_value_should_be_matched_as_float_number(context, outcome): |
| 77 |
expected_type = float |
| 78 |
if outcome == "matched": |
| 79 |
assert type(context.value) is expected_type, \ |
| 80 |
"Unexpected type: %s" % type(context.value) |
| 81 |
else: |
| 82 |
assert context.value is NotMatched |
| 83 |
|
| 84 |
@then('the value should be {outcome} as float number with "{expected:g}"') |
| 85 |
def step_value_should_be_matched_as_float_number_with_expected(context, outcome, expected): |
| 86 |
step_value_should_be_matched_as_float_number(context, outcome) |
| 87 |
assert context.value == expected, \ |
| 88 |
"FAILED: value(%s) == expected(%s)" % (context.value, expected) |
| 89 |
""" |
| 90 |
|
| 91 |
|
| 92 |
Scenario: Float parameter values with type "%" (percentage) |
| 93 |
Given a file named "features/example.float_param.with_percent.feature" with: |
| 94 |
""" |
| 95 |
Feature: Float parameter values with type "%" (percentage) |
| 96 |
|
| 97 |
Scenario Outline: Good cases |
| 98 |
Given a percentage param with "<Value>" |
| 99 |
Then the value should be matched as float number with "<Expected Value>" |
| 100 |
|
| 101 |
Examples: |
| 102 |
| Value | Expected Value | Case | |
| 103 |
| 0% | 0 | Zero | |
| 104 |
| 20% | 0.2 | Positive number | |
| 105 |
| 120% | 1.2 | Larger than 100% | |
| 106 |
| 10.5% | 0.105 | Float number | |
| 107 |
| -10% | -0.1 | Negative number | |
| 108 |
| +10% | 0.1 | With plus sign | |
| 109 |
|
| 110 |
|
| 111 |
Scenario Outline: Bad cases (not matched) |
| 112 |
Given a percentage param with "<Value>" |
| 113 |
Then the value should be <Outcome> as float number |
| 114 |
|
| 115 |
Examples: |
| 116 |
| Value | Outcome | Reason | |
| 117 |
| 123 | not-matched | Percentage sign is missing | |
| 118 |
| 1.23 | not-matched | Float number without percentage sign | |
| 119 |
| Alice | not-matched | Not a number | |
| 120 |
""" |
| 121 |
When I run "behave -f plain features/example.float_param.with_percent.feature" |
| 122 |
Then it should pass with: |
| 123 |
""" |
| 124 |
9 scenarios passed, 0 failed, 0 skipped |
| 125 |
""" |
| 126 |
|
| 127 |
|
| 128 |
Scenario: Fixed-point parameter values with type "f" |
| 129 |
Given a file named "features/example.float_param.with_type_f.feature" with: |
| 130 |
""" |
| 131 |
Feature: Float parameter values with type "f" (fixed-point number) |
| 132 |
|
| 133 |
Scenario Outline: Good cases |
| 134 |
Given a fixed-point number param with "<Value>" |
| 135 |
Then the value should be matched as float number with "<Expected Value>" |
| 136 |
|
| 137 |
Examples: |
| 138 |
| Value | Expected Value | Case | |
| 139 |
| 0.23 | 0.23 | | |
| 140 |
| 1.23 | 1.23 | | |
| 141 |
| 123.45 | 123.45 | | |
| 142 |
| +1.23 | 1.23 | With plus sign | |
| 143 |
| -1.23 | -1.23 | Negative float | |
| 144 |
|
| 145 |
|
| 146 |
Scenario Outline: Bad cases (not matched) |
| 147 |
Given a fixed-point number param with "<Value>" |
| 148 |
Then the value should be <Outcome> as float number |
| 149 |
|
| 150 |
Examples: |
| 151 |
| Value | Outcome | Reason | |
| 152 |
| 123 | not-matched | Integer number | |
| 153 |
| 1.23E-7 | not-matched | Float number with exponent | |
| 154 |
| Alice | not-matched | Not a number | |
| 155 |
""" |
| 156 |
When I run "behave -f plain features/example.float_param.with_type_f.feature" |
| 157 |
Then it should pass with: |
| 158 |
""" |
| 159 |
8 scenarios passed, 0 failed, 0 skipped |
| 160 |
""" |
| 161 |
|
| 162 |
|
| 163 |
Scenario: Float with exponent parameter values with type "e" |
| 164 |
Given a file named "features/example.float_param.with_type_e.feature" with: |
| 165 |
""" |
| 166 |
Feature: Float parameter values with type "e" (float with exponents) |
| 167 |
|
| 168 |
Scenario Outline: Good cases |
| 169 |
Given a float with exponent param with "<Value>" |
| 170 |
Then the value should be matched as float number with "<Expected Value>" |
| 171 |
|
| 172 |
Examples: |
| 173 |
| Value | Expected Value | Case | |
| 174 |
| 1.0E-10 | 1E-10 | With mantisse, negative exponent | |
| 175 |
| 1.23E+5 | 123E3 | Exponent with plus sign | |
| 176 |
| 123.0E+3 | 1.23E5 | Exponent with plus sign | |
| 177 |
| -1.23E5 | -123E3 | Negative number with mantisse and exponent | |
| 178 |
| INF | +INF | Infinity (INF) | |
| 179 |
| +INF | INF | | |
| 180 |
| -INF | -INF | | |
| 181 |
| +inf | INF | Lower-case special names | |
| 182 |
|
| 183 |
|
| 184 |
Scenario Outline: Bad cases (not matched) |
| 185 |
Given a float with exponent param with "<Value>" |
| 186 |
Then the value should be <Outcome> as float number |
| 187 |
|
| 188 |
Examples: |
| 189 |
| Value | Outcome | Reason | |
| 190 |
| 1E10 | not-matched | Without mantissa | |
| 191 |
| 1.E10 | not-matched | Short mantissa | |
| 192 |
| 123 | not-matched | Integer number | |
| 193 |
| Alice | not-matched | Not a number | |
| 194 |
""" |
| 195 |
When I run "behave -f plain features/example.float_param.with_type_e.feature" |
| 196 |
Then it should pass with: |
| 197 |
""" |
| 198 |
12 scenarios passed, 0 failed, 0 skipped |
| 199 |
""" |
| 200 |
|
| 201 |
|
| 202 |
Scenario: Generic float parameter values with type "g" |
| 203 |
Given a file named "features/example.float_param.with_type_g.feature" with: |
| 204 |
""" |
| 205 |
Feature: Float parameter values with type "g" (generic float) |
| 206 |
|
| 207 |
Scenario Outline: Good cases |
| 208 |
Given a generic float param with "<Value>" |
| 209 |
Then the value should be matched as float number with "<Expected Value>" |
| 210 |
|
| 211 |
Examples: |
| 212 |
| Value | Expected Value | Case | |
| 213 |
| 1 | 1.0 | Integer number format | |
| 214 |
| 1E10 | 1.0E10 | Float with exponent and shortened mantissa | |
| 215 |
| 1.23E5 | 1.23E5 | Float with exponent and mantissa | |
| 216 |
| 1.23e5 | 1.23E5 | Float with exponent and mantissa (lower-case) | |
| 217 |
| 1.0E-10 | 1E-10 | With mantisse, negative exponent | |
| 218 |
| 1.23E+5 | 123E3 | Exponent with plus sign | |
| 219 |
| 123.0E+3 | 1.23E5 | Exponent with plus sign | |
| 220 |
| -1.23E5 | -123E3 | Negative number with mantisse and exponent | |
| 221 |
|
| 222 |
|
| 223 |
Scenario Outline: Bad cases (not matched) |
| 224 |
Given a generic float param with "<Value>" |
| 225 |
Then the value should be <Outcome> as float number |
| 226 |
|
| 227 |
Examples: |
| 228 |
| Value | Outcome | Reason | |
| 229 |
| 0b101 | not-matched | Binary number | |
| 230 |
| 0o17 | not-matched | Octal number | |
| 231 |
| 0x1A | not-matched | Hexadecimal number | |
| 232 |
| 1.E10 | not-matched | Short mantissa | |
| 233 |
| Alice | not-matched | Not a number | |
| 234 |
""" |
| 235 |
When I run "behave -f plain features/example.float_param.with_type_g.feature" |
| 236 |
Then it should pass with: |
| 237 |
""" |
| 238 |
13 scenarios passed, 0 failed, 0 skipped |
| 239 |
""" |