github.com/sbinet/present-tex@v0.5.0/main_test.go (about)

     1  // Copyright 2021 The present-tex 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 main
     6  
     7  import (
     8  	"bytes"
     9  	"io/fs"
    10  	"os"
    11  	"os/exec"
    12  	"testing"
    13  )
    14  
    15  func TestConvert(t *testing.T) {
    16  	var tmpldir = func() fs.FS {
    17  		o, err := fs.Sub(tmplFS, "templates")
    18  		if err != nil {
    19  			t.Fatalf("could not locate embedded 'templates' directory: %+v", err)
    20  		}
    21  		return o
    22  	}()
    23  
    24  	err := os.Chdir("testdata")
    25  	if err != nil {
    26  		t.Fatalf("could not chdir to testdata: %+v", err)
    27  	}
    28  
    29  	for _, tc := range []struct {
    30  		input string
    31  		want  string
    32  	}{
    33  		{
    34  			input: "talk.slide",
    35  			want:  "talk_golden.tex",
    36  		},
    37  		{
    38  			input: "talk-md.slide",
    39  			want:  "talk-md_golden.tex",
    40  		},
    41  	} {
    42  		t.Run("", func(t *testing.T) {
    43  			r, err := os.ReadFile(tc.input)
    44  			if err != nil {
    45  				t.Fatalf("could not read input file %q: %+v", tc.input, err)
    46  			}
    47  
    48  			want, err := os.ReadFile(tc.want)
    49  			if err != nil {
    50  				t.Fatalf("could not read golden file %q: %+v", tc.want, err)
    51  			}
    52  
    53  			w := new(bytes.Buffer)
    54  			err = xmain(w, bytes.NewReader(r), tc.input, tmpldir)
    55  			if err != nil {
    56  				t.Fatalf("could not process document: %+v", err)
    57  			}
    58  
    59  			if got := w.Bytes(); !bytes.Equal(got, want) {
    60  				_ = os.WriteFile(tc.input+".tex", got, 0644)
    61  				out, _ := exec.Command("diff", "-urN", tc.input+".tex", tc.want).CombinedOutput()
    62  				t.Fatalf("output documents differ: %q:\n%s", tc.input, out)
    63  			}
    64  		})
    65  	}
    66  }