github.com/mithrandie/csvq@v1.18.1/lib/json/path_parser_test.go (about)

     1  package json
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var parsePathTests = []struct {
     9  	Input  string
    10  	Expect PathExpression
    11  	Error  string
    12  }{
    13  	{
    14  		Input:  "",
    15  		Expect: ObjectPath{},
    16  	},
    17  	{
    18  		Input: "abc",
    19  		Expect: ObjectPath{
    20  			Name: "abc",
    21  		},
    22  	},
    23  	{
    24  		Input: "abc\\def\\",
    25  		Expect: ObjectPath{
    26  			Name: "abc\\def\\",
    27  		},
    28  	},
    29  	{
    30  		Input: "abc.d\\.ef",
    31  		Expect: ObjectPath{
    32  			Name: "abc",
    33  			Child: ObjectPath{
    34  				Name: "d.ef",
    35  			},
    36  		},
    37  	},
    38  	{
    39  		Input: "abc.",
    40  		Error: "unexpected termination",
    41  	},
    42  	{
    43  		Input: "abc..",
    44  		Error: "unexpected token \".\"",
    45  	},
    46  }
    47  
    48  func TestParsePath(t *testing.T) {
    49  	for _, v := range parsePathTests {
    50  		result, err := ParsePath(v.Input)
    51  		if err != nil {
    52  			if len(v.Error) < 1 {
    53  				t.Errorf("unexpected error %q for %q", err.Error(), v.Input)
    54  			} else if err.Error() != v.Error {
    55  				t.Errorf("error %q, want error %q for %q", err, v.Error, v.Input)
    56  			}
    57  			continue
    58  		}
    59  		if 0 < len(v.Error) {
    60  			t.Errorf("no error, want error %q for %q", v.Error, v.Input)
    61  			continue
    62  		}
    63  		if !reflect.DeepEqual(result, v.Expect) {
    64  			t.Errorf("result = %#v, want %#v for %q", result, v.Expect, v.Input)
    65  		}
    66  	}
    67  }