From 5282d445bce227cdbd2636eb33bff72e66a71081 Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Mon, 22 Jan 2024 15:13:53 +0800 Subject: [PATCH] fmt --- ast/node.go | 2 +- ast/search_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ast/node.go b/ast/node.go index 25ecd25f0..3e81774cd 100644 --- a/ast/node.go +++ b/ast/node.go @@ -1804,4 +1804,4 @@ func switchRawType(c byte) types.ValueType { func backward(src string, i int) int { for ; i>=0 && isSpace(src[i]); i-- {} return i -} \ No newline at end of file +} diff --git a/ast/search_test.go b/ast/search_test.go index e26130d2f..9310bbcc8 100644 --- a/ast/search_test.go +++ b/ast/search_test.go @@ -152,6 +152,11 @@ func TestExportErrNotExist(t *testing.T) { if err != test.err || node.Exists(){ t.Fatal(err) } + p.parser.p = 0 + val, err := p.GetValueByPath(test.path...) + if err != test.err || val.Exists(){ + t.Fatal(err) + } } t.Run(test.data, f) } @@ -234,10 +239,15 @@ func TestSearcher_GetByPathSingle(t *testing.T) { s := NewSearcher(test.json) node, err1 := s.GetByPath(test.path...) assert.Equal(t, test.ok, err1 == nil) - value, err2 := node.Interface() assert.Equal(t, test.value, value) assert.Equal(t, test.ok, err2 == nil) + + val, err1 := s.GetValueByPath(test.path...) + assert.Equal(t, test.ok, err1 == nil) + value, err2 = val.Interface() + assert.Equal(t, test.value, value) + assert.Equal(t, test.ok, err2 == nil) }) } } @@ -273,6 +283,36 @@ func TestSearcher_GetByPathErr(t *testing.T) { if e == nil { t.Fatalf("node: %v, err: %v", node, e) } + + val, e := s.GetByPath("zz") + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } + s.parser.p = 0 + val, e = s.GetByPath("xx", 4) + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } + s.parser.p = 0 + val, e = s.GetByPath("yy", "a") + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } + s.parser.p = 0 + val, e = s.GetByPath("test", 2, "x") + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } + s.parser.p = 0 + val, e = s.GetByPath("err1", 0) + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } + s.parser.p = 0 + val, e = s.GetByPath("err2", "x") + if e == nil { + t.Fatalf("val: %v, err: %v", val, e) + } } func TestLoadIndex(t *testing.T) {