fx/path/path_test.go

138 lines
1.8 KiB
Go
Raw Normal View History

2023-09-13 13:52:31 +00:00
package path_test
2023-09-12 11:06:48 +00:00
import (
"testing"
"github.com/stretchr/testify/require"
2023-09-13 13:52:31 +00:00
"github.com/antonmedv/fx/path"
2023-09-12 11:06:48 +00:00
)
func Test_SplitPath(t *testing.T) {
tests := []struct {
input string
want []any
}{
{
input: "",
want: []any{},
},
{
input: ".",
want: []any{},
},
{
input: "x",
want: []any{},
},
{
input: ".foo",
want: []any{"foo"},
},
{
input: "x.foo",
want: []any{"foo"},
},
{
input: "x[42]",
want: []any{42},
},
{
input: ".[42]",
want: []any{42},
},
{
input: ".42",
want: []any{"42"},
},
{
input: ".физ",
want: []any{"физ"},
},
{
input: ".foo.bar",
want: []any{"foo", "bar"},
},
{
input: ".foo[42]",
want: []any{"foo", 42},
},
{
input: ".foo[42].bar",
want: []any{"foo", 42, "bar"},
},
{
input: ".foo[1][2]",
want: []any{"foo", 1, 2},
},
{
input: ".foo[\"bar\"]",
want: []any{"foo", "bar"},
},
{
input: ".foo[\"bar\\\"\"]",
want: []any{"foo", "bar\""},
},
{
input: ".foo['bar']['baz\\'']",
want: []any{"foo", "bar", "baz\\'"},
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
2023-09-13 13:52:31 +00:00
p, ok := path.Split(tt.input)
2023-09-12 11:06:48 +00:00
require.Equal(t, tt.want, p)
require.True(t, ok)
})
}
}
func Test_SplitPath_negative(t *testing.T) {
tests := []struct {
input string
}{
{
input: "./",
},
{
input: "x/",
},
{
input: "1+1",
},
{
input: "x[42",
},
{
input: ".i % 2",
},
{
input: "x[for x]",
},
{
input: "x['y'.",
},
{
input: "x[0?",
},
{
input: "x[\"\\u",
},
{
input: "x['\\n",
},
{
input: "x[9999999999999999999999999999999999999]",
},
{
input: "x[]",
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
2023-09-13 13:52:31 +00:00
p, ok := path.Split(tt.input)
2023-09-12 11:06:48 +00:00
require.False(t, ok, p)
})
}
}