7 "github.com/pelletier/go-toml"
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))
15 for _, o := range objects {
17 for _, a := range array {
24 t.Fatal(o, "not found in array", array)
29 func TestQueryExample(t *testing.T) {
30 config, _ := toml.Load(`
33 author = "Stephen King"
35 title = "For Whom the Bell Tolls"
36 author = "Ernest Hemmingway"
39 author = "William Gibson"
41 authors, err := CompileAndExecute("$.book.author", config)
43 t.Fatal("unexpected error:", err)
45 names := authors.Values()
47 t.Fatalf("query should return 3 names but returned %d", len(names))
49 assertArrayContainsInAnyOrder(t, names, "Stephen King", "Ernest Hemmingway", "William Gibson")
52 func TestQueryReadmeExample(t *testing.T) {
53 config, _ := toml.Load(`
56 password = "mypassword"
59 query, err := Compile("$..[user,password]")
61 t.Fatal("unexpected error:", err)
63 results := query.Execute(config)
64 values := results.Values()
66 t.Fatalf("query should return 2 values but returned %d", len(values))
68 assertArrayContainsInAnyOrder(t, values, "pelletier", "mypassword")
71 func TestQueryPathNotPresent(t *testing.T) {
72 config, _ := toml.Load(`a = "hello"`)
73 query, err := Compile("$.foo.bar")
75 t.Fatal("unexpected error:", err)
77 results := query.Execute(config)
79 t.Fatalf("err should be nil. got %s instead", err)
81 if len(results.items) != 0 {
82 t.Fatalf("no items should be matched. %d matched instead", len(results.items))
86 func ExampleNodeFilterFn_filterExample() {
87 tree, _ := toml.Load(`
97 // create a query that references a user-defined-filter
98 query, _ := Compile("$[?(bazOnly)]")
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")
105 return false // reject all other node types
108 // results contain only the 'struct_two' Tree
112 func ExampleQuery_queryExample() {
113 config, _ := toml.Load(`
116 author = "Stephen King"
118 title = "For Whom the Bell Tolls"
119 author = "Ernest Hemmingway"
121 title = "Neuromancer"
122 author = "William Gibson"
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() {
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")
139 query, err := Compile("$.foo.bar")
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)
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"))