fx/search_test.go

191 lines
4.9 KiB
Go
Raw Normal View History

2022-03-11 15:15:24 +00:00
package main
2022-04-07 08:32:34 +00:00
import (
2022-04-17 20:57:12 +00:00
. "github.com/antonmedv/fx/pkg/dict"
. "github.com/antonmedv/fx/pkg/json"
2022-04-07 08:32:34 +00:00
"github.com/stretchr/testify/require"
"regexp"
"testing"
)
2022-04-14 12:30:36 +00:00
func Test_search_values(t *testing.T) {
tests := []struct {
name string
object interface{}
want *foundRange
}{
{name: "null", object: nil},
{name: "true", object: true},
{name: "false", object: false},
2022-04-17 20:57:12 +00:00
{name: "Number", object: Number("42")},
2022-04-14 12:30:36 +00:00
{name: "string", object: "Hello, World!"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &model{
json: tt.object,
}
re, _ := regexp.Compile(".+")
2022-04-17 20:57:12 +00:00
str := Stringify(m.json)
2022-04-14 12:30:36 +00:00
indexes := re.FindAllStringIndex(str, -1)
m.remapSearchResult(m.json, "", 0, indexes, 0, nil)
s := &searchResult{path: ""}
s.ranges = append(s.ranges, &foundRange{
parent: s,
path: "",
start: 0,
end: len(str),
kind: valueRange,
})
require.Equal(t, []*searchResult{s}, m.searchResults)
})
}
}
func Test_search_array(t *testing.T) {
2022-04-07 08:32:34 +00:00
msg := `
2022-04-14 12:30:36 +00:00
["first","second"]
^^^^^ ^^^^^^
2022-04-07 08:32:34 +00:00
`
m := &model{
2022-04-17 20:57:12 +00:00
json: Array{"first", "second"},
2022-04-07 08:32:34 +00:00
}
re, _ := regexp.Compile("\\w+")
2022-04-17 20:57:12 +00:00
indexes := re.FindAllStringIndex(Stringify(m.json), -1)
2022-04-07 08:32:34 +00:00
m.remapSearchResult(m.json, "", 0, indexes, 0, nil)
s1 := &searchResult{path: "[0]"}
s1.ranges = append(s1.ranges,
&foundRange{
parent: s1,
2022-04-14 12:30:36 +00:00
path: "[0]",
2022-04-07 08:32:34 +00:00
start: 1,
end: 6,
kind: valueRange,
},
)
s2 := &searchResult{path: "[1]", index: 1}
s2.ranges = append(s2.ranges,
&foundRange{
parent: s2,
2022-04-14 12:30:36 +00:00
path: "[1]",
2022-04-07 08:32:34 +00:00
start: 1,
end: 7,
kind: valueRange,
},
)
require.Equal(t, []*searchResult{s1, s2}, m.searchResults, msg)
}
2022-04-14 12:30:36 +00:00
func Test_search_between_array(t *testing.T) {
2022-04-07 08:32:34 +00:00
msg := `
2022-04-14 12:30:36 +00:00
["first","second"]
^^^^^^^^^^^^^^
2022-04-07 08:32:34 +00:00
`
m := &model{
2022-04-17 20:57:12 +00:00
json: Array{"first", "second"},
2022-04-07 08:32:34 +00:00
}
re, _ := regexp.Compile("\\w.+\\w")
2022-04-17 20:57:12 +00:00
indexes := re.FindAllStringIndex(Stringify(m.json), -1)
2022-04-07 08:32:34 +00:00
m.remapSearchResult(m.json, "", 0, indexes, 0, nil)
s := &searchResult{path: "[0]"}
s.ranges = append(s.ranges,
&foundRange{
parent: s,
2022-04-14 12:30:36 +00:00
path: "[0]",
2022-04-07 08:32:34 +00:00
start: 1,
end: 7,
kind: valueRange,
},
&foundRange{
parent: s,
2022-04-14 12:30:36 +00:00
path: "[0]",
start: 0,
end: 1,
kind: commaRange,
},
&foundRange{
parent: s,
path: "[1]",
2022-04-07 08:32:34 +00:00
start: 0,
end: 7,
kind: valueRange,
},
)
require.Equal(t, []*searchResult{s}, m.searchResults, msg)
}
2022-04-14 12:30:36 +00:00
func Test_search_dict(t *testing.T) {
2022-04-07 08:32:34 +00:00
msg := `
{"key": "hello world"}
^^^^^ ^^^^^^^^^^^^^
`
2022-04-17 20:57:12 +00:00
d := NewDict()
d.Set("key", "hello world")
2022-04-07 08:32:34 +00:00
m := &model{
json: d,
}
re, _ := regexp.Compile("\"[\\w\\s]+\"")
2022-04-17 20:57:12 +00:00
indexes := re.FindAllStringIndex(Stringify(m.json), -1)
2022-04-07 08:32:34 +00:00
m.remapSearchResult(m.json, "", 0, indexes, 0, nil)
s1 := &searchResult{path: ".key"}
s1.ranges = append(s1.ranges,
&foundRange{
parent: s1,
2022-04-14 12:30:36 +00:00
path: ".key",
2022-04-07 08:32:34 +00:00
start: 0,
end: 5,
kind: keyRange,
},
)
s2 := &searchResult{path: ".key", index: 1}
s2.ranges = append(s2.ranges,
&foundRange{
parent: s2,
2022-04-14 12:30:36 +00:00
path: ".key",
2022-04-07 08:32:34 +00:00
start: 0,
end: 13,
kind: valueRange,
},
)
require.Equal(t, []*searchResult{s1, s2}, m.searchResults, msg)
}
2022-04-14 12:30:36 +00:00
func Test_search_dict_with_array(t *testing.T) {
msg := `
{"first": [1,2],"second": []}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`
2022-04-17 20:57:12 +00:00
d := NewDict()
d.Set("first", Array{Number("1"), Number("2")})
d.Set("second", Array{})
2022-04-14 12:30:36 +00:00
m := &model{
json: d,
}
re, _ := regexp.Compile(".+")
2022-04-17 20:57:12 +00:00
indexes := re.FindAllStringIndex(Stringify(m.json), -1)
2022-04-14 12:30:36 +00:00
m.remapSearchResult(m.json, "", 0, indexes, 0, nil)
s := &searchResult{path: ""}
s.ranges = append(s.ranges,
/* { */ &foundRange{parent: s, path: "", start: 0, end: 1, kind: openBracketRange},
/* "first" */ &foundRange{parent: s, path: ".first", start: 0, end: 7, kind: keyRange},
/* : */ &foundRange{parent: s, path: ".first", start: 0, end: 2, kind: delimRange},
/* [ */ &foundRange{parent: s, path: ".first", start: 0, end: 1, kind: openBracketRange},
/* 1 */ &foundRange{parent: s, path: ".first[0]", start: 0, end: 1, kind: valueRange},
/* , */ &foundRange{parent: s, path: ".first[0]", start: 0, end: 1, kind: commaRange},
/* 2 */ &foundRange{parent: s, path: ".first[1]", start: 0, end: 1, kind: valueRange},
/* ] */ &foundRange{parent: s, path: ".first", start: 0, end: 1, kind: closeBracketRange},
/* , */ &foundRange{parent: s, path: ".first", start: 0, end: 1, kind: commaRange},
/* "second" */ &foundRange{parent: s, path: ".second", start: 0, end: 8, kind: keyRange},
/* : */ &foundRange{parent: s, path: ".second", start: 0, end: 2, kind: delimRange},
/* [ */ &foundRange{parent: s, path: ".second", start: 0, end: 1, kind: openBracketRange},
/* ] */ &foundRange{parent: s, path: ".second", start: 0, end: 1, kind: closeBracketRange},
/* } */ &foundRange{parent: s, path: "", start: 0, end: 1, kind: closeBracketRange},
)
require.Equal(t, []*searchResult{s}, m.searchResults, msg)
}