Debian Codegraph @

hitch/story/sequence.story

raw
1 Sequence/list validator (Seq):
2 docs: compound/sequences
3 based on: strictyaml
4 description: |
5 Sequences in YAML are denoted by a series of dashes ('-')
6 and parsed as a list in python.
7
8 Validating sequences of a particular type can be done with
9 the Seq validator, specifying the type.
10
11 See also [UniqueSeq](../sequences-of-unique-items) and
12 [FixedSeq](../fixed-length-sequences) for other types of sequence
13 validation.
14 given:
15 yaml_snippet: |
16 - 1
17 - 2
18 - 3
19 setup: |
20 from strictyaml import Seq, Str, Int, load
21 from ensure import Ensure
22 variations:
23 Valid Parsed:
24 steps:
25 - Run: |
26 Ensure(load(yaml_snippet, Seq(Str()))).equals(["1", "2", "3", ])
27
28 Is sequence:
29 steps:
30 - Run: |
31 assert load(yaml_snippet, Seq(Str())).is_sequence()
32
33 Iterator:
34 steps:
35 - Run: |
36 assert [x for x in load(yaml_snippet, Seq(Str()))] == ["1", "2", "3"]
37
38 Lists of lists:
39 given:
40 yaml_snippet: |
41 -
42 - a
43 - b
44 - c
45 -
46 - d
47 - e
48 - f
49 steps:
50 - Run: |
51 assert load(yaml_snippet, Seq(Seq(Str()))) == [["a", "b", "c"], ["d", "e", "f"]]
52
53 .text is nonsensical:
54 given:
55 yaml_snippet: |
56 - â
57 - 2
58 - 3
59 steps:
60 - Run:
61 code: load(yaml_snippet, Seq(Str())).text
62 raises:
63 type:
64 in python 2: exceptions.TypeError
65 in python 3: builtins.TypeError
66 message:
67 in python 2: YAML([u'\xe2', '2', '3']) is a sequence, has no text value.
68 in python 3: YAML(['â', '2', '3']) is a sequence, has no text value.
69
70 Invalid mapping instead:
71 given:
72 yaml_snippet: |
73 a: 1
74 b: 2
75 c: 3
76 steps:
77 - Run:
78 code: load(yaml_snippet, Seq(Str()))
79 raises:
80 type: strictyaml.exceptions.YAMLValidationError
81 message: |-
82 when expecting a sequence
83 in "<unicode string>", line 1, column 1:
84 a: '1'
85 ^ (line: 1)
86 found a mapping
87 in "<unicode string>", line 3, column 1:
88 c: '3'
89 ^ (line: 3)
90
91 Invalid nested structure instead:
92 given:
93 yaml_snippet: |
94 - 2
95 - 3
96 - a:
97 - 1
98 - 2
99 steps:
100 - Run:
101 code: load(yaml_snippet, Seq(Str()))
102 raises:
103 type: strictyaml.exceptions.YAMLValidationError
104 message: |-
105 when expecting a str
106 in "<unicode string>", line 3, column 1:
107 - a:
108 ^ (line: 3)
109 found a mapping
110 in "<unicode string>", line 5, column 1:
111 - '2'
112 ^ (line: 5)
113 Invalid item in sequence:
114 given:
115 yaml_snippet: |
116 - 1.1
117 - 1.2
118 steps:
119 - Run:
120 code: load(yaml_snippet, Seq(Int()))
121 raises:
122 type: strictyaml.exceptions.YAMLValidationError
123 message: |-
124 when expecting an integer
125 found an arbitrary number
126 in "<unicode string>", line 1, column 1:
127 - '1.1'
128 ^ (line: 1)
129 One invalid item in sequence:
130 given:
131 yaml_snippet: |
132 - 1
133 - 2
134 - 3.4
135 steps:
136 - Run:
137 code: load(yaml_snippet, Seq(Int()))
138 raises:
139 type: strictyaml.exceptions.YAMLValidationError
140 message: "when expecting an integer\nfound an arbitrary number\n in \"\
141 <unicode string>\", line 3, column 1:\n - '3.4'\n ^ (line: 3)"
142
143 Modify nested sequence:
144 based on: strictyaml
145 given:
146 yaml_snippet: |
147 a:
148 - a
149 - b
150 b: 2
151 c: 3
152 setup: |
153 from strictyaml import Map, Int, load, Seq, Str
154 from collections import OrderedDict
155
156 schema = Map({"a": Seq(Str()), "b": Int(), "c": Int()})
157
158 variations:
159 successfully:
160 steps:
161 - Run:
162 code: |
163 yaml = load(yaml_snippet, schema)
164
165 yaml['a'] = ['b', 'c', 'd']
166 yaml['a'][1] = 'x'
167 print(yaml.as_yaml())
168 will output: |-
169 a:
170 - b
171 - x
172 - d
173 b: 2
174 c: 3
175
176 with non sequence where sequence expected:
177 steps:
178 - Run:
179 code: |
180 yaml = load(yaml_snippet, schema)
181
182 yaml['a'] = {'a': '1'}
183 raises:
184 type: strictyaml.exceptions.YAMLSerializationError
185 message: "Expected a list, found '{'a': '1'}'"
186
187 with empty list:
188 steps:
189 - Run:
190 code: |
191 yaml = load(yaml_snippet, schema)
192
193 yaml['a'] = []
194 raises:
195 type: strictyaml.exceptions.YAMLSerializationError
196 message: "Expected a non-empty list, found an empty list.\nUse EmptyList\
197 \ validator to serialize empty lists."