diff options
| author | Bobby <[email protected]> | 2023-11-17 20:23:24 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-11-17 20:23:24 +0000 |
| commit | a797a2642018b69f7c35713c695bc767b24fcaab (patch) | |
| tree | ee7f039ad8ecf5ad11f1738969352b2fd829221a /parser/parser_test.go | |
| parent | 066ab9ccf3b1d161f91ba1f50599ca33b1708d62 (diff) | |
| download | mana-a797a2642018b69f7c35713c695bc767b24fcaab.tar.xz mana-a797a2642018b69f7c35713c695bc767b24fcaab.zip | |
added helper functions: `testIdentifier`, `testLiteralExpression` `testInfixExpression`
Diffstat (limited to 'parser/parser_test.go')
| -rw-r--r-- | parser/parser_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go index ddacb17..4d76935 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -124,6 +124,77 @@ func TestReturnStatements(t *testing.T) { } } +func testIdentifier(t *testing.T, exp ast.Expression, value string) bool { + ident, ok := exp.(*ast.Identifier) + + if !ok { + t.Errorf("exp not *ast.Identifier. got=%T", exp) + return false + } + + if ident.Value != value { + t.Errorf("ident.Value not %s. got=%s", value, ident.Value) + return false + } + + if ident.TokenLiteral() != value { + t.Errorf("ident.TokenLiteral not %s. got =%s", value, ident.TokenLiteral()) + + return false + } + + return true +} + +func testLiteralExpression( + t *testing.T, + exp ast.Expression, + expected interface{}, +) bool { + switch v := expected.(type) { + case int: + return testIntegerLiteral(t, exp, int64(v)) + + case int64: + return testIntegerLiteral(t, exp, v) + case string: + return testIdentifier(t, exp, v) + } + + t.Errorf("type of exp not handled. got=%T", exp) + return false +} + +func testInfixExpression( + t *testing.T, + exp ast.Expression, + left interface{}, + operator string, + right interface{}, +) bool { + opExp, ok := exp.(*ast.InfixExpression) + + if !ok { + t.Errorf("exp is not ast.InfixExpression. got =%T(%s)", exp, exp) + return false + } + + if !testLiteralExpression(t, opExp.Left, left) { + return false + } + + if opExp.Operator != operator { + t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator) + return false + } + + if !testLiteralExpression(t, opExp.Right, right) { + return false + } + + return true +} + // Identifier expression tests. func TestIdentifierExpression(t *testing.T) { var input string = "foobar;" |
