Skip to content

Commit bb9e33d

Browse files
committed
chore: create a single consolidated test module to cover all modules
1 parent 50af613 commit bb9e33d

File tree

14 files changed

+671
-594
lines changed

14 files changed

+671
-594
lines changed

build.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ pub fn build(b: *std.Build) void {
117117
run_step.dependOn(&run_cmd.step);
118118

119119
// --- Define Unit Tests ---
120-
// Create a test module that imports all other modules to run tests
120+
// Create single consolidated test module
121121
const test_module = b.createModule(.{
122-
.root_source_file = b.path("cmd/fun/main.zig"), // Assuming tests can be run from/imported by main
122+
.root_source_file = b.path("tests/main_test.zig"),
123123
.target = target,
124-
.optimize = .Debug, // Use Debug optimize for tests
124+
.optimize = .Debug,
125125
});
126126

127127
// Add all modules as imports to the test module

cmd/fun/main.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,3 @@ pub fn main() void {
9797
}
9898
}
9999
}
100-
101-
test {
102-
std.testing.refAllDecls(@This());
103-
}

modules/lexer/lexer.zig

Lines changed: 0 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -830,233 +830,3 @@ pub const LexProcess = struct {
830830
if (self.arg_str_buf != null) self.arg_str_buf.?.deinit();
831831
}
832832
};
833-
834-
test "LexProcess initialization" {
835-
const ifilepath = "LexProcess_initialization.fn";
836-
const ofilepath = "LexProcess_initialization.c";
837-
// Mock input file
838-
{
839-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
840-
defer file.close();
841-
const input = "dummy";
842-
try file.writeAll(input);
843-
}
844-
845-
const allocator = std.testing.allocator;
846-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
847-
var lex_proc = LexProcess.init(&transpile_proc);
848-
849-
defer transpile_proc.deinit();
850-
defer lex_proc.deinit();
851-
852-
try std.testing.expect(transpile_proc.tokens.items().len == 0);
853-
try std.testing.expect(lex_proc.transpile_proc == &transpile_proc);
854-
try std.testing.expect(lex_proc.curr_exp_count == 0);
855-
try std.testing.expect(lex_proc.parenthesis_buf == null);
856-
try std.testing.expect(lex_proc.arg_str_buf == null);
857-
858-
// Delete test files
859-
try fs.cwd().deleteFile(ifilepath);
860-
try fs.cwd().deleteFile(ofilepath);
861-
}
862-
863-
test "LexProcess next_char" {
864-
const ifilepath = "LexProcess_next_char.fn";
865-
const ofilepath = "LexProcess_next_char.c";
866-
// Mock input file
867-
{
868-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
869-
defer file.close();
870-
const input = "abc\n";
871-
try file.writeAll(input);
872-
}
873-
874-
const allocator = std.testing.allocator;
875-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
876-
var lex_proc = LexProcess.init(&transpile_proc);
877-
878-
defer transpile_proc.deinit();
879-
defer lex_proc.deinit();
880-
881-
try std.testing.expectEqual('a', (try lex_proc.next_char()).?);
882-
try std.testing.expectEqual('b', (try lex_proc.next_char()).?);
883-
try std.testing.expectEqual('c', (try lex_proc.next_char()).?);
884-
try std.testing.expectEqual('\n', (try lex_proc.next_char()).?);
885-
try std.testing.expect(try lex_proc.next_char() == null);
886-
887-
// Delete test files
888-
try fs.cwd().deleteFile(ifilepath);
889-
try fs.cwd().deleteFile(ofilepath);
890-
}
891-
892-
test "LexProcess peek_char" {
893-
const ifilepath = "LexProcess_peek_char.fn";
894-
const ofilepath = "LexProcess_peek_char.c";
895-
// Mock input file
896-
{
897-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
898-
defer file.close();
899-
const input = "abc";
900-
try file.writeAll(input);
901-
}
902-
903-
const allocator = std.testing.allocator;
904-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
905-
var lex_proc = LexProcess.init(&transpile_proc);
906-
907-
defer transpile_proc.deinit();
908-
defer lex_proc.deinit();
909-
910-
try std.testing.expectEqual('a', (try lex_proc.peek_char()).?);
911-
try std.testing.expectEqual('a', (try lex_proc.peek_char()).?);
912-
_ = try lex_proc.next_char();
913-
try std.testing.expectEqual('b', (try lex_proc.peek_char()).?);
914-
915-
// Delete test files
916-
try fs.cwd().deleteFile(ifilepath);
917-
try fs.cwd().deleteFile(ofilepath);
918-
}
919-
920-
test "LexProcess push_char" {
921-
const ifilepath = "LexProcess_push_char.fn";
922-
const ofilepath = "LexProcess_push_char.c";
923-
// Mock input file
924-
{
925-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
926-
defer file.close();
927-
const input = "abc";
928-
try file.writeAll(input);
929-
}
930-
931-
const allocator = std.testing.allocator;
932-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
933-
var lex_proc = LexProcess.init(&transpile_proc);
934-
935-
defer transpile_proc.deinit();
936-
defer lex_proc.deinit();
937-
938-
_ = try lex_proc.next_char();
939-
_ = try lex_proc.next_char();
940-
try lex_proc.push_char('b');
941-
try std.testing.expectEqual('b', (try lex_proc.next_char()).?);
942-
943-
// Delete test files
944-
try fs.cwd().deleteFile(ifilepath);
945-
try fs.cwd().deleteFile(ofilepath);
946-
}
947-
948-
test "LexProcess comment" {
949-
const ifilepath = "LexProcess_comment.fn";
950-
const ofilepath = "LexProcess_comment.c";
951-
// Mock input file
952-
{
953-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
954-
defer file.close();
955-
const input = "// This is a comment\n";
956-
try file.writeAll(input);
957-
}
958-
959-
const allocator = std.testing.allocator;
960-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
961-
var lex_proc = LexProcess.init(&transpile_proc);
962-
963-
defer transpile_proc.deinit();
964-
defer lex_proc.deinit();
965-
966-
try lex_proc.lex();
967-
const t = transpile_proc.tokens.items()[0];
968-
try std.testing.expectEqual(t.type, .Comment);
969-
try std.testing.expectEqualStrings(" This is a comment", t.data.sval.items);
970-
971-
// Delete test files
972-
try fs.cwd().deleteFile(ifilepath);
973-
try fs.cwd().deleteFile(ofilepath);
974-
}
975-
976-
test "LexProcess string" {
977-
const ifilepath = "LexProcess_string.fn";
978-
const ofilepath = "LexProcess_string.c";
979-
// Mock input file
980-
{
981-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
982-
defer file.close();
983-
const input = "\"Hello, World!\"";
984-
try file.writeAll(input);
985-
}
986-
987-
const allocator = std.testing.allocator;
988-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
989-
var lex_proc = LexProcess.init(&transpile_proc);
990-
991-
defer transpile_proc.deinit();
992-
defer lex_proc.deinit();
993-
994-
try lex_proc.lex();
995-
const t = transpile_proc.tokens.items()[0];
996-
try std.testing.expectEqual(t.type, .String);
997-
try std.testing.expectEqualStrings("Hello, World!", t.data.sval.items);
998-
999-
// Delete test files
1000-
try fs.cwd().deleteFile(ifilepath);
1001-
try fs.cwd().deleteFile(ofilepath);
1002-
}
1003-
1004-
test "LexProcess number" {
1005-
const ifilepath = "LexProcess_number.fn";
1006-
const ofilepath = "LexProcess_number.c";
1007-
// Mock input file
1008-
{
1009-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
1010-
defer file.close();
1011-
const input = "12345";
1012-
try file.writeAll(input);
1013-
}
1014-
1015-
const allocator = std.testing.allocator;
1016-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
1017-
var lex_proc = LexProcess.init(&transpile_proc);
1018-
1019-
defer transpile_proc.deinit();
1020-
defer lex_proc.deinit();
1021-
1022-
try lex_proc.lex();
1023-
const t = transpile_proc.tokens.items()[0];
1024-
try std.testing.expectEqual(t.type, .Number);
1025-
try std.testing.expectEqual(t.data.llnum, 12345);
1026-
1027-
// Delete test files
1028-
try fs.cwd().deleteFile(ifilepath);
1029-
try fs.cwd().deleteFile(ofilepath);
1030-
}
1031-
1032-
test "LexProcess lex" {
1033-
const ifilepath = "LexProcess_lex.fn";
1034-
const ofilepath = "LexProcess_lex.c";
1035-
// Mock input file
1036-
{
1037-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
1038-
defer file.close();
1039-
const input = "123 + 456 // comment\n\"string\"";
1040-
try file.writeAll(input);
1041-
}
1042-
1043-
const allocator = std.testing.allocator;
1044-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
1045-
var lex_proc = LexProcess.init(&transpile_proc);
1046-
1047-
defer transpile_proc.deinit();
1048-
defer lex_proc.deinit();
1049-
1050-
try lex_proc.lex();
1051-
1052-
try std.testing.expectEqual(6, transpile_proc.tokens.items().len);
1053-
try std.testing.expectEqual(transpile_proc.tokens.items()[0].type, .Number);
1054-
try std.testing.expectEqual(transpile_proc.tokens.items()[1].type, .Operator);
1055-
try std.testing.expectEqual(transpile_proc.tokens.items()[2].type, .Number);
1056-
try std.testing.expectEqual(transpile_proc.tokens.items()[3].type, .Comment);
1057-
try std.testing.expectEqual(transpile_proc.tokens.items()[4].type, .NewLine);
1058-
try std.testing.expectEqual(transpile_proc.tokens.items()[5].type, .String);
1059-
// Delete test files
1060-
try fs.cwd().deleteFile(ifilepath);
1061-
try fs.cwd().deleteFile(ofilepath);
1062-
}

modules/parser/parser.zig

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,102 +1920,3 @@ pub const ParseProcess = struct {
19201920
while (try self.next()) {}
19211921
}
19221922
};
1923-
1924-
test "ParseProcess parse_function" {
1925-
const ifilepath = "ParseProcess_parse_function.fn";
1926-
const ofilepath = "ParseProcess_parse_function.c";
1927-
// Mock input file
1928-
{
1929-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
1930-
defer file.close();
1931-
const input = "fun test() { ret; }";
1932-
try file.writeAll(input);
1933-
}
1934-
1935-
const allocator = std.testing.allocator;
1936-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
1937-
var lex_proc = lexer.LexProcess.init(&transpile_proc);
1938-
var parse_proc = ParseProcess.init(&transpile_proc);
1939-
1940-
defer {
1941-
lex_proc.deinit();
1942-
transpile_proc.deinit();
1943-
}
1944-
1945-
try lex_proc.lex();
1946-
try parse_proc.parse();
1947-
const nodes = transpile_proc.nodes.items();
1948-
try std.testing.expectEqual(1, nodes.len);
1949-
try std.testing.expectEqual(nodes[0].type, .Function);
1950-
try std.testing.expectEqualStrings("test", nodes[0].node_variant.?.function.name.?.items);
1951-
1952-
// Delete test files
1953-
try fs.cwd().deleteFile(ifilepath);
1954-
try fs.cwd().deleteFile(ofilepath);
1955-
}
1956-
1957-
test "ParseProcess parse_return" {
1958-
const ifilepath = "ParseProcess_parse_return.fn";
1959-
const ofilepath = "ParseProcess_parse_return.c";
1960-
// Mock input file
1961-
{
1962-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
1963-
defer file.close();
1964-
const input = "ret 42;";
1965-
try file.writeAll(input);
1966-
}
1967-
1968-
// const allocator = std.testing.allocator;
1969-
const allocator = std.testing.allocator;
1970-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
1971-
var lex_proc = lexer.LexProcess.init(&transpile_proc);
1972-
var parse_proc = ParseProcess.init(&transpile_proc);
1973-
1974-
defer {
1975-
lex_proc.deinit();
1976-
transpile_proc.deinit();
1977-
}
1978-
1979-
try lex_proc.lex();
1980-
try parse_proc.parse();
1981-
const nodes = transpile_proc.nodes.items();
1982-
try std.testing.expectEqual(1, nodes.len);
1983-
try std.testing.expectEqual(nodes[0].type, .StatementReturn);
1984-
1985-
// Delete test files
1986-
try fs.cwd().deleteFile(ifilepath);
1987-
try fs.cwd().deleteFile(ofilepath);
1988-
}
1989-
1990-
test "ParseProcess parse_expression" {
1991-
const ifilepath = "ParseProcess_parse_expression.fn";
1992-
const ofilepath = "ParseProcess_parse_expression.c";
1993-
// Mock input file
1994-
{
1995-
const file = try fs.cwd().createFile(ifilepath, .{ .read = true });
1996-
defer file.close();
1997-
const input = "1 + 2 * 3";
1998-
try file.writeAll(input);
1999-
}
2000-
2001-
// const allocator = std.testing.allocator;
2002-
const allocator = std.testing.allocator;
2003-
var transpile_proc = try codegen.TranspileProcess.init(allocator, ifilepath, ofilepath, .{ .outf = true });
2004-
var lex_proc = lexer.LexProcess.init(&transpile_proc);
2005-
var parse_proc = ParseProcess.init(&transpile_proc);
2006-
2007-
defer {
2008-
lex_proc.deinit();
2009-
transpile_proc.deinit();
2010-
}
2011-
2012-
try lex_proc.lex();
2013-
try parse_proc.parse();
2014-
const nodes = transpile_proc.nodes.items();
2015-
try std.testing.expectEqual(1, nodes.len);
2016-
try std.testing.expectEqual(nodes[0].type, .Expression);
2017-
2018-
// Delete test files
2019-
try fs.cwd().deleteFile(ifilepath);
2020-
try fs.cwd().deleteFile(ofilepath);
2021-
}

0 commit comments

Comments
 (0)