| 1 |
@sequential |
| 2 |
Feature: Parse integer data types in step parameters (type transformation) |
| 3 |
|
| 4 |
As a test writer |
| 5 |
I want write steps with parameter that should have integer data types |
| 6 |
So that I can just use the step parameter with the correct type |
| 7 |
|
| 8 |
|
| 9 |
Behave uses the parse module (inverse of Python string.format). |
| 10 |
Therefore, the following ``parse types`` for integer numbers are already supported: |
| 11 |
|
| 12 |
===== =========================================== ============= ================================ |
| 13 |
Type Characters Matched (regex class) Output Type Example(s) |
| 14 |
===== =========================================== ============= ================================ |
| 15 |
d Digits (effectively integer numbers) int 12345 0b101 0o761 0x1ABE |
| 16 |
n Numbers with thousands separators (, or .) int 12,345 |
| 17 |
b Binary numbers int 10111 0b1011 |
| 18 |
o Octal numbers int 07654 0o123 |
| 19 |
x Hexadecimal numbers (lower and upper case) int DEADBEAF 0xBEEF |
| 20 |
===== =========================================== ============= ================================ |
| 21 |
|
| 22 |
SEE ALSO: |
| 23 |
* http://pypi.python.org/pypi/parse |
| 24 |
* string.format: http://docs.python.org/library/string.html#format-string-syntax |
| 25 |
|
| 26 |
|
| 27 |
@setup |
| 28 |
Scenario: Feature Setup |
| 29 |
Given a new working directory |
| 30 |
And a file named "features/steps/integer_param_steps.py" with: |
| 31 |
""" |
| 32 |
from behave import then, step |
| 33 |
from six import integer_types |
| 34 |
|
| 35 |
class NotMatched(object): pass |
| 36 |
|
| 37 |
@step('a integer param with "{value:d}"') |
| 38 |
def step_integer_param_with(context, value): |
| 39 |
assert isinstance(value, integer_types), "value.type=%s" % type(value) |
| 40 |
context.value = value |
| 41 |
|
| 42 |
@step('a integer param with "{value}"') |
| 43 |
def step_integer_param_otherwise(context, value): |
| 44 |
context.value = NotMatched |
| 45 |
|
| 46 |
@step('a number param with "{value:n}"') |
| 47 |
def step_number_param_with(context, value): |
| 48 |
step_integer_param_with(context, value) |
| 49 |
|
| 50 |
@step('a number param with "{value}"') |
| 51 |
def step_number_param_otherwise(context, value): |
| 52 |
step_integer_param_otherwise(context, value) |
| 53 |
|
| 54 |
@step('a binary number param with "{value:b}"') |
| 55 |
def step_binary_param_with(context, value): |
| 56 |
step_integer_param_with(context, value) |
| 57 |
|
| 58 |
@step('a binary number param with "{value}"') |
| 59 |
def step_binary_param_otherwise(context, value): |
| 60 |
step_integer_param_otherwise(context, value) |
| 61 |
|
| 62 |
@step('a octal number param with "{value:o}"') |
| 63 |
def step_hexadecimal_param_with(context, value): |
| 64 |
step_integer_param_with(context, value) |
| 65 |
|
| 66 |
@step('a octal number param with "{value}"') |
| 67 |
def step_hexadecimal_param_otherwise(context, value): |
| 68 |
step_integer_param_otherwise(context, value) |
| 69 |
|
| 70 |
@step('a hexadecimal number param with "{value:x}"') |
| 71 |
def step_hexadecimal_param_with(context, value): |
| 72 |
step_integer_param_with(context, value) |
| 73 |
|
| 74 |
@step('a hexadecimal number param with "{value}"') |
| 75 |
def step_hexadecimal_param_otherwise(context, value): |
| 76 |
step_integer_param_otherwise(context, value) |
| 77 |
|
| 78 |
@then('the value should be {outcome} as integer number') |
| 79 |
def step_value_should_be_matched_as_number(context, outcome): |
| 80 |
if outcome == "matched": |
| 81 |
assert isinstance(context.value, integer_types), \ |
| 82 |
"Unexpected type: %s" % type(context.value) |
| 83 |
else: |
| 84 |
assert context.value is NotMatched |
| 85 |
|
| 86 |
@then('the value should be {outcome} as integer number with "{expected:d}"') |
| 87 |
def step_value_should_be_matched_as_number_with_expected(context, outcome, expected): |
| 88 |
step_value_should_be_matched_as_number(context, outcome) |
| 89 |
assert context.value == expected, \ |
| 90 |
"FAILED: value(%s) == expected(%s)" % (context.value, expected) |
| 91 |
""" |
| 92 |
|
| 93 |
|
| 94 |
Scenario: Use type "d" (Digits) for integer params |
| 95 |
Given a file named "features/example.int_param.with_type_d.feature" with: |
| 96 |
""" |
| 97 |
Feature: Use type "d" (Digits) for integer params |
| 98 |
|
| 99 |
Scenario Outline: Good cases |
| 100 |
Given a integer param with "<Value>" |
| 101 |
Then the value should be matched as integer number with "<Expected Value>" |
| 102 |
|
| 103 |
Examples: |
| 104 |
| Value | Expected Value | Case | |
| 105 |
| -1 | -1 | Negative number | |
| 106 |
| +1 | 1 | With plus sign | |
| 107 |
| 0 | 0 | | |
| 108 |
| 1 | 1 | | |
| 109 |
| 10 | 10 | | |
| 110 |
| 42 | 42 | | |
| 111 |
|
| 112 |
|
| 113 |
Scenario Outline: Bad cases (not matched) |
| 114 |
Given a integer param with "<Value>" |
| 115 |
Then the value should be <Outcome> as integer number |
| 116 |
|
| 117 |
Examples: |
| 118 |
| Value | Outcome | Reason | |
| 119 |
| 1.23 | not-matched | Float number | |
| 120 |
| 1E+2 | not-matched | Float number | |
| 121 |
| Alice | not-matched | Not a number | |
| 122 |
|
| 123 |
|
| 124 |
Scenario Outline: Conversion from other number system (base=2, 8, 16) |
| 125 |
Given a integer param with "<Value>" |
| 126 |
Then the value should be matched as integer number with "<Expected Value>" |
| 127 |
|
| 128 |
Examples: |
| 129 |
| Value | Expected Value | Case | |
| 130 |
| 0b1011 | 11 | Binary number | |
| 131 |
| 0o123 | 83 | Octal number | |
| 132 |
| 0xBEEF | 48879 | Hexadecimal number | |
| 133 |
""" |
| 134 |
When I run "behave -f plain features/example.int_param.with_type_d.feature" |
| 135 |
Then it should pass with: |
| 136 |
""" |
| 137 |
12 scenarios passed, 0 failed, 0 skipped |
| 138 |
""" |
| 139 |
|
| 140 |
Scenario: Use type "n" (Number) for integer params |
| 141 |
|
| 142 |
This data type supports numbers with thousands separators (',' or '.'). |
| 143 |
|
| 144 |
Given a file named "features/example.int_param.with_type_n.feature" with: |
| 145 |
""" |
| 146 |
Feature: Use type "n" (Number) for integer params |
| 147 |
|
| 148 |
Scenario Outline: Good cases |
| 149 |
Given a number param with "<Value>" |
| 150 |
Then the value should be matched as integer number with "<Expected Value>" |
| 151 |
|
| 152 |
Examples: |
| 153 |
| Value | Expected Value | |
| 154 |
| -1 | -1 | |
| 155 |
| 0 | 0 | |
| 156 |
| 1 | 1 | |
| 157 |
| 10 | 10 | |
| 158 |
| 12.345 | 12345 | |
| 159 |
| 12,543 | 12543 | |
| 160 |
| 12,345.678 | 12345678 | |
| 161 |
| 12.345,678 | 12345678 | |
| 162 |
|
| 163 |
|
| 164 |
Scenario Outline: Bad cases (not matched) |
| 165 |
Given a number param with "<Value>" |
| 166 |
Then the value should be <Outcome> as integer number |
| 167 |
|
| 168 |
Examples: |
| 169 |
| Value | Outcome | Reason | |
| 170 |
| 123.34 | not-matched | Separator in wrong position | |
| 171 |
| 1.23 | not-matched | Float number or separator in wrong position | |
| 172 |
| 1E+2 | not-matched | Float number | |
| 173 |
| Alice | not-matched | Not a number | |
| 174 |
""" |
| 175 |
When I run "behave -f plain features/example.int_param.with_type_n.feature" |
| 176 |
Then it should pass with: |
| 177 |
""" |
| 178 |
12 scenarios passed, 0 failed, 0 skipped |
| 179 |
""" |
| 180 |
|
| 181 |
Scenario: Use type "b" (Binary Number) for integer params |
| 182 |
Given a file named "features/example.int_param.with_type_b.feature" with: |
| 183 |
""" |
| 184 |
Feature: Use type "b" (Binary number) for integer params |
| 185 |
|
| 186 |
Scenario Outline: Good cases |
| 187 |
Given a binary number param with "<Value>" |
| 188 |
Then the value should be matched as integer number with "<Expected Value>" |
| 189 |
|
| 190 |
Examples: |
| 191 |
| Value | Expected Value | Case | |
| 192 |
| 0 | 0 | | |
| 193 |
| 1 | 1 | | |
| 194 |
| 10 | 2 | | |
| 195 |
| 11 | 3 | | |
| 196 |
| 100 | 4 | | |
| 197 |
| 101 | 5 | | |
| 198 |
| 0111 | 7 | With padded, leading zero | |
| 199 |
| 0b1001 | 9 | With binary number prefix "0b" | |
| 200 |
| 0b1011 | 11 | With binary number prefix "0b" | |
| 201 |
| 10000000 | 128 | Larger binary number | |
| 202 |
|
| 203 |
Scenario Outline: Bad cases (not matched) |
| 204 |
Given a binary number param with "<Value>" |
| 205 |
Then the value should be <Outcome> as integer number |
| 206 |
|
| 207 |
Examples: |
| 208 |
| Value | Outcome | Reason | |
| 209 |
| 21 | not-matched | Invalid binary number | |
| 210 |
| 0b21 | not-matched | Invalid binary number with binary number prefix | |
| 211 |
| 1.23 | not-matched | Float number | |
| 212 |
| 1E+2 | not-matched | Float number | |
| 213 |
| Alice | not-matched | Not a number | |
| 214 |
""" |
| 215 |
When I run "behave -f plain features/example.int_param.with_type_b.feature" |
| 216 |
Then it should pass with: |
| 217 |
""" |
| 218 |
15 scenarios passed, 0 failed, 0 skipped |
| 219 |
""" |
| 220 |
|
| 221 |
Scenario: Use type "o" (octal number) for integer params |
| 222 |
Given a file named "features/example.int_param.with_type_o.feature" with: |
| 223 |
""" |
| 224 |
Feature: Use type "o" (octal number) for integer params |
| 225 |
|
| 226 |
Scenario Outline: Good cases |
| 227 |
Given a octal number param with "<Value>" |
| 228 |
Then the value should be matched as integer number with "<Expected Value>" |
| 229 |
|
| 230 |
Examples: |
| 231 |
| Value | Expected Value | Case | |
| 232 |
| 0 | 0 | | |
| 233 |
| 12 | 10 | | |
| 234 |
| 21 | 17 | | |
| 235 |
| 65 | 53 | | |
| 236 |
| 123 | 83 | | |
| 237 |
| 0o1 | 1 | With leading octal prefix | |
| 238 |
| 0o12 | 10 | | |
| 239 |
| 0o21 | 17 | | |
| 240 |
|
| 241 |
|
| 242 |
Scenario Outline: Bad cases (not matched) |
| 243 |
Given a octal number param with "<Value>" |
| 244 |
Then the value should be <Outcome> as integer number |
| 245 |
|
| 246 |
Examples: |
| 247 |
| Value | Outcome | Reason | |
| 248 |
| 8 | not-matched | Invalid octal number | |
| 249 |
| 81 | not-matched | Invalid octal number | |
| 250 |
| 0o81 | not-matched | Invalid octal number with octal number prefix | |
| 251 |
| 1.23 | not-matched | Float number | |
| 252 |
| 1E+2 | not-matched | Float number | |
| 253 |
| Alice | not-matched | Not a number | |
| 254 |
""" |
| 255 |
When I run "behave -f plain features/example.int_param.with_type_o.feature" |
| 256 |
Then it should pass with: |
| 257 |
""" |
| 258 |
14 scenarios passed, 0 failed, 0 skipped |
| 259 |
""" |
| 260 |
|
| 261 |
Scenario: Use type "x" (hexadecimal number) for integer params |
| 262 |
Given a file named "features/example.int_param.with_type_x.feature" with: |
| 263 |
""" |
| 264 |
Feature: Use type "x" (hexadecimal number) for integer params |
| 265 |
|
| 266 |
Scenario Outline: Good cases: <Value> |
| 267 |
Given a hexadecimal number param with "<Value>" |
| 268 |
Then the value should be matched as integer number with "<Expected Value>" |
| 269 |
|
| 270 |
Examples: |
| 271 |
| Value | Expected Value | Case | |
| 272 |
| 0 | 0 | | |
| 273 |
| 12 | 18 | | |
| 274 |
| 21 | 33 | | |
| 275 |
| 65 | 101 | | |
| 276 |
| 123 | 291 | | |
| 277 |
| beef | 48879 | With hexadecimal letters (lowercase) | |
| 278 |
| BEEF | 48879 | With hexadecimal letters (uppercase) | |
| 279 |
| deadbeef | 3735928559 | Larger hex number | |
| 280 |
| DeadBeef | 3735928559 | Larger hex number (mixed case) | |
| 281 |
| 0x01 | 1 | With hexadecimal prefix | |
| 282 |
| 0x12 | 18 | | |
| 283 |
| 0x21 | 33 | | |
| 284 |
| 0xbeef | 48879 | | |
| 285 |
| 0xBeEF | 48879 | | |
| 286 |
|
| 287 |
|
| 288 |
Scenario Outline: Bad cases (not matched) |
| 289 |
Given a hexadecimal number param with "<Value>" |
| 290 |
Then the value should be <Outcome> as integer number |
| 291 |
|
| 292 |
Examples: |
| 293 |
| Value | Outcome | Reason | |
| 294 |
| G | not-matched | Invalid hex digit | |
| 295 |
| G1 | not-matched | Invalid hex number | |
| 296 |
| 0x1G | not-matched | Invalid hex number with hex number prefix | |
| 297 |
| 1.23 | not-matched | Float number | |
| 298 |
| 1E+2 | not-matched | Float number | |
| 299 |
| Alice | not-matched | Not a number | |
| 300 |
""" |
| 301 |
When I run "behave -f plain features/example.int_param.with_type_x.feature" |
| 302 |
Then it should pass with: |
| 303 |
""" |
| 304 |
20 scenarios passed, 0 failed, 0 skipped |
| 305 |
""" |