OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / pelletier / go-toml / query / query_test.go
1 package query
2
3 import (
4         "fmt"
5         "testing"
6
7         "github.com/pelletier/go-toml"
8 )
9
10 func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) {
11         if len(array) != len(objects) {
12                 t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
13         }
14
15         for _, o := range objects {
16                 found := false
17                 for _, a := range array {
18                         if a == o {
19                                 found = true
20                                 break
21                         }
22                 }
23                 if !found {
24                         t.Fatal(o, "not found in array", array)
25                 }
26         }
27 }
28
29 func TestQueryExample(t *testing.T) {
30         config, _ := toml.Load(`
31       [[book]]
32       title = "The Stand"
33       author = "Stephen King"
34       [[book]]
35       title = "For Whom the Bell Tolls"
36       author = "Ernest Hemmingway"
37       [[book]]
38       title = "Neuromancer"
39       author = "William Gibson"
40     `)
41         authors, err := CompileAndExecute("$.book.author", config)
42         if err != nil {
43                 t.Fatal("unexpected error:", err)
44         }
45         names := authors.Values()
46         if len(names) != 3 {
47                 t.Fatalf("query should return 3 names but returned %d", len(names))
48         }
49         assertArrayContainsInAnyOrder(t, names, "Stephen King", "Ernest Hemmingway", "William Gibson")
50 }
51
52 func TestQueryReadmeExample(t *testing.T) {
53         config, _ := toml.Load(`
54 [postgres]
55 user = "pelletier"
56 password = "mypassword"
57 `)
58
59         query, err := Compile("$..[user,password]")
60         if err != nil {
61                 t.Fatal("unexpected error:", err)
62         }
63         results := query.Execute(config)
64         values := results.Values()
65         if len(values) != 2 {
66                 t.Fatalf("query should return 2 values but returned %d", len(values))
67         }
68         assertArrayContainsInAnyOrder(t, values, "pelletier", "mypassword")
69 }
70
71 func TestQueryPathNotPresent(t *testing.T) {
72         config, _ := toml.Load(`a = "hello"`)
73         query, err := Compile("$.foo.bar")
74         if err != nil {
75                 t.Fatal("unexpected error:", err)
76         }
77         results := query.Execute(config)
78         if err != nil {
79                 t.Fatalf("err should be nil. got %s instead", err)
80         }
81         if len(results.items) != 0 {
82                 t.Fatalf("no items should be matched. %d matched instead", len(results.items))
83         }
84 }
85
86 func ExampleNodeFilterFn_filterExample() {
87         tree, _ := toml.Load(`
88       [struct_one]
89       foo = "foo"
90       bar = "bar"
91
92       [struct_two]
93       baz = "baz"
94       gorf = "gorf"
95     `)
96
97         // create a query that references a user-defined-filter
98         query, _ := Compile("$[?(bazOnly)]")
99
100         // define the filter, and assign it to the query
101         query.SetFilter("bazOnly", func(node interface{}) bool {
102                 if tree, ok := node.(*toml.Tree); ok {
103                         return tree.Has("baz")
104                 }
105                 return false // reject all other node types
106         })
107
108         // results contain only the 'struct_two' Tree
109         query.Execute(tree)
110 }
111
112 func ExampleQuery_queryExample() {
113         config, _ := toml.Load(`
114       [[book]]
115       title = "The Stand"
116       author = "Stephen King"
117       [[book]]
118       title = "For Whom the Bell Tolls"
119       author = "Ernest Hemmingway"
120       [[book]]
121       title = "Neuromancer"
122       author = "William Gibson"
123     `)
124
125         // find and print all the authors in the document
126         query, _ := Compile("$.book.author")
127         authors := query.Execute(config)
128         for _, name := range authors.Values() {
129                 fmt.Println(name)
130         }
131 }
132
133 func TestTomlQuery(t *testing.T) {
134         tree, err := toml.Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
135         if err != nil {
136                 t.Error(err)
137                 return
138         }
139         query, err := Compile("$.foo.bar")
140         if err != nil {
141                 t.Error(err)
142                 return
143         }
144         result := query.Execute(tree)
145         values := result.Values()
146         if len(values) != 1 {
147                 t.Errorf("Expected resultset of 1, got %d instead: %v", len(values), values)
148         }
149
150         if tt, ok := values[0].(*toml.Tree); !ok {
151                 t.Errorf("Expected type of Tree: %T", values[0])
152         } else if tt.Get("a") != int64(1) {
153                 t.Errorf("Expected 'a' with a value 1: %v", tt.Get("a"))
154         } else if tt.Get("b") != int64(2) {
155                 t.Errorf("Expected 'b' with a value 2: %v", tt.Get("b"))
156         }
157 }