Go does not support inheritance, just composition. While composition with type embedding (i.e. forwarding method calls to the embedded type) can replace inheritance for most use cases this is not one of them. We really want to overwrite methods so that method calls from inside the base writer also use the custom methods ouf our extending writer - naive embedding does not work here as the this in this.WriteText refers to the embedded type rather than the outer extending type (see open recursion). A simple solution is to make a reference of the extending type available from the extended type and use that for nested method calls. We'll go with that one as it does not require huge code changes. Another solution would be to flatten the writing process and not use nested method calls - this is what blackfriday does. Assuming the current solution works I feel it's cleaner and keeps the ugliness of simulating inheritance with composition contained to a small portion of the code while blackfridays approach requires all write methods to be written in a flat style (i.e. not do nested calls to write by being called twice with entering / leaving). The current solution becomes ugly if we want to do multiple levels of extending but i don't expect that to be a valid use case - if it turns out to be one we can always adapt to it later. YAGNI.
86 lines
2 KiB
Go
86 lines
2 KiB
Go
package org
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pmezard/go-difflib/difflib"
|
|
)
|
|
|
|
type ExtendedOrgWriter struct {
|
|
*OrgWriter
|
|
callCount int
|
|
}
|
|
|
|
func (w *ExtendedOrgWriter) WriteText(t Text) {
|
|
w.callCount++
|
|
w.OrgWriter.WriteText(t)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExtendedOrgWriter(t *testing.T) {
|
|
p := Paragraph{Children: []Node{Text{Content: "text"}, Text{Content: "more text"}}}
|
|
orgWriter := NewOrgWriter()
|
|
extendedWriter := &ExtendedOrgWriter{orgWriter, 0}
|
|
orgWriter.ExtendingWriter = extendedWriter
|
|
WriteNodes(extendedWriter, p)
|
|
if extendedWriter.callCount != 2 {
|
|
t.Errorf("WriteText method of extending writer was not called: CallCount %d", extendedWriter.callCount)
|
|
}
|
|
}
|
|
|
|
func orgTestFiles() []string {
|
|
dir := "./testdata"
|
|
files, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Could not read directory: %s", err))
|
|
}
|
|
orgFiles := []string{}
|
|
for _, f := range files {
|
|
name := f.Name()
|
|
if filepath.Ext(name) != ".org" {
|
|
continue
|
|
}
|
|
orgFiles = append(orgFiles, filepath.Join(dir, name))
|
|
}
|
|
return orgFiles
|
|
}
|
|
|
|
func fileString(path string) string {
|
|
bs, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Could not read file %s: %s", path, err))
|
|
}
|
|
return string(bs)
|
|
}
|
|
|
|
func diff(actual, expected string) string {
|
|
diff := difflib.UnifiedDiff{
|
|
A: difflib.SplitLines(actual),
|
|
B: difflib.SplitLines(expected),
|
|
FromFile: "Actual",
|
|
ToFile: "Expected",
|
|
Context: 3,
|
|
}
|
|
text, _ := difflib.GetUnifiedDiffString(diff)
|
|
return text
|
|
}
|