Repository navigation
Expand file tree
/
Copy pathif_expr_test.go
More file actions
110 lines (105 loc) · 2.89 KB
/
Copy pathif_expr_test.go
File metadata and controls
110 lines (105 loc) · 2.89 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package parser_test
import (
"testing"
"github.com/go-ruby-parser/parser"
"github.com/go-ruby-parser/parser/ast"
)
// TestIfExpression covers `if`/`unless` used in expression position (RHS of an
// assignment, a call argument, a return value). The produced node is the same
// *ast.If used in statement position — `unless` still desugars to an `if` with a
// negated condition.
func TestIfExpression(t *testing.T) {
cases := []struct {
name string
src string
// check inspects the *ast.If reached from the program body.
check func(t *testing.T, n *ast.If)
}{
{
name: "assign rhs if",
src: "x = if c then 1 else 2 end",
check: func(t *testing.T, n *ast.If) {
if len(n.Then) != 1 || len(n.Else) != 1 {
t.Errorf("then=%d else=%d, want 1/1", len(n.Then), len(n.Else))
}
},
},
{
name: "call arg if",
src: "foo(if c then 1 else 2 end)",
check: func(t *testing.T, n *ast.If) {
if n.Else == nil {
t.Errorf("expected an else branch")
}
},
},
{
// Parenthesized: an if-expression as the return value. Without the
// parens, `return if c …` is a bare return with an if MODIFIER (MRI
// rejects `return if c then … end`), covered in TestReturnModifier.
name: "return paren if",
src: "return (if c then 1 else 2 end)",
check: func(t *testing.T, n *ast.If) {
if len(n.Then) != 1 {
t.Errorf("then=%d, want 1", len(n.Then))
}
},
},
{
name: "assign rhs unless",
src: "y = unless c then 1 else 2 end",
check: func(t *testing.T, n *ast.If) {
// unless desugars to `if !c`.
if _, ok := n.Cond.(*ast.UnaryExpr); !ok {
t.Errorf("Cond=%T, want a negated UnaryExpr", n.Cond)
}
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
prog, err := parser.Parse(c.src)
if err != nil {
t.Fatalf("Parse(%q): %v", c.src, err)
}
c.check(t, ifNode(t, prog.Body[0]))
})
}
}
// ifNode extracts the *ast.If from an assignment value, a single call argument,
// a return value, or the node itself.
func ifNode(t *testing.T, n ast.Node) *ast.If {
t.Helper()
switch v := n.(type) {
case *ast.If:
return v
case *ast.Assign:
return ifNode(t, v.Value)
case *ast.Return:
return ifNode(t, v.Value)
case *ast.Call:
if len(v.Args) != 1 {
t.Fatalf("call has %d args, want 1", len(v.Args))
}
return ifNode(t, v.Args[0])
}
t.Fatalf("no *ast.If reachable from %T", n)
return nil
}
// TestIfStatementUnchanged: statement-position if/unless still parse and still
// yield the same *ast.If node.
func TestIfStatementUnchanged(t *testing.T) {
for _, src := range []string{
"if a\n b\nelsif c\n d\nelse\n e\nend",
"unless a\n b\nelse\n c\nend",
"if a then b else c end",
} {
prog, err := parser.Parse(src)
if err != nil {
t.Fatalf("Parse(%q): %v", src, err)
}
if _, ok := prog.Body[0].(*ast.If); !ok {
t.Errorf("%q: top node %T, want *ast.If", src, prog.Body[0])
}
}
}