modernc.org/knuth@v0.0.4/pktype/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 pktype // modernc.org/knuth/pktype
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"github.com/pmezard/go-difflib/difflib"
    11  	"os"
    12  	"path/filepath"
    13  	"strconv"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	os.Exit(m.Run())
    20  }
    21  
    22  func Test(t *testing.T) {
    23  	stdout := bytes.NewBuffer(nil)
    24  	stderr := bytes.NewBuffer(nil)
    25  	pkFile, err := os.Open(filepath.FromSlash("testdata/cm/mf/cmr10.pk"))
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	defer pkFile.Close()
    31  
    32  	expect, err := os.ReadFile(filepath.FromSlash("testdata/cm/mf/cmr10.out"))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	err = Main(pkFile, stdout, stdout, stderr)
    38  	if err != nil {
    39  		t.Logf("stdout:\n%s", stdout.Bytes())
    40  		t.Logf("stderr:\n%s", stderr.Bytes())
    41  		t.Fatal(err)
    42  	}
    43  
    44  	if g, e := noBanner(adjust(stdout.String())), noBanner(string(expect)); g != e {
    45  		t.Logf("stdout:\n%s", stdout.Bytes())
    46  		t.Logf("stderr:\n%s", stderr.Bytes())
    47  		t.Logf("expected:\n%s", expect)
    48  		diff := difflib.UnifiedDiff{
    49  			A:        difflib.SplitLines(e),
    50  			B:        difflib.SplitLines(g),
    51  			FromFile: "expected",
    52  			ToFile:   "got",
    53  			Context:  0,
    54  		}
    55  		s, _ := difflib.GetUnifiedDiffString(diff)
    56  		t.Fatalf(
    57  			"result differs\n%v\n--- expected\n%s\n\n--- got\n%s\n\n--- expected\n%s\n--- got\n%s",
    58  			s, e, g, hex.Dump([]byte(e)), hex.Dump([]byte(g)),
    59  		)
    60  	}
    61  
    62  	t.Logf("%v bytes OK", stdout.Len())
    63  }
    64  
    65  func noBanner(s string) string {
    66  	x := strings.Index(s, "This is PKtype, Version")
    67  	x2 := strings.Index(s[x:], "\n")
    68  	return s[:x] + s[x2+1:]
    69  }
    70  
    71  // pktype from TeX-live reports locations differently.
    72  //
    73  // change eg "        80:  Num special: 434050" to "        76:  Num special: 434050"
    74  func adjust(s string) string {
    75  	a := strings.Split(s, "\n")
    76  	const tag = ":  Num special:"
    77  	for i, v := range a {
    78  		if !strings.Contains(v, tag) {
    79  			continue
    80  		}
    81  
    82  		x1 := strings.Index(v, ":")
    83  		x := x1 - 1
    84  		for x >= 0 && v[x] >= '0' && v[x] <= '9' {
    85  			x--
    86  		}
    87  		if x < 0 {
    88  			x = 0
    89  		}
    90  		s := v[x:x1]
    91  		n, err := strconv.Atoi(s)
    92  		if err != nil {
    93  			panic(todo("%q %v %v %q", v, x, x1, s))
    94  		}
    95  
    96  		a[i] = v[:x] + strconv.Itoa(n-4) + v[x1:]
    97  	}
    98  	return strings.Join(a, "\n")
    99  }