From e48e36bea84eb59bca15784c10f9ae93f3c7af43 Mon Sep 17 00:00:00 2001 From: Fernando Araoz Date: Thu, 12 Dec 2024 07:11:06 -0500 Subject: [PATCH] test: add tests for variable parsing, fix mistakes --- src/02_syntax/variable.zig | 60 +++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/02_syntax/variable.zig b/src/02_syntax/variable.zig index 09180ae..74f2bca 100644 --- a/src/02_syntax/variable.zig +++ b/src/02_syntax/variable.zig @@ -46,7 +46,9 @@ const VariableBinding = struct { return ParseError.Error; }; errdefer allocator.destroy(exp); - try exp.init(tokens, pos + 3); + exp.init(tokens, pos + 3) catch { + return ParseError.Error; + }; // return target.* = .{ @@ -102,3 +104,59 @@ test "should fail if the idenfier is missing" { try std.testing.expect(false); } + +test "should fail if there is not an identifier after var" { + const input = "var 322"; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Error, err); + return; + }; + + try std.testing.expect(false); +} + +test "should fail if the equal sign is missing" { + const input = "var my_id "; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Error, err); + return; + }; + + try std.testing.expect(false); +} + +test "should fail if the equal sign is not found" { + const input = "var my_id is string"; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Error, err); + return; + }; + + try std.testing.expect(false); +} + +test "should fail if the expression parsing fails" { + const input = "var my_id = ehhh"; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Error, err); + return; + }; + + try std.testing.expect(false); +}