Skip to content

Commit

Permalink
Improve code block attributes parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jan 21, 2025
1 parent 7892570 commit 6662d79
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Features

* Optionally extracts doc-tests to run with `mojo test` (#81)
* Optionally extracts doc-tests to run with `mojo test` (#81, #88)

### Documentation

Expand Down
6 changes: 2 additions & 4 deletions docs/content/guide/doctests.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn add(a: Int, b: Int) -> Int:
Function `add` sums up its arguments.
```mojo {doctest="add" global=true hide=true}
from testing import *
from testing import assert_equal
from mypkg import add
```
Expand Down Expand Up @@ -87,12 +87,10 @@ var result = add(1, 2)
Further, Modo🧯 creates a test file with this content:

```mojo
from testing import *
from testing import assert_equal
from mypkg import add
fn test_add() raises:
result = add(1, 2)
assert_equal(result, 3)
```

----
16 changes: 13 additions & 3 deletions document/doctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,32 @@ func parseBlockAttr(line string) (name string, hide bool, global bool, ok bool,
return
}
attrString = strings.TrimSuffix(attrString, "}")
attrPairs := strings.Split(attrString, " ")

quoted := false
attrPairs := strings.FieldsFunc(attrString, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == ' '
})

for _, pair := range attrPairs {
elems := strings.Split(pair, "=")
if len(elems) != 2 {
if len(elems) > 2 {
err = fmt.Errorf("malformed code block attributes '%s'", pair)
return
}
if len(elems) == 1 {
continue
}

key := strings.TrimSpace(elems[0])
if key == docTestAttr {
name = strings.Trim(elems[1], "\"")
continue
}
if key == hideAttr {
h := strings.Trim(elems[1], "\"")
h := strings.Trim(elems[1], "\" ")
if h == "true" {
hide = true
}
Expand Down
29 changes: 29 additions & 0 deletions document/doctest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ import (
"github.com/stretchr/testify/assert"
)

func TestParseBlockAttributes(t *testing.T) {
tests := []struct {
Text, Name string
Hide, Global, Ok, Error bool
}{
{"```mojo",
"", false, false, false, false},
{"```mojo {doctest=\"test\" hide=true global=true}",
"test", true, true, true, false},
{"```mojo { doctest=\"test\" hide=true global=true }",
"test", true, true, true, false},
{"```mojo {doctest=\"test\"}",
"test", false, false, true, false},
{"```mojo {other=\"abc\" doctest=\"test\"}",
"test", false, false, true, false},
{"```mojo {.class1 doctest=\"test\" class2}",
"test", false, false, true, false},
}

for _, test := range tests {
name, hide, global, ok, err := parseBlockAttr(test.Text)
assert.Equal(t, name, test.Name, "Name %s", test.Text)
assert.Equal(t, hide, test.Hide, "Hide %s", test.Text)
assert.Equal(t, global, test.Global, "Global %s", test.Text)
assert.Equal(t, ok, test.Ok, "Ok %s", test.Text)
assert.Equal(t, err != nil, test.Error, "Err %s %s", test.Text, err)
}
}

func TestExtractDocTests(t *testing.T) {
text := "Docstring\n" +
"\n" +
Expand Down

0 comments on commit 6662d79

Please sign in to comment.