| 1 |
Integers (Int): |
| 2 |
docs: scalar/integer |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
StrictYAML parses to a YAML object, not |
| 6 |
the value directly to give you more flexibility |
| 7 |
and control over what you can do with the YAML. |
| 8 |
|
| 9 |
This is what that can object can do - in many |
| 10 |
cases if parsed as a integer, it will behave in |
| 11 |
the same way. |
| 12 |
given: |
| 13 |
yaml_snippet: | |
| 14 |
a: 1 |
| 15 |
b: 5 |
| 16 |
setup: | |
| 17 |
from strictyaml import Map, Int, load |
| 18 |
from ensure import Ensure |
| 19 |
|
| 20 |
schema = Map({"a": Int(), "b": Int()}) |
| 21 |
|
| 22 |
parsed = load(yaml_snippet, schema) |
| 23 |
variations: |
| 24 |
Parsed correctly: |
| 25 |
steps: |
| 26 |
- Run: | |
| 27 |
Ensure(parsed).equals({"a": 1, "b": 5}) |
| 28 |
|
| 29 |
Has underscores: |
| 30 |
given: |
| 31 |
yaml_snippet: | |
| 32 |
a: 10_000_000 |
| 33 |
b: 10_0_0 |
| 34 |
steps: |
| 35 |
- Run: |
| 36 |
code: | |
| 37 |
Ensure(load(yaml_snippet, schema).data).equals({"a": 10000000, "b": 1000}) |
| 38 |
|
| 39 |
Cast with str: |
| 40 |
steps: |
| 41 |
- Run: Ensure(str(parsed["a"])).equals("1") |
| 42 |
|
| 43 |
Cast with float: |
| 44 |
steps: |
| 45 |
- Run: Ensure(float(parsed["a"])).equals(1.0) |
| 46 |
|
| 47 |
Greater than: |
| 48 |
steps: |
| 49 |
- Run: Ensure(parsed["a"] > 0).equals(True) |
| 50 |
|
| 51 |
Less than: |
| 52 |
steps: |
| 53 |
- Run: Ensure(parsed["a"] < 2).equals(True) |
| 54 |
|
| 55 |
To get actual int, use .data: |
| 56 |
steps: |
| 57 |
- Run: Ensure(type(load(yaml_snippet, schema)["a"].data) is int).equals(True) |
| 58 |
|
| 59 |
Cannot cast to bool: |
| 60 |
steps: |
| 61 |
- Run: |
| 62 |
code: bool(load(yaml_snippet, schema)['a']) |
| 63 |
raises: |
| 64 |
message: |- |
| 65 |
Cannot cast 'YAML(1)' to bool. |
| 66 |
Use bool(yamlobj.data) or bool(yamlobj.text) instead. |
| 67 |
|
| 68 |
|
| 69 |
Invalid scalar integer: |
| 70 |
based on: strictyaml |
| 71 |
given: |
| 72 |
yaml_snippet: | |
| 73 |
a: string |
| 74 |
b: 2 |
| 75 |
setup: | |
| 76 |
from strictyaml import Map, Int, load |
| 77 |
|
| 78 |
schema = Map({"a": Int(), "b": Int()}) |
| 79 |
steps: |
| 80 |
- Run: |
| 81 |
code: load(yaml_snippet, schema) |
| 82 |
raises: |
| 83 |
type: strictyaml.exceptions.YAMLValidationError |
| 84 |
message: |- |
| 85 |
when expecting an integer |
| 86 |
found arbitrary text |
| 87 |
in "<unicode string>", line 1, column 1: |
| 88 |
a: string |
| 89 |
^ (line: 1) |