modernc.org/knuth@v0.0.4/mft/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 mft // modernc.org/knuth/mft
     6  
     7  import (
     8  	"bytes"
     9  	"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  	mfFile, err := os.ReadFile(filepath.FromSlash("testdata/test.mf"))
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	changeFile := bytes.NewBuffer(nil)
    28  	styleFile, err := os.ReadFile(filepath.FromSlash("testdata/plain.mft"))
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	texFile := bytes.NewBuffer(nil)
    34  	stdout := bytes.NewBuffer(nil)
    35  	stderr := bytes.NewBuffer(nil)
    36  	expect, err := os.ReadFile(filepath.FromSlash("testdata/test.tex"))
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	err = Main(bytes.NewReader(mfFile), changeFile, bytes.NewReader(styleFile), texFile, stdout, stderr)
    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 := texFile.String(), string(expect); g != e {
    49  		t.Logf("stderr:\n%s", stderr.Bytes())
    50  		diff := difflib.UnifiedDiff{
    51  			A:        difflib.SplitLines(e),
    52  			B:        difflib.SplitLines(g),
    53  			FromFile: "expected",
    54  			ToFile:   "got",
    55  			Context:  0,
    56  		}
    57  		s, _ := difflib.GetUnifiedDiffString(diff)
    58  		t.Fatalf(
    59  			"result differs\n%v\n--- expected\n%s\n\n--- got\n%s\n\n--- expected\n%s\n--- got\n%s",
    60  			s, e, g, hex.Dump([]byte(e)), hex.Dump([]byte(g)),
    61  		)
    62  	}
    63  
    64  	t.Logf("%v bytes OK", texFile.Len())
    65  }