Debian Codegraph @

hitch/story/mappattern.story

raw
1 Mappings with arbitrary key names (MapPattern):
2 docs: compound/map-pattern
3 based on: strictyaml
4 description: |
5 When you do not wish to let the user define the key
6 names in a mapping and and only specify what type the
7 keys are, use a MapPattern.
8
9 When you wish to specify the exact key name, use the
10 'Map' validator instead.
11 given:
12 setup: |
13 from strictyaml import MapPattern, Int, Float, Str, Any, Seq, YAMLValidationError, load
14 from ensure import Ensure
15
16 schema = MapPattern(Str(), Int())
17
18 variations:
19 Equivalence 1:
20 given:
21 yaml_snippet: |
22 â: 1
23 b: 2
24 steps:
25 - Run:
26 code: |
27 Ensure(load(yaml_snippet, schema)).equals({u"â": 1, "b": 2})
28
29 Equivalence 2:
30 given:
31 yaml_snippet: |
32 a: 1
33 c: 3
34 steps:
35 - Run:
36 code: |
37 Ensure(load(yaml_snippet, schema)).equals({"a": 1, "c": 3})
38
39 Equivalence 3:
40 given:
41 yaml_snippet: |
42 a: 1
43 steps:
44 - Run:
45 code: |
46 Ensure(load(yaml_snippet, schema)).equals({"a": 1, })
47
48 With floats and ints:
49 given:
50 yaml_snippet: |
51 10.25: 23
52 20.33: 76
53 steps:
54 - Run:
55 code: |
56 Ensure(load(yaml_snippet, MapPattern(Float(), Int())).data).equals({10.25: 23, 20.33: 76})
57
58 With Int and List:
59 given:
60 yaml_snippet: |
61 1:
62 - ABC
63 2:
64 - DEF
65 steps:
66 - Run:
67 code: |
68 Ensure(load(yaml_snippet, MapPattern(Int(), Seq(Str()))).data).equals({1: ["ABC"], 2: ["DEF"]})
69
70
71 Invalid 1:
72 given:
73 yaml_snippet: |
74 b: b
75 steps:
76 - Run:
77 code: load(yaml_snippet, schema)
78 raises:
79 type: strictyaml.exceptions.YAMLValidationError
80 message: |-
81 when expecting an integer
82 found arbitrary text
83 in "<unicode string>", line 1, column 1:
84 b: b
85 ^ (line: 1)
86 Invalid 2:
87 given:
88 yaml_snippet: |
89 a: a
90 b: 2
91 steps:
92 - Run:
93 code: load(yaml_snippet, schema)
94 raises:
95 type: strictyaml.exceptions.YAMLValidationError
96 message: |-
97 when expecting an integer
98 found arbitrary text
99 in "<unicode string>", line 1, column 1:
100 a: a
101 ^ (line: 1)
102
103 More than the maximum number of keys:
104 given:
105 yaml_snippet: |
106 â: 1
107 b: 2
108 steps:
109 - Run:
110 code: load(yaml_snippet, MapPattern(Str(), Int(), maximum_keys=1))
111 raises:
112 type: strictyaml.exceptions.YAMLValidationError
113 message: |-
114 while parsing a mapping
115 in "<unicode string>", line 1, column 1:
116 "\xE2": '1'
117 ^ (line: 1)
118 expected a maximum of 1 key, found 2.
119 in "<unicode string>", line 2, column 1:
120 b: '2'
121 ^ (line: 2)
122
123 Fewer than the minimum number of keys:
124 given:
125 yaml_snippet: |
126 â: 1
127 steps:
128 - Run:
129 code: load(yaml_snippet, MapPattern(Str(), Int(), minimum_keys=2))
130 raises:
131 type: strictyaml.exceptions.YAMLValidationError
132 message: |-
133 while parsing a mapping
134 expected a minimum of 2 keys, found 1.
135 in "<unicode string>", line 1, column 1:
136 "\xE2": '1'
137 ^ (line: 1)
138
139 Invalid with non-ascii:
140 given:
141 yaml_snippet: |
142 a: 1
143 b: yâs
144 c: 3
145 steps:
146 - Run:
147 code: load(yaml_snippet, schema)
148 raises:
149 type: strictyaml.exceptions.YAMLValidationError
150 message: |-
151 when expecting an integer
152 found arbitrary text
153 in "<unicode string>", line 2, column 1:
154 b: "y\xE2s"
155 ^ (line: 2)