@@ -156,6 +156,32 @@ func findRoot() (string, error) {
156156
157157func fileExists (p string ) bool { _ , err := os .Stat (p ); return err == nil }
158158
159+ func containsPkgSelector (code , pkg string ) bool {
160+ target := pkg + "."
161+ searchFrom := 0
162+ for {
163+ idx := strings .Index (code [searchFrom :], target )
164+ if idx < 0 {
165+ return false
166+ }
167+ idx += searchFrom
168+ if idx == 0 || ! isIdentByte (code [idx - 1 ]) {
169+ return true
170+ }
171+ searchFrom = idx + len (target )
172+ if searchFrom >= len (code ) {
173+ return false
174+ }
175+ }
176+ }
177+
178+ func isIdentByte (b byte ) bool {
179+ return (b >= 'a' && b <= 'z' ) ||
180+ (b >= 'A' && b <= 'Z' ) ||
181+ (b >= '0' && b <= '9' ) ||
182+ b == '_'
183+ }
184+
159185func modulePath (root string ) (string , error ) {
160186 data , err := os .ReadFile (filepath .Join (root , "go.mod" ))
161187 if err != nil {
@@ -203,6 +229,8 @@ type Example struct {
203229
204230var exampleHeader = regexp .MustCompile (`(?i)^\s*Example:\s*(.*)$` )
205231var groupHeader = regexp .MustCompile (`(?i)^\s*@group\s+(.+)$` )
232+ var testTBAssertionCall = regexp .MustCompile (`\bAssert[A-Za-z0-9_]*\(\s*t\b` )
233+ var testVarMethodCall = regexp .MustCompile (`\bt\.(Helper|Fatal|Fatalf|Error|Errorf|Log|Logf|Skip|Skipf|SkipNow)\(` )
206234
207235type docLine struct {
208236 text string
@@ -495,6 +523,7 @@ func selectPackage(pkgs map[string]*ast.Package) (string, error) {
495523//
496524
497525func writeMain (base string , fd * FuncDoc , moduleImportPath , importPath string ) error {
526+ fd .Examples = runnableExamples (fd .Examples )
498527 if len (fd .Examples ) == 0 {
499528 return nil
500529 }
@@ -563,7 +592,7 @@ func writeMain(base string, fd *FuncDoc, moduleImportPath, importPath string) er
563592 if strings .Contains (ex .Code , "redis." ) {
564593 imports ["github.com/redis/go-redis/v9" ] = true
565594 }
566- if strings . Contains (ex .Code , "time. " ) {
595+ if containsPkgSelector (ex .Code , "time" ) {
567596 imports ["time" ] = true
568597 }
569598 if strings .Contains (ex .Code , "gocron" ) {
@@ -663,6 +692,30 @@ func writeMain(base string, fd *FuncDoc, moduleImportPath, importPath string) er
663692 return os .WriteFile (filepath .Join (dir , "main.go" ), buf .Bytes (), 0o644 )
664693}
665694
695+ func runnableExamples (in []Example ) []Example {
696+ out := make ([]Example , 0 , len (in ))
697+ for _ , ex := range in {
698+ if requiresTestingTB (ex .Code ) {
699+ continue
700+ }
701+ out = append (out , ex )
702+ }
703+ return out
704+ }
705+
706+ func requiresTestingTB (code string ) bool {
707+ if strings .Contains (code , "testing." ) {
708+ return true
709+ }
710+ if testTBAssertionCall .MatchString (code ) {
711+ return true
712+ }
713+ if testVarMethodCall .MatchString (code ) {
714+ return true
715+ }
716+ return false
717+ }
718+
666719func exampleDirName (fd * FuncDoc ) string {
667720 if fd .Package != "" && fd .Package != "queue" && fd .Owner == "" {
668721 return strings .ToLower (fd .Package ) + "-" + strings .ToLower (fd .Name )
0 commit comments