xgo/parser/parser_for_test.go
2019-01-08 23:17:42 -08:00

118 lines
2.3 KiB
Go

package parser_test
import (
"testing"
"github.com/d5/tengo/ast"
"github.com/d5/tengo/token"
)
func TestFor(t *testing.T) {
expect(t, "for {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(nil, nil, nil, blockStmt(p(1, 5), p(1, 6)), p(1, 1)))
})
expect(t, "for a == 5 {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
nil,
binaryExpr(
ident("a", p(1, 5)),
intLit(5, p(1, 10)),
token.Equal,
p(1, 7)),
nil,
blockStmt(p(1, 12), p(1, 13)),
p(1, 1)))
})
expect(t, "for a := 0; a == 5; {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
assignStmt(
exprs(ident("a", p(1, 5))),
exprs(intLit(0, p(1, 10))),
token.Define, p(1, 7)),
binaryExpr(
ident("a", p(1, 13)),
intLit(5, p(1, 18)),
token.Equal,
p(1, 15)),
nil,
blockStmt(p(1, 22), p(1, 23)),
p(1, 1)))
})
expect(t, "for a := 0; a < 5; a++ {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
assignStmt(
exprs(ident("a", p(1, 5))),
exprs(intLit(0, p(1, 10))),
token.Define, p(1, 7)),
binaryExpr(
ident("a", p(1, 13)),
intLit(5, p(1, 17)),
token.Less,
p(1, 15)),
incDecStmt(
ident("a", p(1, 20)),
token.Inc, p(1, 21)),
blockStmt(p(1, 24), p(1, 25)),
p(1, 1)))
})
expect(t, "for ; a < 5; a++ {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
nil,
binaryExpr(
ident("a", p(1, 7)),
intLit(5, p(1, 11)),
token.Less,
p(1, 9)),
incDecStmt(
ident("a", p(1, 14)),
token.Inc, p(1, 15)),
blockStmt(p(1, 18), p(1, 19)),
p(1, 1)))
})
expect(t, "for a := 0; ; a++ {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
assignStmt(
exprs(ident("a", p(1, 5))),
exprs(intLit(0, p(1, 10))),
token.Define, p(1, 7)),
nil,
incDecStmt(
ident("a", p(1, 15)),
token.Inc, p(1, 16)),
blockStmt(p(1, 19), p(1, 20)),
p(1, 1)))
})
expect(t, "for a == 5 && b != 4 {}", func(p pfn) []ast.Stmt {
return stmts(
forStmt(
nil,
binaryExpr(
binaryExpr(
ident("a", p(1, 5)),
intLit(5, p(1, 10)),
token.Equal,
p(1, 7)),
binaryExpr(
ident("b", p(1, 15)),
intLit(4, p(1, 20)),
token.NotEqual,
p(1, 17)),
token.LAnd,
p(1, 12)),
nil,
blockStmt(p(1, 22), p(1, 23)),
p(1, 1)))
})
}