Debian Codegraph @

hitch/story/fixed-sequence.story

raw
1 Fixed length sequences (FixedSeq):
2 docs: compound/fixed-length-sequences
3 based on: strictyaml
4 description: |
5 Sequences of fixed length can be validated with a series
6 of different (or the same) types.
7 given:
8 setup: |
9 from strictyaml import FixedSeq, Str, Map, Int, Float, YAMLValidationError, load
10 from ensure import Ensure
11
12 schema = FixedSeq([Int(), Map({"x": Str()}), Float()])
13 variations:
14 Equivalent list:
15 given:
16 yaml_snippet: |
17 - 1
18 - x: 5
19 - 2.5
20 steps:
21 - Run:
22 code: |
23 Ensure(load(yaml_snippet, schema)).equals([1, {"x": "5"}, 2.5, ])
24
25 Invalid list 1:
26 given:
27 yaml_snippet: |
28 a: 1
29 b: 2
30 c: 3
31 steps:
32 - Run:
33 code: load(yaml_snippet, schema)
34 raises:
35 type: strictyaml.exceptions.YAMLValidationError
36 message: |-
37 when expecting a sequence of 3 elements
38 in "<unicode string>", line 1, column 1:
39 a: '1'
40 ^ (line: 1)
41 found a mapping
42 in "<unicode string>", line 3, column 1:
43 c: '3'
44 ^ (line: 3)
45
46 Invalid list 2:
47 given:
48 yaml_snippet: |
49 - 2
50 - a
51 - a:
52 - 1
53 - 2
54 steps:
55 - Run:
56 code: load(yaml_snippet, schema)
57 raises:
58 type: strictyaml.exceptions.YAMLValidationError
59 message: |-
60 when expecting a mapping
61 found arbitrary text
62 in "<unicode string>", line 2, column 1:
63 - a
64 ^ (line: 2)
65
66 Invalid list 3:
67 given:
68 yaml_snippet: |
69 - 1
70 - a
71 steps:
72 - Run:
73 code: load(yaml_snippet, schema)
74 raises:
75 type: strictyaml.exceptions.YAMLValidationError
76 message: |-
77 when expecting a sequence of 3 elements
78 in "<unicode string>", line 1, column 1:
79 - '1'
80 ^ (line: 1)
81 found a sequence of 2 elements
82 in "<unicode string>", line 2, column 1:
83 - a
84 ^ (line: 2)