35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package renderkit
|
|
|
|
import "testing"
|
|
|
|
func TestSnippets(t *testing.T) {
|
|
src := "intro\n\n```\nno lang\n```\n\n```go\npackage main\n\nfunc main() {}\n```\n\n```python\nprint(1)\n```\n\n```weird\nx\n```\n"
|
|
got := Snippets(src)
|
|
if len(got) != 2 {
|
|
t.Fatalf("want 2 snippets, got %+v", got)
|
|
}
|
|
if got[0].Index != 1 || got[0].Ext != ".go" || got[0].Lines != 3 {
|
|
t.Errorf("go snippet wrong: %+v", got[0])
|
|
}
|
|
if got[1].Index != 2 || got[1].Ext != ".py" || got[1].Label != "Python" {
|
|
t.Errorf("python snippet wrong: %+v", got[1])
|
|
}
|
|
}
|
|
|
|
func TestSnippetFilenameHint(t *testing.T) {
|
|
src := "```go\n// internal/db/users.go\npackage db\n\nvar X = 1\n```\n\n```go\n// just a comment\npackage x\n\nvar Y = 2\n```\n\n```python\n# notes.txt\nprint(1)\nprint(2)\n```\n"
|
|
got := Snippets(src)
|
|
if len(got) != 3 {
|
|
t.Fatalf("want 3, got %+v", got)
|
|
}
|
|
if got[0].Filename != "users.go" {
|
|
t.Errorf("want users.go, got %q", got[0].Filename)
|
|
}
|
|
if got[1].Filename != "" {
|
|
t.Errorf("plain comment must not become a filename: %q", got[1].Filename)
|
|
}
|
|
if got[2].Filename != "" {
|
|
t.Errorf("extension mismatch (.txt vs .py) must be ignored: %q", got[2].Filename)
|
|
}
|
|
}
|