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:
parent
10b71b143a
commit
0f145082ad
2 changed files with 20 additions and 30 deletions
|
@ -16,20 +16,7 @@ func (w *ExtendedHTMLWriter) WriteText(t Text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTMLWriter(t *testing.T) {
|
func TestHTMLWriter(t *testing.T) {
|
||||||
for _, path := range orgTestFiles() {
|
testWriter(t, func() Writer { return NewHTMLWriter() }, ".html")
|
||||||
expected := fileString(path[:len(path)-len(".org")] + ".html")
|
|
||||||
reader, writer := strings.NewReader(fileString(path)), NewHTMLWriter()
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtendedHTMLWriter(t *testing.T) {
|
func TestExtendedHTMLWriter(t *testing.T) {
|
||||||
|
|
|
@ -21,20 +21,7 @@ func (w *ExtendedOrgWriter) WriteText(t Text) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOrgWriter(t *testing.T) {
|
func TestOrgWriter(t *testing.T) {
|
||||||
for _, path := range orgTestFiles() {
|
testWriter(t, func() Writer { return NewOrgWriter() }, ".pretty_org")
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtendedOrgWriter(t *testing.T) {
|
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 {
|
func orgTestFiles() []string {
|
||||||
dir := "./testdata"
|
dir := "./testdata"
|
||||||
files, err := ioutil.ReadDir(dir)
|
files, err := ioutil.ReadDir(dir)
|
||||||
|
@ -65,10 +68,10 @@ func orgTestFiles() []string {
|
||||||
return orgFiles
|
return orgFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileString(path string) string {
|
func fileString(t *testing.T, path string) string {
|
||||||
bs, err := ioutil.ReadFile(path)
|
bs, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
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)
|
return string(bs)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue