v / vlib / yaml / test_helpers.v
19 lines · 17 sloc · 817 bytes · d96fe54c0a96fd6d651e9cb3c61b137ccc5c7dbe
Raw
1module yaml
2
3import x.json2
4
5// json_logically_eq compares two JSON strings by decoding and re-encoding
6// both sides via json2, normalizing whitespace and treating an empty input
7// as the literal "null" document.
8//
9// For tests only. Lives in a non-_test.v file because V compiles each
10// `_test.v` file as its own binary, so a helper cannot otherwise be shared
11// across the module's test files. Not exported (lowercase name) so it stays
12// invisible to consumers of `yaml`.
13fn json_logically_eq(a string, b string) !bool {
14 a_norm := if a.trim_space() == '' { 'null' } else { a }
15 b_norm := if b.trim_space() == '' { 'null' } else { b }
16 pa := json2.decode[json2.Any](a_norm)!
17 pb := json2.decode[json2.Any](b_norm)!
18 return json2.encode(pa, json2.EncoderOptions{}) == json2.encode(pb, json2.EncoderOptions{})
19}
20