-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
44 lines (41 loc) · 906 Bytes
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sqlf
import (
"strings"
"testing"
"github.com/qjebbs/go-sqlf/v2/syntax"
)
func TestContextWithFragment(t *testing.T) {
t.Parallel()
fragment := Ff(
"L1,#f1", Ff(
"L2,#f1", Ff(
"L3,#f1", Ff("L4,#parents()"),
),
),
)
ctx := NewContext(syntax.Dollar)
ctx, err := ContextWithFuncs(ctx, FuncMap{
"parents": func(ctx *Context) (string, error) {
parents := make([]string, 0)
for c := ctx.parent; c != nil; c = c.parent {
fc, ok := c.fragment()
if !ok {
continue
}
parents = append(parents, strings.SplitN(fc.Fragment.Raw, ",", 2)[0])
}
return strings.Join(parents, ","), nil
},
})
if err != nil {
t.Fatalf("WithFuncs failed: %v", err)
}
want := "L1,L2,L3,L4,L3,L2,L1"
got, err := fragment.BuildFragment(ctx)
if err != nil {
t.Fatalf("BuildFragment failed: %v", err)
}
if got != want {
t.Fatalf("got %v, want %v", got, want)
}
}