Skip to content

Commit d2c42d1

Browse files
committed
printer: add test for rare case of BinaryExpr and UnaryExpr
1 parent c1f9352 commit d2c42d1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

printer/expr_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"testing"
77

88
vimlparser "github.com/haya14busa/go-vimlparser"
9+
"github.com/haya14busa/go-vimlparser/ast"
10+
"github.com/haya14busa/go-vimlparser/token"
911
)
1012

1113
func TestFprint_expr(t *testing.T) {
@@ -48,3 +50,39 @@ func TestFprint_expr(t *testing.T) {
4850
}
4951
}
5052
}
53+
54+
func TestFprint_expr_insert_paren_to_binary(t *testing.T) {
55+
want := `(x + y) * z`
56+
buf := new(bytes.Buffer)
57+
left := &ast.BinaryExpr{
58+
Left: &ast.Ident{Name: "x"},
59+
Op: token.PLUS,
60+
Right: &ast.Ident{Name: "y"},
61+
}
62+
node := &ast.BinaryExpr{
63+
Left: left,
64+
Op: token.STAR,
65+
Right: &ast.Ident{Name: "z"},
66+
}
67+
if err := Fprint(buf, node, nil); err != nil {
68+
t.Fatal(err)
69+
}
70+
if got := buf.String(); got != want {
71+
t.Errorf("got %q, want %q", got, want)
72+
}
73+
}
74+
75+
func TestFprint_expr_insert_paren_to_unary(t *testing.T) {
76+
want := `(-x)[y]`
77+
buf := new(bytes.Buffer)
78+
node := &ast.SubscriptExpr{
79+
Left: &ast.UnaryExpr{Op: token.MINUS, X: &ast.Ident{Name: "x"}},
80+
Right: &ast.Ident{Name: "y"},
81+
}
82+
if err := Fprint(buf, node, nil); err != nil {
83+
t.Fatal(err)
84+
}
85+
if got := buf.String(); got != want {
86+
t.Errorf("got %q, want %q", got, want)
87+
}
88+
}

0 commit comments

Comments
 (0)