Refactor writer tests

Until now we're not using t.Run to create a sub test for each fixture - for the
current bug I want to only run a single file as I'm a print debugger and don't
care for the noise of logs from other tests. While at it, it made sense to
merge the implementations.
This commit is contained in:
Niklas Fasching 2022-06-18 15:06:48 +02:00
parent 10b71b143a
commit 0f145082ad
2 changed files with 20 additions and 30 deletions

View file

@ -21,20 +21,7 @@ func (w *ExtendedOrgWriter) WriteText(t Text) {
}
func TestOrgWriter(t *testing.T) {
for _, path := range orgTestFiles() {
expected := fileString(path[:len(path)-len(".org")] + ".pretty_org")
reader, writer := strings.NewReader(fileString(path)), NewOrgWriter()
actual, err := New().Silent().Parse(reader, path).Write(writer)
if err != nil {
t.Errorf("%s\n got error: %s", path, err)
continue
}
if actual != expected {
t.Errorf("%s:\n%s'", path, diff(actual, expected))
} else {
t.Logf("%s: passed!", path)
}
}
testWriter(t, func() Writer { return NewOrgWriter() }, ".pretty_org")
}
func TestExtendedOrgWriter(t *testing.T) {
@ -48,6 +35,22 @@ func TestExtendedOrgWriter(t *testing.T) {
}
}
func testWriter(t *testing.T, newWriter func() Writer, ext string) {
for _, path := range orgTestFiles() {
tmpPath := path[:len(path)-len(".org")]
t.Run(filepath.Base(tmpPath), func(t *testing.T) {
expected := fileString(t, tmpPath+ext)
reader := strings.NewReader(fileString(t, path))
actual, err := New().Silent().Parse(reader, path).Write(newWriter())
if err != nil {
t.Fatalf("%s\n got error: %s", path, err)
} else if actual != expected {
t.Fatalf("%s:\n%s'", path, "")
}
})
}
}
func orgTestFiles() []string {
dir := "./testdata"
files, err := ioutil.ReadDir(dir)
@ -65,10 +68,10 @@ func orgTestFiles() []string {
return orgFiles
}
func fileString(path string) string {
func fileString(t *testing.T, path string) string {
bs, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("Could not read file %s: %s", path, err))
t.Fatalf("Could not read file %s: %s", path, err)
}
return string(bs)
}