Skip to content

Commit

Permalink
Proper tests for read function.
Browse files Browse the repository at this point in the history
Made a test assert helper function in the simplest way possible,
to avoid introducing dependencies.
  • Loading branch information
warpfork committed Sep 20, 2021
1 parent 7cb404c commit b64e21b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
37 changes: 28 additions & 9 deletions read_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
package testmark
package testmark_test

import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"

"github.com/warpfork/go-testmark"
)

func TestRead(t *testing.T) {
testdata, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
doc, err := ReadFile(filepath.Join(testdata, "example.md"))
doc, err := testmark.ReadFile(filepath.Join(testdata, "example.md"))
if err != nil {
t.Fatal(err)
}
for _, hunk := range doc.DataHunks {
t.Logf("hunk %q is on lines %d:%d, has body %q\n", hunk.Name, hunk.LineStart+1, hunk.LineEnd+1, string(hunk.Body))
}

readFixturesExample(t, doc)
}

func readFixturesExample(t *testing.T, doc *testmark.Document) {
assert(t, doc.DataHunks[0].Name, "this-is-the-data-name")
assert(t, doc.DataHunks[0].LineStart+1, "13")
assert(t, doc.DataHunks[0].LineEnd+1, "17")
assert(t, doc.DataHunks[0].Body, "the content of this code block is data which can be read,\nand *replaced*, by testmark.\n")

assert(t, doc.DataHunks[1].Name, "more-data")
assert(t, doc.DataHunks[1].LineStart+1, "36")
assert(t, doc.DataHunks[1].LineEnd+1, "41")
assert(t, doc.DataHunks[1].Body, "func OtherMarkdownParsers() (shouldHighlight bool) {\n\treturn true\n}\n")

assert(t, doc.DataHunks[2].Name, "cannot-describe-no-linebreak")
assert(t, doc.DataHunks[2].LineStart+1, "70")
assert(t, doc.DataHunks[2].LineEnd+1, "73")
assert(t, doc.DataHunks[2].Body, "A markdown codeblock always has a trailing linebreak before its close indicator, you see.\n")
}

func TestParseCRLF(t *testing.T) {
t.Skip("currently broken")

input, err := ioutil.ReadFile(filepath.Join("testdata", "example.md"))
if err != nil {
t.Fatal(err)
}
input = bytes.ReplaceAll(input, []byte("\n"), []byte("\r\n"))
doc, err := Parse(input)
doc, err := testmark.Parse(input)
if err != nil {
t.Fatal(err)
}
for _, hunk := range doc.DataHunks {
t.Logf("hunk %q is on lines %d:%d, has body %q\n", hunk.Name, hunk.LineStart+1, hunk.LineEnd+1, string(hunk.Body))
}

readFixturesExample(t, doc)
}
36 changes: 36 additions & 0 deletions testutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testmark_test

import (
"fmt"
"testing"
)

/*
This file is for the quickest, dumbest, most essential test helpers.
I'm refraining from using a full-blown testing library,
because (at the time of writing) the golang module system
does not differentiate test deps from runtime deps,
and if this library is going to be easy to use widely,
I'd like for its transitive dependency tree to not foist
my personal preference of testing library's onto other people's module graphs.
*/

// assert is a quick and dirty test helper.
// It stringifies anything given and uses string equality.
// You can probably give it strings or bytes and it'll probably "DTRT";
// anything else relies on "%v".
// It'll emit both the expected and actual values as strings if there's a mismatch.
func assert(t *testing.T, actual interface{}, expect string) {
var actualStr string
if s, ok := actual.(string); ok {
actualStr = s
} else if bs, ok := actual.([]byte); ok {
actualStr = string(bs)
} else {
actualStr = fmt.Sprintf("%v", actual)
}
if actualStr != expect {
t.Errorf("expected: %q;\nactual: %q", expect, actualStr)
}
}

0 comments on commit b64e21b

Please sign in to comment.