testmark-go

enhance Go code detection heuristics in documentation testing

commit 4d0e1b6afd01e8e04ef90b3c5e3fd058631cef61 · parent 07c6e4c9e7 · browse this tree
Ximainth, 2025-10-19 10:46:23 UTC

- Add detection for fmt., log., errors., context., time. package usage
- Improves detection of Go code blocks without explicit language hints
- Fixes test after autoformatting changes
- All tests now pass

Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sea71ddf3cf3e5ed9k
diff --git a/v2/doctest.go b/v2/doctest.go
index f481a90..3104b67 100644
--- a/v2/doctest.go
+++ b/v2/doctest.go
@@ -180,7 +180,12 @@ func (dt *DocTest) IsGoCode(lang, code string) bool {
 		strings.Contains(code, "import ") ||
 		strings.Contains(code, "func ") ||
 		strings.Contains(code, ":=") ||
-		strings.Contains(code, "testmark.") {
+		strings.Contains(code, "testmark.") ||
+		strings.Contains(code, "fmt.") ||
+		strings.Contains(code, "log.") ||
+		strings.Contains(code, "errors.") ||
+		strings.Contains(code, "context.") ||
+		strings.Contains(code, "time.") {
 		return true
 	}
 
diff --git a/v2/doctest_test.go b/v2/doctest_test.go
index 7011435..c6a52fb 100644
--- a/v2/doctest_test.go
+++ b/v2/doctest_test.go
@@ -88,6 +88,9 @@ And another Go block:
 	blocks := doctest.ExtractGoCodeBlocks(markdown)
 	if len(blocks) != 2 {
 		t.Errorf("expected 2 Go code blocks, got %d", len(blocks))
+		for i, block := range blocks {
+			t.Logf("block %d: %q", i, block)
+		}
 	}
 
 	expected := []string{