From 23297d770bda2ed535780a6edee3dfa4c8b96b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domas=20Tama=C5=A1auskas?= Date: Sat, 25 Jul 2026 17:00:31 +0300 Subject: [PATCH] Rewrite ast error tests --- tests/ast/ast.odin | 109 ++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 70 deletions(-) diff --git a/tests/ast/ast.odin b/tests/ast/ast.odin index f322cec..4533fe1 100644 --- a/tests/ast/ast.odin +++ b/tests/ast/ast.odin @@ -7,32 +7,12 @@ import "../../token" import "core:testing" @(test) -let_statement :: proc(t: ^testing.T) { - tokens := []token.Token { - {type = .Let}, - {type = .Identifier, literal = "x"}, - {type = .Assignment}, - {type = .Integer, literal = "5"}, - {type = .Semicolon}, - } - - program, errors := ast.parse(tokens) - defer free_all() - - testing.expect_value(t, len(program.statements), 1) - testing.expect_value(t, len(errors), 0) - statement, ok := program.statements[0].(ast.Let_Statement) - testing.expect(t, ok) - testing.expect_value(t, statement.name^, ast.Identifier{value = "x"}) -} - -@(test) -let_statement_error :: proc(t: ^testing.T) { +error :: proc(t: ^testing.T) { tests := map[string]struct { input: [dynamic]token.Token, output: string, } { - "missing assignment" = { + "let missing assignment" = { input = [dynamic]token.Token { {type = .Let}, {type = .Identifier, literal = "x"}, @@ -41,7 +21,7 @@ let_statement_error :: proc(t: ^testing.T) { }, output = "expected Assignment, but got Integer", }, - "missing identifier" = { + "let missing identifier" = { input = [dynamic]token.Token { {type = .Let}, {type = .Assignment}, @@ -50,7 +30,7 @@ let_statement_error :: proc(t: ^testing.T) { }, output = "expected Identifier, but got Assignment", }, - "missing integer" = { + "let missing integer" = { input = [dynamic]token.Token { {type = .Let}, {type = .Identifier, literal = "x"}, @@ -59,7 +39,7 @@ let_statement_error :: proc(t: ^testing.T) { }, output = "expected Integer, but got Semicolon", }, - "missing semicolon" = { + "let missing semicolon" = { input = [dynamic]token.Token { {type = .Let}, {type = .Identifier, literal = "x"}, @@ -68,6 +48,22 @@ let_statement_error :: proc(t: ^testing.T) { }, output = "expected Semicolon, but got Illegal", }, + "return missing integer" = { + input = [dynamic]token.Token{{type = .Return}, {type = .Semicolon}}, + output = "expected Integer, but got Semicolon", + }, + "return missing semicolon" = { + input = [dynamic]token.Token{{type = .Return}, {type = .Integer, literal = "0"}}, + output = "expected Semicolon, but got Illegal", + }, + "identifier missing semicolon" = { + input = [dynamic]token.Token{{type = .Identifier, literal = "x"}}, + output = "expected Semicolon, but got Illegal", + }, + "integer missing semicolon" = { + input = [dynamic]token.Token{{type = .Integer, literal = "0"}}, + output = "expected Semicolon, but got Illegal", + }, } defer free_all() @@ -80,9 +76,11 @@ let_statement_error :: proc(t: ^testing.T) { } @(test) -return_statement :: proc(t: ^testing.T) { +let_statement :: proc(t: ^testing.T) { tokens := []token.Token { - {type = .Return}, + {type = .Let}, + {type = .Identifier, literal = "x"}, + {type = .Assignment}, {type = .Integer, literal = "5"}, {type = .Semicolon}, } @@ -92,34 +90,27 @@ return_statement :: proc(t: ^testing.T) { testing.expect_value(t, len(program.statements), 1) testing.expect_value(t, len(errors), 0) - statement, ok := program.statements[0].(ast.Return_Statement) + statement, ok := program.statements[0].(ast.Let_Statement) testing.expect(t, ok) - testing.expect_value(t, statement.value, ast.Identifier{value = "5"}) + testing.expect_value(t, statement.name^, ast.Identifier{value = "x"}) } @(test) -return_statement_error :: proc(t: ^testing.T) { - tests := map[string]struct { - input: [dynamic]token.Token, - output: string, - } { - "missing integer" = { - input = [dynamic]token.Token{{type = .Return}, {type = .Semicolon}}, - output = "expected Integer, but got Semicolon", - }, - "missing semicolon" = { - input = [dynamic]token.Token{{type = .Return}, {type = .Integer, literal = "0"}}, - output = "expected Semicolon, but got Illegal", - }, +return_statement :: proc(t: ^testing.T) { + tokens := []token.Token { + {type = .Return}, + {type = .Integer, literal = "5"}, + {type = .Semicolon}, } + + program, errors := ast.parse(tokens) defer free_all() - for name, data in tests { - program, errors := ast.parse(data.input[:]) - expect.value(t, name, len(program.statements), 0) - expect.value(t, name, len(errors), 1) - expect.value(t, name, string(errors[0]), data.output) - } + testing.expect_value(t, len(program.statements), 1) + testing.expect_value(t, len(errors), 0) + statement, ok := program.statements[0].(ast.Return_Statement) + testing.expect(t, ok) + testing.expect_value(t, statement.value, ast.Identifier{value = "5"}) } @(test) @@ -135,17 +126,6 @@ identifier_expression :: proc(t: ^testing.T) { testing.expect_value(t, statement.expression, ast.Identifier{value = "x"}) } -@(test) -identifier_without_semicolon :: proc(t: ^testing.T) { - tokens := []token.Token{{type = .Identifier, literal = "x"}} - program, errors := ast.parse(tokens) - defer free_all() - - testing.expect_value(t, len(program.statements), 0) - testing.expect_value(t, len(errors), 1) - testing.expect_value(t, errors[0], "expected Semicolon, but got Illegal") -} - @(test) integer_expression :: proc(t: ^testing.T) { tokens := []token.Token{{type = .Integer, literal = "0"}, {type = .Semicolon}} @@ -158,14 +138,3 @@ integer_expression :: proc(t: ^testing.T) { testing.expect(t, ok) testing.expect_value(t, statement.expression, ast.Integer{value = 0}) } - -@(test) -integer_without_semicolon :: proc(t: ^testing.T) { - tokens := []token.Token{{type = .Integer, literal = "0"}} - program, errors := ast.parse(tokens) - defer free_all() - - testing.expect_value(t, len(program.statements), 0) - testing.expect_value(t, len(errors), 1) - testing.expect_value(t, errors[0], "expected Semicolon, but got Illegal") -} -- 2.51.2