Skip to content

Commit cc5d294

Browse files
committed
Fix SliceNode String()
1 parent 8cd12a9 commit cc5d294

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ast/print.go

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ func (n *MemberNode) String() string {
9494
}
9595

9696
func (n *SliceNode) String() string {
97+
if n.From == nil && n.To == nil {
98+
return fmt.Sprintf("%s[:]", n.Node.String())
99+
}
100+
if n.From == nil {
101+
return fmt.Sprintf("%s[:%s]", n.Node.String(), n.To.String())
102+
}
103+
if n.To == nil {
104+
return fmt.Sprintf("%s[%s:]", n.Node.String(), n.From.String())
105+
}
97106
return fmt.Sprintf("%s[%s:%s]", n.Node.String(), n.From.String(), n.To.String())
98107
}
99108

ast/print_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/antonmedv/expr/ast"
77
"github.com/antonmedv/expr/parser"
8+
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
)
1011

@@ -63,14 +64,16 @@ func TestPrint(t *testing.T) {
6364
{`a.b()`, `a.b()`},
6465
{`a.b(c)`, `a.b(c)`},
6566
{`a[1:-1]`, `a[1:-1]`},
67+
{`a[1:]`, `a[1:]`},
68+
{`a[1:]`, `a[1:]`},
69+
{`a[:]`, `a[:]`},
6670
}
6771

6872
for _, tt := range tests {
6973
t.Run(tt.input, func(t *testing.T) {
7074
tree, err := parser.Parse(tt.input)
7175
require.NoError(t, err)
72-
73-
require.Equal(t, tt.want, tree.Node.String())
76+
assert.Equal(t, tt.want, tree.Node.String())
7477
})
7578
}
7679
}

0 commit comments

Comments
 (0)