modernc.org/knuth@v0.0.4/tangle/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 tangle // modernc.org/knuth/tangle 6 7 import ( 8 "bytes" 9 gohex "encoding/hex" 10 "os" 11 "path/filepath" 12 "testing" 13 14 "github.com/pmezard/go-difflib/difflib" 15 ) 16 17 func TestMain(m *testing.M) { 18 os.Exit(m.Run()) 19 } 20 21 func Test(t *testing.T) { 22 pascalFile := bytes.NewBuffer(nil) 23 poolFile := bytes.NewBuffer(nil) 24 stdout := bytes.NewBuffer(nil) 25 stderr := bytes.NewBuffer(nil) 26 webFile, err := os.Open(filepath.FromSlash("testdata/tex.web")) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 defer webFile.Close() 32 33 changeFile, err := os.Open(filepath.FromSlash("testdata/tex.ch")) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 defer changeFile.Close() 39 40 expectPascal, err := os.ReadFile(filepath.FromSlash("testdata/tex.p")) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 expectPool, err := os.ReadFile(filepath.FromSlash("testdata/tex.pool")) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 err = Main(webFile, changeFile, pascalFile, poolFile, stdout, stderr) 51 if err != nil { 52 t.Logf("stdout:\n%s", stdout.Bytes()) 53 t.Logf("stderr:\n%s", stderr.Bytes()) 54 t.Fatal(err) 55 } 56 57 log := true 58 if g, e := pascalFile.String(), (string(expectPascal)); g != e { 59 log = false 60 t.Logf("stdout:\n%s", stdout.Bytes()) 61 t.Logf("stderr:\n%s", stderr.Bytes()) 62 diff := difflib.UnifiedDiff{ 63 A: difflib.SplitLines(e), 64 B: difflib.SplitLines(g), 65 FromFile: "expected", 66 ToFile: "got", 67 Context: 0, 68 } 69 s, _ := difflib.GetUnifiedDiffString(diff) 70 t.Errorf( 71 "result differs\n%v\n--- expected\n%s\n\n--- got\n%s\n\n--- expected\n%s\n--- got\n%s", 72 s, e, g, gohex.Dump([]byte(e)), gohex.Dump([]byte(g)), 73 ) 74 } else { 75 t.Log("produced Pascal matches OK") 76 } 77 78 if g, e := poolFile.String(), (string(expectPool)); g != e { 79 if log { 80 t.Logf("stdout:\n%s", stdout.Bytes()) 81 t.Logf("stderr:\n%s", stderr.Bytes()) 82 } 83 diff := difflib.UnifiedDiff{ 84 A: difflib.SplitLines(e), 85 B: difflib.SplitLines(g), 86 FromFile: "expected", 87 ToFile: "got", 88 Context: 0, 89 } 90 s, _ := difflib.GetUnifiedDiffString(diff) 91 t.Errorf( 92 "result differs\n%v\n--- expected\n%s\n\n--- got\n%s\n\n--- expected\n%s\n--- got\n%s", 93 s, e, g, gohex.Dump([]byte(e)), gohex.Dump([]byte(g)), 94 ) 95 } else { 96 t.Log("produced string pool matches OK") 97 } 98 }