|
| 1 | +package printer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + vimlparser "github.com/haya14busa/go-vimlparser" |
| 9 | + "github.com/haya14busa/go-vimlparser/ast" |
| 10 | + "github.com/haya14busa/go-vimlparser/token" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFprint_expr(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + in string |
| 16 | + want string |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + {in: `xyz`, want: `xyz`}, // Ident |
| 20 | + {in: `"double quote"`, want: `"double quote"`}, // BasicLit |
| 21 | + {in: `14`, want: `14`}, // BasicLit |
| 22 | + {in: `+1`, want: `+1`}, // UnaryExpr |
| 23 | + {in: `- 1`, want: `-1`}, // UnaryExpr |
| 24 | + {in: `! + - 1`, want: `!+-1`}, // UnaryExpr |
| 25 | + {in: `x+1`, want: `x + 1`}, // BinaryExpr |
| 26 | + {in: `1+2*3`, want: `1 + 2 * 3`}, // BinaryExpr |
| 27 | + {in: `1*2+3`, want: `1 * 2 + 3`}, // BinaryExpr |
| 28 | + {in: `(1+2)*(3-4)`, want: `(1 + 2) * (3 - 4)`}, // ParenExpr |
| 29 | + {in: `1+(2*3)`, want: `1 + (2 * 3)`}, // ParenExpr |
| 30 | + {in: `(((x+(1))))`, want: `(x + (1))`}, // ParenExpr |
| 31 | + {in: `x+1==14 ||-1`, want: `x + 1 == 14 || -1`}, // BinaryExpr |
| 32 | + {in: `x[ y ]`, want: `x[y]`}, // SubscriptExpr |
| 33 | + } |
| 34 | + |
| 35 | + for _, tt := range tests { |
| 36 | + r := strings.NewReader(tt.in) |
| 37 | + node, err := vimlparser.ParseExpr(r) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + buf := new(bytes.Buffer) |
| 42 | + if err := Fprint(buf, node, nil); err != nil { |
| 43 | + if !tt.wantErr { |
| 44 | + t.Errorf("got unexpected error: %v", err) |
| 45 | + } |
| 46 | + continue |
| 47 | + } |
| 48 | + if got := buf.String(); got != tt.want { |
| 49 | + t.Errorf("got: %v, want: %v", got, tt.want) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 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