Skip to content

Commit 3a0a1da

Browse files
committed
Refactor examples tests
1 parent 646c17c commit 3a0a1da

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed

test/examples/examples_test.go

+12-35
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,32 @@
11
package main
22

33
import (
4-
"flag"
54
"os"
6-
"strings"
75
"testing"
86

97
"github.com/expr-lang/expr"
108
"github.com/expr-lang/expr/internal/testify/require"
119
)
1210

13-
func TestExamples(t *testing.T) {
14-
flag.Parse()
11+
var examples []CodeBlock
1512

13+
func init() {
1614
b, err := os.ReadFile("../../testdata/examples.md")
17-
require.NoError(t, err)
18-
examples := extractCodeBlocks(string(b))
15+
if err != nil {
16+
panic(err)
17+
}
18+
examples = extractCodeBlocksWithTitle(string(b))
19+
}
1920

20-
for _, line := range examples {
21-
line := line
22-
t.Run(line, func(t *testing.T) {
23-
program, err := expr.Compile(line, expr.Env(nil))
21+
func TestExamples(t *testing.T) {
22+
for _, code := range examples {
23+
code := code
24+
t.Run(code.Title, func(t *testing.T) {
25+
program, err := expr.Compile(code.Content, expr.Env(nil))
2426
require.NoError(t, err)
2527

2628
_, err = expr.Run(program, nil)
2729
require.NoError(t, err)
2830
})
2931
}
3032
}
31-
32-
func extractCodeBlocks(markdown string) []string {
33-
var blocks []string
34-
var currentBlock []string
35-
inBlock := false
36-
37-
lines := strings.Split(markdown, "\n")
38-
for _, line := range lines {
39-
trimmed := strings.TrimSpace(line)
40-
if strings.HasPrefix(trimmed, "```") {
41-
if inBlock {
42-
blocks = append(blocks, strings.Join(currentBlock, "\n"))
43-
currentBlock = nil
44-
inBlock = false
45-
} else {
46-
inBlock = true
47-
}
48-
continue
49-
}
50-
if inBlock {
51-
currentBlock = append(currentBlock, line)
52-
}
53-
}
54-
return blocks
55-
}

test/examples/markdown.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// CodeBlock holds the optional title and content of a code block.
8+
type CodeBlock struct {
9+
Title string
10+
Content string
11+
}
12+
13+
func extractCodeBlocksWithTitle(markdown string) []CodeBlock {
14+
var blocks []CodeBlock
15+
var currentBlock []string
16+
var currentTitle string
17+
inBlock := false
18+
19+
// Split the markdown into lines.
20+
lines := strings.Split(markdown, "\n")
21+
for i, line := range lines {
22+
trimmed := strings.TrimSpace(line)
23+
// Check if the line starts with a code block fence.
24+
if strings.HasPrefix(trimmed, "```") {
25+
// If already inside a code block, this marks its end.
26+
if inBlock {
27+
blocks = append(blocks, CodeBlock{
28+
Title: currentTitle,
29+
Content: strings.Join(currentBlock, "\n"),
30+
})
31+
currentBlock = nil
32+
inBlock = false
33+
currentTitle = ""
34+
} else {
35+
// Not in a block: starting a new code block.
36+
// Look backwards for the closest non-empty line that is not a code fence.
37+
title := ""
38+
for j := i - 1; j >= 0; j-- {
39+
prev := strings.TrimSpace(lines[j])
40+
if prev == "" || strings.HasPrefix(prev, "```") {
41+
continue
42+
}
43+
title = prev
44+
break
45+
}
46+
currentTitle = title
47+
inBlock = true
48+
}
49+
// Skip the fence line.
50+
continue
51+
}
52+
53+
// If inside a code block, add the line.
54+
if inBlock {
55+
currentBlock = append(currentBlock, line)
56+
}
57+
}
58+
return blocks
59+
}

0 commit comments

Comments
 (0)