test: add tests for variable parsing, fix mistakes

This commit is contained in:
Fernando Araoz 2024-12-12 07:11:06 -05:00
parent fd0459aef1
commit e48e36bea8

View File

@ -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);
}