diff --git a/test/logger_test.go b/test/logger_test.go new file mode 100644 index 0000000..667d2ea --- /dev/null +++ b/test/logger_test.go @@ -0,0 +1,59 @@ +package integration + +import ( + "os" + "os/exec" + "strings" + "testing" + + "github.com/marte-community/marte-dev-tools/internal/logger" +) + +func TestLoggerPrint(t *testing.T) { + if os.Getenv("TEST_LOGGER_PRINT") == "1" { + logger.Printf("Test Printf %d", 123) + logger.Println("Test Println") + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestLoggerPrint") + cmd.Env = append(os.Environ(), "TEST_LOGGER_PRINT=1") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("process failed: %v", err) + } + output := string(out) + if !strings.Contains(output, "Test Printf 123") { + t.Error("Printf output missing") + } + if !strings.Contains(output, "Test Println") { + t.Error("Println output missing") + } +} + +func TestLoggerFatal(t *testing.T) { + if os.Getenv("TEST_LOGGER_FATAL") == "1" { + logger.Fatal("Test Fatal") + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestLoggerFatal") + cmd.Env = append(os.Environ(), "TEST_LOGGER_FATAL=1") + err := cmd.Run() + if e, ok := err.(*exec.ExitError); ok && !e.Success() { + return // Success (exit code non-zero) + } + t.Fatalf("process ran with err %v, want exit status 1", err) +} + +func TestLoggerFatalf(t *testing.T) { + if os.Getenv("TEST_LOGGER_FATALF") == "1" { + logger.Fatalf("Test Fatalf %d", 456) + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestLoggerFatalf") + cmd.Env = append(os.Environ(), "TEST_LOGGER_FATALF=1") + err := cmd.Run() + if e, ok := err.(*exec.ExitError); ok && !e.Success() { + return // Success + } + t.Fatalf("process ran with err %v, want exit status 1", err) +} diff --git a/test/lsp_coverage_test.go b/test/lsp_coverage_test.go new file mode 100644 index 0000000..8c51204 --- /dev/null +++ b/test/lsp_coverage_test.go @@ -0,0 +1,110 @@ +package integration + +import ( + "bytes" + "encoding/json" + "os" + "os/exec" + "strings" + "testing" + + "github.com/marte-community/marte-dev-tools/internal/lsp" +) + +func TestLSPIncrementalSync(t *testing.T) { + lsp.Documents = make(map[string]string) + var buf bytes.Buffer + lsp.Output = &buf + + content := "Line1\nLine2\nLine3" + uri := "file://inc.marte" + lsp.Documents[uri] = content + + // Replace "Line2" (Line 1, 0-5) with "Modified" + change := lsp.TextDocumentContentChangeEvent{ + Range: &lsp.Range{ + Start: lsp.Position{Line: 1, Character: 0}, + End: lsp.Position{Line: 1, Character: 5}, + }, + Text: "Modified", + } + + params := lsp.DidChangeTextDocumentParams{ + TextDocument: lsp.VersionedTextDocumentIdentifier{URI: uri, Version: 2}, + ContentChanges: []lsp.TextDocumentContentChangeEvent{change}, + } + +lsp.HandleDidChange(params) + + expected := "Line1\nModified\nLine3" + if lsp.Documents[uri] != expected { + t.Errorf("Incremental update failed. Got:\n%q\nWant:\n%q", lsp.Documents[uri], expected) + } + + // Insert at end + change2 := lsp.TextDocumentContentChangeEvent{ + Range: &lsp.Range{ + Start: lsp.Position{Line: 2, Character: 5}, + End: lsp.Position{Line: 2, Character: 5}, + }, + Text: "\nLine4", + } + params2 := lsp.DidChangeTextDocumentParams{ + TextDocument: lsp.VersionedTextDocumentIdentifier{URI: uri, Version: 3}, + ContentChanges: []lsp.TextDocumentContentChangeEvent{change2}, + } + lsp.HandleDidChange(params2) + + expected2 := "Line1\nModified\nLine3\nLine4" + if lsp.Documents[uri] != expected2 { + t.Errorf("Incremental insert failed. Got:\n%q\nWant:\n%q", lsp.Documents[uri], expected2) + } +} + +func TestLSPLifecycle(t *testing.T) { + var buf bytes.Buffer + lsp.Output = &buf + + // Shutdown + msgShutdown := &lsp.JsonRpcMessage{ + Method: "shutdown", + ID: 1, + } + lsp.HandleMessage(msgShutdown) + + if !strings.Contains(buf.String(), `"result":null`) { + t.Error("Shutdown response incorrect") + } + + // Exit + if os.Getenv("TEST_LSP_EXIT") == "1" { + msgExit := &lsp.JsonRpcMessage{Method: "exit"} + lsp.HandleMessage(msgExit) + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestLSPLifecycle") + cmd.Env = append(os.Environ(), "TEST_LSP_EXIT=1") + err := cmd.Run() + if err != nil { + t.Errorf("Exit failed: %v", err) + } +} + +func TestLSPMalformedParams(t *testing.T) { + var buf bytes.Buffer + lsp.Output = &buf + + // Malformed Hover + msg := &lsp.JsonRpcMessage{ + Method: "textDocument/hover", + ID: 2, + Params: json.RawMessage(`{invalid`), + } + lsp.HandleMessage(msg) + + output := buf.String() + // Should respond with nil result + if !strings.Contains(output, `"result":null`) { + t.Errorf("Expected nil result for malformed params, got: %s", output) + } +}