File: yamlpath_test.go

package info (click to toggle)
golang-github-caspr-io-yamlpath 0.0~git20200722.502e8d1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116 kB
  • sloc: makefile: 11
file content (84 lines) | stat: -rw-r--r-- 2,572 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package yamlpath

import (
	"testing"

	"gopkg.in/yaml.v3"
	"gotest.tools/v3/assert"
)

//nolint:gochecknoglobals
var yamlDoc = `---
hash:
  child_attr:
    key: 5280
  dotted.child:
    key: 42
aliases:
  - &first_anchor Simple string value
  - Complex ending
users:
  - name: User One
    password: foobar
    roles:
      - Writers
  - name: User Two
    password: barfoo
    roles:
      - Power Users
      - Editors
:f:oo::
  bar: baz
`

var tests = []struct { //nolint:gochecknoglobals
	testName string
	path     string
	output   interface{}
}{
	{"DotNotation", "hash.child_attr.key", 5280},
	{"DotNotation", ":f:oo:.bar", "baz"},
	{"SlashNotation", "/hash/child_attr/key", 5280},
	{"EscapedDotNotation", "hash.dotted\\.child.key", 42},
	{"QuotedDotNotation", "hash.\"dotted.child\".key", 42},
	{"SingleQuotedDotNotation", "hash.'dotted.child'.key", 42},
	{"SlashDotted", "/hash/dotted.child/key", 42},
	{"SearchChildKeyDot", "hash.child_attr[.=key]", 5280},
	{"SearchChildKeySlash", "/hash/child_attr[.=key]", 5280},
	{"ExplicitIndex", "aliases[0]", "Simple string value"},
	{"ImplicitIndex", "aliases.0", "Simple string value"},
	{"ArraySlice", "aliases[0:2]", []interface{}{"Simple string value", "Complex ending"}},
	{"ValuePrefix", "aliases[.^Simple]", "Simple string value"},
	{"ValueContains", "aliases[.%string]", "Simple string value"},
	{"ValueSuffix", "aliases[.$value]", "Simple string value"},
	// TODO implement regex support
	// {"ValueRegex", "aliases[.=~/^(\\b[Ss][a-z]+\\s){2}[a-z]+$/]", "Simple string value"},
	{"SlashExplicitIndex", "/aliases[0]", "Simple string value"},
	{"SlashImplicitIndex", "/aliases/0", "Simple string value"},
	{"GetArrayOfHashes", "/users/name", []interface{}{"User One", "User Two"}},
	{"IndexIntoArrayOfHashes", "/users[1]/name", "User Two"},
	// Unsupported as anchors are erased in parsed yaml
	// {"AnchoredIndex", "aliases[&first_anchor]", "Simple string value"},
	// {"SlashAnchoredIndex", "/aliases[&first_anchor]", "Simple string value"},
	{"ErrorOnNonExistingKey", "/broken", nil},
	{"ErrorOnNonExistingArrayIndex", "aliases[4]", nil},
}

func TestYamlPath(t *testing.T) {
	for _, tst := range tests {
		path := tst.path
		expected := tst.output
		t.Run(tst.testName, func(t *testing.T) {
			yamlBytes := []byte(yamlDoc)
			yamlMap := map[string]interface{}{}
			assert.NilError(t, yaml.Unmarshal(yamlBytes, &yamlMap))
			out, err := YamlPath(yamlMap, path)
			if expected != nil {
				assert.NilError(t, err)
				assert.DeepEqual(t, out, expected)
			} else {
				assert.Assert(t, err != nil)
			}
		})
	}
}