Mako/tests/integration_test.go
2025-05-06 15:55:55 -05:00

203 lines
3.7 KiB
Go

package tests
import (
"testing"
"git.sharkk.net/Sharkk/Mako/compiler"
"git.sharkk.net/Sharkk/Mako/lexer"
"git.sharkk.net/Sharkk/Mako/parser"
"git.sharkk.net/Sharkk/Mako/vm"
)
// Helper function to execute Mako code
func executeMako(code string) *vm.VM {
lex := lexer.New(code)
p := parser.New(lex)
program := p.ParseProgram()
bytecode := compiler.Compile(program)
virtualMachine := vm.New()
virtualMachine.Run(bytecode)
return virtualMachine
}
func TestBasicExecution(t *testing.T) {
// We can't directly validate output, but we can check for absence of errors
executeMako(`
// Variables and echo
x = 5;
y = 10;
echo x + y;
`)
}
func TestTableOperations(t *testing.T) {
executeMako(`
// Table creation and access
person = {
name = "John",
age = 30,
isActive = true
};
echo person["name"];
person["location"] = "New York";
echo person["location"];
`)
}
func TestConditionalExecution(t *testing.T) {
executeMako(`
// If-else statements
x = 5;
if x < 10 then
echo "x is less than 10";
else
echo "x is not less than 10";
end
// Nested if-else
y = 20;
if x > y then
echo "x is greater than y";
elseif x < y then
echo "x is less than y";
else
echo "x equals y";
end
`)
}
func TestScopes(t *testing.T) {
executeMako(`
// Global scope
x = 10;
echo x;
// Enter a new scope
{
// Local scope - variable shadowing
x = 20;
echo x;
// New local variable
y = 30;
echo y;
}
// Back to global scope
echo x;
`)
}
func TestArithmeticOperations(t *testing.T) {
executeMako(`
// Basic arithmetic
echo 5 + 10;
echo 20 - 5;
echo 4 * 5;
echo 20 / 4;
// Compound expressions
echo (5 + 10) * 2;
echo 5 + 10 * 2;
echo -5 + 10;
`)
}
func TestComparisonOperations(t *testing.T) {
executeMako(`
// Basic comparisons
echo 5 == 5;
echo 5 != 10;
echo 5 < 10;
echo 10 > 5;
echo 5 <= 5;
echo 5 >= 5;
// Compound comparisons
echo 5 + 5 == 10;
echo 5 * 2 != 15;
`)
}
func TestLogicalOperations(t *testing.T) {
executeMako(`
// Logical operators
echo true and true;
echo true and false;
echo true or false;
echo false or false;
echo not true;
echo not false;
// Short-circuit evaluation
x = 5;
echo true or (x = 10); // Should not change x
echo x; // Should still be 5
echo false and (x = 15); // Should not change x
echo x; // Should still be 5
`)
}
func TestComplexProgram(t *testing.T) {
executeMako(`
// Define a table to store data
data = {
users = {
admin = {
name = "Admin User",
access = "full",
active = true
},
guest = {
name = "Guest User",
access = "limited",
active = true
},
blocked = {
name = "Blocked User",
access = "none",
active = false
}
},
settings = {
theme = "dark",
notifications = true,
language = "en"
}
};
// Function to check if user has access
// Since Mako doesn't have actual functions yet, we'll simulate with code blocks
// Get the user type from input (simulated)
userType = "admin";
// Check access and print message
if data["users"][userType]["active"] then
access = data["users"][userType]["access"];
if access == "full" then
echo "Welcome, Administrator!";
elseif access == "limited" then
echo "Welcome, Guest!";
else
echo "Access denied.";
end
else
echo "User account is inactive.";
end
// Update a setting
data["settings"]["theme"] = "light";
echo "Theme changed to: " + data["settings"]["theme"];
// Toggle notifications
data["settings"]["notifications"] = not data["settings"]["notifications"];
echo "Notifications: " + data["settings"]["notifications"];
`)
}