Skip to content

Commit eb61655

Browse files
committed
chore: fix examples
1 parent 78b2e67 commit eb61655

File tree

7 files changed

+56
-79
lines changed

7 files changed

+56
-79
lines changed

docs/examplegen/main.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,32 @@ func findRoot() (string, error) {
156156

157157
func 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+
159185
func 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

204230
var exampleHeader = regexp.MustCompile(`(?i)^\s*Example:\s*(.*)$`)
205231
var 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

207235
type docLine struct {
208236
text string
@@ -495,6 +523,7 @@ func selectPackage(pkgs map[string]*ast.Package) (string, error) {
495523
//
496524

497525
func 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+
666719
func exampleDirName(fd *FuncDoc) string {
667720
if fd.Package != "" && fd.Package != "queue" && fd.Owner == "" {
668721
return strings.ToLower(fd.Package) + "-" + strings.ToLower(fd.Name)

examples/new/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package main
88
import (
99
"context"
1010
"github.com/goforj/queue"
11-
"time"
1211
)
1312

1413
func main() {

examples/queue-withworkers/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
package main
77

8-
import (
9-
"github.com/goforj/queue"
10-
"time"
11-
)
8+
import "github.com/goforj/queue"
129

1310
func main() {
1411
// WithWorkers sets desired worker concurrency before StartWorkers.

examples/queuefake-fake-reset/main.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/queuefake-fake-workflow/main.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/queuefake-new/main.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/withworkers/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
package main
77

8-
import (
9-
"github.com/goforj/queue"
10-
"time"
11-
)
8+
import "github.com/goforj/queue"
129

1310
func main() {
1411
// WithWorkers sets desired worker concurrency before StartWorkers.

0 commit comments

Comments
 (0)