|
| 1 | +package goat |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + //"fmt" |
| 8 | + "io" |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "text/template" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + examplesDir = "examples" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + write = flag.Bool("write", |
| 23 | + false, "write reference SVG output files") |
| 24 | + svgColorLightScheme = flag.String("svg-color-light-scheme", "#000000", |
| 25 | + `See help for cmd/goat`) |
| 26 | + svgColorDarkScheme = flag.String("svg-color-dark-scheme", "#FFFFFF", |
| 27 | + `See help for cmd/goat`) |
| 28 | + |
| 29 | + // Begin the directory name with '_' to hide from git. |
| 30 | + svgDeltaDir = flag.String("svg-delta-dir", "_examples_new", |
| 31 | + `Directory to be filled with a delta-image file for each |
| 32 | +newly-generated SVG that does not match those in ` + examplesDir) |
| 33 | +) |
| 34 | + |
| 35 | +func TestExamples(t *testing.T) { |
| 36 | + // XX This sweeps up ~every~ *.txt file in examples/ |
| 37 | + txtPaths, err := filepath.Glob(filepath.Join(examplesDir, "*.txt")) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + baseNames := make([]string, len(txtPaths)) |
| 43 | + for i := range txtPaths { |
| 44 | + baseName, found := strings.CutPrefix(txtPaths[i], examplesDir+"/") |
| 45 | + if !found { |
| 46 | + panic("Could not cut prefix from pathname.") |
| 47 | + } |
| 48 | + baseNames[i] = baseName |
| 49 | + } |
| 50 | + |
| 51 | + if *write { |
| 52 | + writeExamples(examplesDir, examplesDir, baseNames, *svgColorLightScheme, *svgColorDarkScheme) |
| 53 | + } else { |
| 54 | + t.Logf("Verifying equality of current SVG with examples/ references.\n") |
| 55 | + verifyExamples(t, examplesDir, baseNames) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +func writeExamples(inDir, outDir string, baseNames []string, lightColor, darkColor string) { |
| 61 | + for _, name := range baseNames { |
| 62 | + in := getIn(inDir + "/" + name) |
| 63 | + out := getOut(outDir + "/" + name) |
| 64 | + BuildAndWriteSVG(in, out, lightColor, darkColor) |
| 65 | + in.Close() |
| 66 | + out.Close() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func verifyExamples(t *testing.T, examplesDir string, baseNames []string) { |
| 71 | + var failures []string |
| 72 | + for _, name := range baseNames { |
| 73 | + in := getIn(examplesDir + "/" + name) |
| 74 | + buff := &bytes.Buffer{} |
| 75 | + BuildAndWriteSVG(in, buff, *svgColorLightScheme, *svgColorDarkScheme) |
| 76 | + in.Close() |
| 77 | + if nil != compareSVG(t, buff, examplesDir, name) { |
| 78 | + failures = append(failures, name) |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + if len(failures) > 0 { |
| 83 | + t.Logf(`Failed to verify contents of %d .svg files`, |
| 84 | + len(failures)) |
| 85 | + err := os.Mkdir(*svgDeltaDir, 0770) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf(` |
| 88 | + Aborting: "%v"`, err) |
| 89 | + } |
| 90 | + writeExamples(examplesDir, *svgDeltaDir, failures, "#000088", "#88CCFF") |
| 91 | + writeDeltaHTML(t, "../" + examplesDir, *svgDeltaDir, failures) |
| 92 | + t.FailNow() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func compareSVG(t *testing.T, buff *bytes.Buffer, examplesDir string, baseName string) error { |
| 97 | + fileName := examplesDir + "/" + baseName |
| 98 | + golden, err := getOutString(fileName) |
| 99 | + if err != nil { |
| 100 | + t.Log(err) |
| 101 | + } |
| 102 | + if newStr := buff.String(); newStr != golden { |
| 103 | + // Skip complaint if the modification timestamp of the .txt file |
| 104 | + // source is fresher than that of the .svg? |
| 105 | + // => NO, Any .txt difference might be an editing mistake. |
| 106 | + |
| 107 | + t.Logf("Content mismatch for %s. Length was %d, expected %d", |
| 108 | + toSVGFilename(fileName), buff.Len(), len(golden)) |
| 109 | + for i:=0; i<min(len(golden), len(newStr)); i++ { |
| 110 | + if newStr[i] != golden[i] { |
| 111 | + t.Logf("Differing runes at offset %d: new='%#v' reference='%#v'\n", |
| 112 | + i, newStr[i], golden[i]) |
| 113 | + break |
| 114 | + } |
| 115 | + } |
| 116 | + t.Logf("Generated contents do not match existing %s", |
| 117 | + toSVGFilename(fileName)) |
| 118 | + return errors.New("Generated contents do not match existing") |
| 119 | + } else { |
| 120 | + if testing.Verbose() { |
| 121 | + t.Logf("Existing and generated contents match %s\n", |
| 122 | + toSVGFilename(fileName)) |
| 123 | + } |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// See https://developer.mozilla.org/en-US/docs/Web/CSS/blend-mode#example_using_difference |
| 129 | +func writeDeltaHTML(t *testing.T, examplesDir, deltaDir string, baseNames []string) { |
| 130 | + t.Logf("Writing new SVG and HTML delta files into %s/", deltaDir) |
| 131 | + |
| 132 | + tmpl := template.Must(template.New("_ignored_").Parse(` |
| 133 | +<style type="text/css"> |
| 134 | +.blended-images { |
| 135 | + height: 100%; /* XX How to make equal to pixel bounds of the SVGs? */ |
| 136 | + background-size: contain, contain; |
| 137 | + background-repeat: no-repeat; |
| 138 | + background-blend-mode: difference; |
| 139 | + background-image: url('{{.ExamplesDir}}/{{.SvgBaseName}}'), url('{{.DeltaDir}}/{{.SvgBaseName}}'); |
| 140 | + } |
| 141 | +</style> |
| 142 | +
|
| 143 | +<div style="background-color: grey;"> |
| 144 | + <div class="blended-images"></div> |
| 145 | +</div> |
| 146 | +`)) |
| 147 | + for _, name := range baseNames { |
| 148 | + htmlOutName := stripSuffix(name) + ".html" |
| 149 | + t.Logf("\t%s", htmlOutName) |
| 150 | + htmlOutFile, err := os.Create(deltaDir + "/" + htmlOutName) |
| 151 | + err = tmpl.Execute(htmlOutFile, map[string]string{ |
| 152 | + "ExamplesDir": examplesDir, |
| 153 | + "DeltaDir": ".", |
| 154 | + "SvgBaseName": toSVGFilename(name), |
| 155 | + }) |
| 156 | + htmlOutFile.Close() |
| 157 | + if err != nil { |
| 158 | + panic(err) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func getIn(txtFilename string) io.ReadCloser { |
| 164 | + in, err := os.Open(txtFilename) |
| 165 | + if err != nil { |
| 166 | + panic(err) |
| 167 | + } |
| 168 | + return in |
| 169 | +} |
| 170 | + |
| 171 | +func getOutExport(pathPrefix, txtBaseName string) io.WriteCloser { |
| 172 | + svgBaseName := toSVGFilename(txtBaseName) |
| 173 | + out, err := os.Create(pathPrefix + svgBaseName) |
| 174 | + if err != nil { |
| 175 | + panic(err) |
| 176 | + } |
| 177 | + return out |
| 178 | +} |
| 179 | + |
| 180 | +func getOut(txtFilename string) io.WriteCloser { |
| 181 | + out, err := os.Create(toSVGFilename(txtFilename)) |
| 182 | + if err != nil { |
| 183 | + panic(err) |
| 184 | + } |
| 185 | + return out |
| 186 | +} |
| 187 | + |
| 188 | +func getOutString(txtFilename string) (string, error) { |
| 189 | + b, err := ioutil.ReadFile(toSVGFilename(txtFilename)) |
| 190 | + if err != nil { |
| 191 | + // XX Simply panic rather than return an error? |
| 192 | + return "", err |
| 193 | + } |
| 194 | + // XX Why are there RETURN characters in contents of the .SVG files? |
| 195 | + b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n")) |
| 196 | + return string(b), nil |
| 197 | +} |
| 198 | + |
| 199 | +func toSVGFilename(txtFilename string) string { |
| 200 | + return strings.TrimSuffix(txtFilename, filepath.Ext(txtFilename)) + ".svg" |
| 201 | +} |
| 202 | + |
| 203 | +func stripSuffix(basename string) string { |
| 204 | + return strings.Split(basename,".")[0] |
| 205 | +} |
0 commit comments