...

Source file src/github.com/pelletier/go-toml/query/query_test.go

Documentation: github.com/pelletier/go-toml/query

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

View as plain text