Skip to content

Commit

Permalink
Fix SliceNode String()
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 21, 2023
1 parent 8cd12a9 commit cc5d294
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (n *MemberNode) String() string {
}

func (n *SliceNode) String() string {
if n.From == nil && n.To == nil {
return fmt.Sprintf("%s[:]", n.Node.String())
}
if n.From == nil {
return fmt.Sprintf("%s[:%s]", n.Node.String(), n.To.String())
}
if n.To == nil {
return fmt.Sprintf("%s[%s:]", n.Node.String(), n.From.String())
}
return fmt.Sprintf("%s[%s:%s]", n.Node.String(), n.From.String(), n.To.String())
}

Expand Down
7 changes: 5 additions & 2 deletions ast/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -63,14 +64,16 @@ func TestPrint(t *testing.T) {
{`a.b()`, `a.b()`},
{`a.b(c)`, `a.b(c)`},
{`a[1:-1]`, `a[1:-1]`},
{`a[1:]`, `a[1:]`},
{`a[1:]`, `a[1:]`},
{`a[:]`, `a[:]`},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
tree, err := parser.Parse(tt.input)
require.NoError(t, err)

require.Equal(t, tt.want, tree.Node.String())
assert.Equal(t, tt.want, tree.Node.String())
})
}
}
Expand Down

0 comments on commit cc5d294

Please sign in to comment.