From b462f85a19768625daf440e51e19bb6f3ded0205 Mon Sep 17 00:00:00 2001 From: Akash Date: Wed, 15 Jan 2025 22:24:46 +0530 Subject: [PATCH] feat: add unit tests for lexer scanner functionality Signed-off-by: Akash --- lexer/lexer_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ transpiler/transpiler.go | 1 - 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lexer/lexer_test.go diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go new file mode 100644 index 0000000..3e94df7 --- /dev/null +++ b/lexer/lexer_test.go @@ -0,0 +1,41 @@ +package lexer + +import ( + "testing" +) + +func TestNewScanner(t *testing.T) { + src := "fn main() {}" + scanner := NewScanner(src) + if scanner.source != src { + t.Errorf("Expected source %q, got %q", src, scanner.source) + } + if scanner.line != 1 { + t.Errorf("Expected line to be 1, got %d", scanner.line) + } +} + +func TestScanTokens_EmptySource(t *testing.T) { + scanner := NewScanner("") + tokens, errs := scanner.ScanTokens() + if len(tokens) != 1 { + t.Errorf("Expected 1 token (EOF), got %d", len(tokens)) + } + if len(errs) != 0 { + t.Errorf("Expected no errors, got %d", len(errs)) + } + if tokens[0].Type != EOF { + t.Errorf("Expected EOF token, got %v", tokens[0].Type) + } +} + +func TestScanTokens_ValidSource(t *testing.T) { + scanner := NewScanner("fn main() {}") + tokens, errs := scanner.ScanTokens() + if len(errs) != 0 { + t.Errorf("Expected no errors, got %d", len(errs)) + } + if len(tokens) == 0 { + t.Error("Expected some tokens, got none") + } +} diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go index b7d5651..86d234b 100644 --- a/transpiler/transpiler.go +++ b/transpiler/transpiler.go @@ -5,7 +5,6 @@ import ( "GoCrab/lexer" "GoCrab/parser" "fmt" - ) var hasError bool