summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/query_test.go
blob: 0d9f3832b94ae860940a2a86498fd36ae682c6e9 (plain)
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
package toml

import (
	"testing"
)

func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) {
	if len(array) != len(objects) {
		t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
	}

	for _, o := range objects {
		found := false
		for _, a := range array {
			if a == o {
				found = true
				break
			}
		}
		if !found {
			t.Fatal(o, "not found in array", array)
		}
	}
}

func TestQueryExample(t *testing.T) {
	config, _ := Load(`
      [[book]]
      title = "The Stand"
      author = "Stephen King"
      [[book]]
      title = "For Whom the Bell Tolls"
      author = "Ernest Hemmingway"
      [[book]]
      title = "Neuromancer"
      author = "William Gibson"
    `)

	authors, _ := config.Query("$.book.author")
	names := authors.Values()
	if len(names) != 3 {
		t.Fatalf("query should return 3 names but returned %d", len(names))
	}
	assertArrayContainsInAnyOrder(t, names, "Stephen King", "Ernest Hemmingway", "William Gibson")
}

func TestQueryReadmeExample(t *testing.T) {
	config, _ := Load(`
[postgres]
user = "pelletier"
password = "mypassword"
`)
	results, _ := config.Query("$..[user,password]")
	values := results.Values()
	if len(values) != 2 {
		t.Fatalf("query should return 2 values but returned %d", len(values))
	}
	assertArrayContainsInAnyOrder(t, values, "pelletier", "mypassword")
}

func TestQueryPathNotPresent(t *testing.T) {
	config, _ := Load(`a = "hello"`)
	results, err := config.Query("$.foo.bar")
	if err != nil {
		t.Fatalf("err should be nil. got %s instead", err)
	}
	if len(results.items) != 0 {
		t.Fatalf("no items should be matched. %d matched instead", len(results.items))
	}
}