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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
@sequential
Feature: Parse integer data types in step parameters (type transformation)
As a test writer
I want write steps with parameter that should have integer data types
So that I can just use the step parameter with the correct type
Behave uses the parse module (inverse of Python string.format).
Therefore, the following ``parse types`` for integer numbers are already supported:
===== =========================================== ============= ================================
Type Characters Matched (regex class) Output Type Example(s)
===== =========================================== ============= ================================
d Digits (effectively integer numbers) int 12345 0b101 0o761 0x1ABE
n Numbers with thousands separators (, or .) int 12,345
b Binary numbers int 10111 0b1011
o Octal numbers int 07654 0o123
x Hexadecimal numbers (lower and upper case) int DEADBEAF 0xBEEF
===== =========================================== ============= ================================
SEE ALSO:
* http://pypi.python.org/pypi/parse
* string.format: http://docs.python.org/library/string.html#format-string-syntax
@setup
Scenario: Feature Setup
Given a new working directory
And a file named "features/steps/integer_param_steps.py" with:
"""
from behave import then, step
from six import integer_types
class NotMatched(object): pass
@step('a integer param with "{value:d}"')
def step_integer_param_with(context, value):
assert isinstance(value, integer_types), "value.type=%s" % type(value)
context.value = value
@step('a integer param with "{value}"')
def step_integer_param_otherwise(context, value):
context.value = NotMatched
@step('a number param with "{value:n}"')
def step_number_param_with(context, value):
step_integer_param_with(context, value)
@step('a number param with "{value}"')
def step_number_param_otherwise(context, value):
step_integer_param_otherwise(context, value)
@step('a binary number param with "{value:b}"')
def step_binary_param_with(context, value):
step_integer_param_with(context, value)
@step('a binary number param with "{value}"')
def step_binary_param_otherwise(context, value):
step_integer_param_otherwise(context, value)
@step('a octal number param with "{value:o}"')
def step_hexadecimal_param_with(context, value):
step_integer_param_with(context, value)
@step('a octal number param with "{value}"')
def step_hexadecimal_param_otherwise(context, value):
step_integer_param_otherwise(context, value)
@step('a hexadecimal number param with "{value:x}"')
def step_hexadecimal_param_with(context, value):
step_integer_param_with(context, value)
@step('a hexadecimal number param with "{value}"')
def step_hexadecimal_param_otherwise(context, value):
step_integer_param_otherwise(context, value)
@then('the value should be {outcome} as integer number')
def step_value_should_be_matched_as_number(context, outcome):
if outcome == "matched":
assert isinstance(context.value, integer_types), \
"Unexpected type: %s" % type(context.value)
else:
assert context.value is NotMatched
@then('the value should be {outcome} as integer number with "{expected:d}"')
def step_value_should_be_matched_as_number_with_expected(context, outcome, expected):
step_value_should_be_matched_as_number(context, outcome)
assert context.value == expected, \
"FAILED: value(%s) == expected(%s)" % (context.value, expected)
"""
Scenario: Use type "d" (Digits) for integer params
Given a file named "features/example.int_param.with_type_d.feature" with:
"""
Feature: Use type "d" (Digits) for integer params
Scenario Outline: Good cases
Given a integer param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| -1 | -1 | Negative number |
| +1 | 1 | With plus sign |
| 0 | 0 | |
| 1 | 1 | |
| 10 | 10 | |
| 42 | 42 | |
Scenario Outline: Bad cases (not matched)
Given a integer param with "<Value>"
Then the value should be <Outcome> as integer number
Examples:
| Value | Outcome | Reason |
| 1.23 | not-matched | Float number |
| 1E+2 | not-matched | Float number |
| Alice | not-matched | Not a number |
Scenario Outline: Conversion from other number system (base=2, 8, 16)
Given a integer param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0b1011 | 11 | Binary number |
| 0o123 | 83 | Octal number |
| 0xBEEF | 48879 | Hexadecimal number |
"""
When I run "behave -f plain features/example.int_param.with_type_d.feature"
Then it should pass with:
"""
12 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Use type "n" (Number) for integer params
This data type supports numbers with thousands separators (',' or '.').
Given a file named "features/example.int_param.with_type_n.feature" with:
"""
Feature: Use type "n" (Number) for integer params
Scenario Outline: Good cases
Given a number param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value |
| -1 | -1 |
| 0 | 0 |
| 1 | 1 |
| 10 | 10 |
| 12.345 | 12345 |
| 12,543 | 12543 |
| 12,345.678 | 12345678 |
| 12.345,678 | 12345678 |
Scenario Outline: Bad cases (not matched)
Given a number param with "<Value>"
Then the value should be <Outcome> as integer number
Examples:
| Value | Outcome | Reason |
| 123.34 | not-matched | Separator in wrong position |
| 1.23 | not-matched | Float number or separator in wrong position |
| 1E+2 | not-matched | Float number |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.int_param.with_type_n.feature"
Then it should pass with:
"""
12 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Use type "b" (Binary Number) for integer params
Given a file named "features/example.int_param.with_type_b.feature" with:
"""
Feature: Use type "b" (Binary number) for integer params
Scenario Outline: Good cases
Given a binary number param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0 | 0 | |
| 1 | 1 | |
| 10 | 2 | |
| 11 | 3 | |
| 100 | 4 | |
| 101 | 5 | |
| 0111 | 7 | With padded, leading zero |
| 0b1001 | 9 | With binary number prefix "0b" |
| 0b1011 | 11 | With binary number prefix "0b" |
| 10000000 | 128 | Larger binary number |
Scenario Outline: Bad cases (not matched)
Given a binary number param with "<Value>"
Then the value should be <Outcome> as integer number
Examples:
| Value | Outcome | Reason |
| 21 | not-matched | Invalid binary number |
| 0b21 | not-matched | Invalid binary number with binary number prefix |
| 1.23 | not-matched | Float number |
| 1E+2 | not-matched | Float number |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.int_param.with_type_b.feature"
Then it should pass with:
"""
15 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Use type "o" (octal number) for integer params
Given a file named "features/example.int_param.with_type_o.feature" with:
"""
Feature: Use type "o" (octal number) for integer params
Scenario Outline: Good cases
Given a octal number param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0 | 0 | |
| 12 | 10 | |
| 21 | 17 | |
| 65 | 53 | |
| 123 | 83 | |
| 0o1 | 1 | With leading octal prefix |
| 0o12 | 10 | |
| 0o21 | 17 | |
Scenario Outline: Bad cases (not matched)
Given a octal number param with "<Value>"
Then the value should be <Outcome> as integer number
Examples:
| Value | Outcome | Reason |
| 8 | not-matched | Invalid octal number |
| 81 | not-matched | Invalid octal number |
| 0o81 | not-matched | Invalid octal number with octal number prefix |
| 1.23 | not-matched | Float number |
| 1E+2 | not-matched | Float number |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.int_param.with_type_o.feature"
Then it should pass with:
"""
14 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Use type "x" (hexadecimal number) for integer params
Given a file named "features/example.int_param.with_type_x.feature" with:
"""
Feature: Use type "x" (hexadecimal number) for integer params
Scenario Outline: Good cases: <Value>
Given a hexadecimal number param with "<Value>"
Then the value should be matched as integer number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0 | 0 | |
| 12 | 18 | |
| 21 | 33 | |
| 65 | 101 | |
| 123 | 291 | |
| beef | 48879 | With hexadecimal letters (lowercase) |
| BEEF | 48879 | With hexadecimal letters (uppercase) |
| deadbeef | 3735928559 | Larger hex number |
| DeadBeef | 3735928559 | Larger hex number (mixed case) |
| 0x01 | 1 | With hexadecimal prefix |
| 0x12 | 18 | |
| 0x21 | 33 | |
| 0xbeef | 48879 | |
| 0xBeEF | 48879 | |
Scenario Outline: Bad cases (not matched)
Given a hexadecimal number param with "<Value>"
Then the value should be <Outcome> as integer number
Examples:
| Value | Outcome | Reason |
| G | not-matched | Invalid hex digit |
| G1 | not-matched | Invalid hex number |
| 0x1G | not-matched | Invalid hex number with hex number prefix |
| 1.23 | not-matched | Float number |
| 1E+2 | not-matched | Float number |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.int_param.with_type_x.feature"
Then it should pass with:
"""
20 scenarios passed, 0 failed, 0 skipped
"""
|