Skip to content

Commit

Permalink
Enumerate examples/ for which SVG file contents may need to change
Browse files Browse the repository at this point in the history
Refactor for tidiness.
  • Loading branch information
dmullis authored and dmullis committed Jul 17, 2024
1 parent 544fa9e commit fae1668
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package goat

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

qt "github.com/frankban/quicktest"
)

var (
Expand All @@ -23,9 +23,7 @@ var (
)

// XX TXT source file suite is limited to a single file -- "circuits.txt"
func TestExamplesStableOutput(t *testing.T) {
c := qt.New(t)

func TestExampleStableOutput(t *testing.T) {
var previous string
for i := 0; i < 3; i++ {
in, err := os.Open(filepath.Join(basePath, "circuits.txt"))
Expand All @@ -36,7 +34,7 @@ func TestExamplesStableOutput(t *testing.T) {
BuildAndWriteSVG(in, &out, "black", "white")
in.Close()
if i > 0 && previous != out.String() {
c.Fail()
t.FailNow()
}
previous = out.String()

Expand All @@ -49,84 +47,87 @@ func TestExamples(t *testing.T) {
t.Fatal(err)
}

var buff *bytes.Buffer
if write == nil {
if *write {
writeExamples(t, filenames)
} else {
t.Logf("Verifying equality of current SVG with examples/ references.\n")
verifyExamples(t, filenames)
}
var failures int
}


func writeExamples(t *testing.T, filenames []string) {
for _, name := range filenames {
in := getIn(name)
var out io.WriteCloser
if *write {
out = getOut(name)
} else {
if buff == nil {
buff = &bytes.Buffer{}
} else {
buff.Reset()
}
out = struct {
io.Writer
io.Closer
}{
buff,
io.NopCloser(nil),
}
}

out := getOut(name)
BuildAndWriteSVG(in, out, *svgColorLightScheme, *svgColorDarkScheme)

in.Close()
out.Close()
}
}

if buff != nil {
golden, err := getOutString(name)
if err != nil {
t.Log(err)
}
if newStr := buff.String(); newStr != golden {
// Skip complaint if the modification timestamp of the .txt file
// source is fresher than that of the .svg?
// => NO, Any .txt difference might be an editing mistake.

t.Logf("Content mismatch for %s. Length was %d, expected %d",
toSVGFilename(name), buff.Len(), len(golden))
for i:=0; i<min(len(golden), len(newStr)); i++ {
if newStr[i] != golden[i] {
t.Logf("Differing runes at offset %d: new='%#v' reference='%#v'\n",
i, newStr[i], golden[i])
break
}
}
t.Logf("Generated contents do not match existing %s",
toSVGFilename(name))
failures++
} else {
if testing.Verbose() {
t.Logf("Existing and generated contents match %s\n",
toSVGFilename(name))
}
}
in.Close()
out.Close()
func verifyExamples(t *testing.T, filenames []string) {
var failures []string
for _, name := range filenames {
in := getIn(name)
buff := &bytes.Buffer{}
BuildAndWriteSVG(in, buff, *svgColorLightScheme, *svgColorDarkScheme)
in.Close()
if nil != compareSVG(t, buff, name) {
failures = append(failures, name)
}

}
if failures > 0 {
if len(failures) > 0 {
t.Logf(`Failed to verify contents of %d .svg files
Consider:
%s`,
failures,
"$ go test -run TestExamples -v -args -write")
Failing files:`,
len(failures))
for _, name := range failures {
svgFile := toSVGFilename(name)
fmt.Printf("\t\t%s\n", svgFile)
}
t.FailNow()
}
}

func compareSVG(t *testing.T, buff *bytes.Buffer, fileName string) error {
golden, err := getOutString(fileName)
if err != nil {
t.Log(err)
}
if newStr := buff.String(); newStr != golden {
// Skip complaint if the modification timestamp of the .txt file
// source is fresher than that of the .svg?
// => NO, Any .txt difference might be an editing mistake.

t.Logf("Content mismatch for %s. Length was %d, expected %d",
toSVGFilename(fileName), buff.Len(), len(golden))
for i:=0; i<min(len(golden), len(newStr)); i++ {
if newStr[i] != golden[i] {
t.Logf("Differing runes at offset %d: new='%#v' reference='%#v'\n",
i, newStr[i], golden[i])
break
}
}
t.Logf("Generated contents do not match existing %s",
toSVGFilename(fileName))
return errors.New("Generated contents do not match existing")
} else {
if testing.Verbose() {
t.Logf("Existing and generated contents match %s\n",
toSVGFilename(fileName))
}
}
return nil
}

func BenchmarkComplicated(b *testing.B) {
in := getIn(filepath.FromSlash("examples/complicated.txt"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
BuildAndWriteSVG(in, io.Discard, "black", "white")
}
in.Close()
}

const basePath string = "examples"
Expand All @@ -150,6 +151,7 @@ func getOut(txtFilename string) io.WriteCloser {
func getOutString(txtFilename string) (string, error) {
b, err := ioutil.ReadFile(toSVGFilename(txtFilename))
if err != nil {
// XX Simply panic rather than return an error?
return "", err
}
// XX Why are there RETURN characters in contents of the .SVG files?
Expand Down

0 comments on commit fae1668

Please sign in to comment.