modernc.org/knuth@v0.0.4/dvitype/all_test.go (about) 1 // Copyright 2023 The Knuth Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package dvitype // modernc.org/knuth/dvitype 6 7 import ( 8 "bytes" 9 "encoding/hex" 10 "io" 11 "os" 12 "path/filepath" 13 "strings" 14 "testing" 15 16 "github.com/pmezard/go-difflib/difflib" 17 "modernc.org/knuth" 18 ) 19 20 func TestMain(m *testing.M) { 21 os.Exit(m.Run()) 22 } 23 24 func Test(t *testing.T) { 25 stdout := bytes.NewBuffer(nil) 26 stderr := bytes.NewBuffer(nil) 27 dviFile, err := os.Open(filepath.FromSlash("testdata/test.dvi")) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 defer dviFile.Close() 33 34 expect, err := os.ReadFile(filepath.FromSlash("testdata/test.out")) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 err = Main(dviFile, stdout, stderr, -1, "", -1, 0, -1, 40 func(fn string) (io.Reader, error) { return knuth.Open(fn, nil) }, 41 ) 42 if err != nil { 43 t.Logf("stdout:\n%s", stdout.Bytes()) 44 t.Logf("stderr:\n%s", stderr.Bytes()) 45 t.Fatal(err) 46 } 47 48 if g, e := noBanner(stdout.String()), noBanner(string(expect)); g != e { 49 t.Logf("stdout:\n%s", stdout.Bytes()) 50 t.Logf("stderr:\n%s", stderr.Bytes()) 51 diff := difflib.UnifiedDiff{ 52 A: difflib.SplitLines(e), 53 B: difflib.SplitLines(g), 54 FromFile: "expected", 55 ToFile: "got", 56 Context: 0, 57 } 58 s, _ := difflib.GetUnifiedDiffString(diff) 59 t.Fatalf( 60 "result differs\n%v\n--- expected\n%s\n\n--- got\n%s\n\n--- expected\n%s\n--- got\n%s", 61 s, e, g, hex.Dump([]byte(e)), hex.Dump([]byte(g)), 62 ) 63 } 64 65 t.Logf("%v bytes OK", stdout.Len()) 66 } 67 68 func noBanner(s string) string { 69 x := strings.Index(s, "This is DVItype, Version") 70 x2 := strings.Index(s[x:], "\n") 71 return s[:x] + s[x2+1:] 72 }