4444// hello
4545// ...
4646// ```
47+ //
48+ // # SPQ tests
49+ //
50+ // A fenced code block with mdtest-spq as the first word of its info string
51+ // contains an SPQ test. The content of the block must comprise three sections,
52+ // each preceeded by one or more "#"-prefixed lines. The first section contains
53+ // an SPQ program, the second contains input provided to the program when the
54+ // test runs, and the third contains the program's expected output.
55+ //
56+ // ```mdtest-spq
57+ // # spq
58+ // yield a
59+ // # input
60+ // {a:1}
61+ // # expected output
62+ // 1
63+ // ```
4764package mdtest
4865
4966import (
@@ -53,6 +70,7 @@ import (
5370 "io/fs"
5471 "os"
5572 "path/filepath"
73+ "regexp"
5674 "strconv"
5775 "strings"
5876 "testing"
@@ -131,6 +149,9 @@ func (f *File) Run(t *testing.T) {
131149 }
132150}
133151
152+ // Matches one or more "#"-prefixed lines.
153+ var spqSeparatorRE = regexp .MustCompile (`(?m:^#.*\n)+` )
154+
134155func parseMarkdown (source []byte ) (map [string ]string , []* Test , error ) {
135156 var commandFCB * ast.FencedCodeBlock
136157 var inputs map [string ]string
@@ -197,6 +218,23 @@ func parseMarkdown(source []byte) (map[string]string, []*Test, error) {
197218 GoExample : fcbLines (fcb , source ),
198219 Line : fcbLineNumber (fcb , source ),
199220 })
221+ case "mdtest-spq" :
222+ lines := fcbLines (fcb , source )
223+ if ! strings .HasPrefix (lines , "#" ) {
224+ return ast .WalkStop , fcbError (fcb , source , "mdtest-spq content must begin with '#'" )
225+ }
226+ sections := spqSeparatorRE .Split (lines , - 1 )
227+ // Ignore sections[0].
228+ if n := len (sections ); n != 4 {
229+ msg := fmt .Sprintf ("mdtest-spq content has %d '#'-prefixed sections (expected 3)" , n - 1 )
230+ return ast .WalkStop , fcbError (fcb , source , msg )
231+ }
232+ tests = append (tests , & Test {
233+ Expected : sections [3 ],
234+ Line : fcbLineNumber (fcb , source ),
235+ Input : sections [2 ],
236+ SPQ : sections [1 ],
237+ })
200238 }
201239 return ast .WalkContinue , nil
202240 })
0 commit comments