github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/metamorphic/history_test.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package metamorphic
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"regexp"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/cockroachdb/datadriven"
    15  	"github.com/pmezard/go-difflib/difflib"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestHistoryLogger(t *testing.T) {
    20  	var buf bytes.Buffer
    21  	h := newHistory(nil, &buf)
    22  	h.Infof("hello\nworld\n")
    23  	h.Fatalf("hello\n\nworld")
    24  
    25  	expected := `// INFO: hello
    26  // INFO: world
    27  // FATAL: hello
    28  // FATAL: 
    29  // FATAL: world
    30  `
    31  	if actual := buf.String(); expected != actual {
    32  		t.Fatalf("expected\n%s\nbut found\n%s", expected, actual)
    33  	}
    34  }
    35  
    36  func TestHistoryFail(t *testing.T) {
    37  	var buf bytes.Buffer
    38  	h := newHistory(regexp.MustCompile("foo"), &buf)
    39  	h.Recordf(1, "bar")
    40  	require.NoError(t, h.Error())
    41  	h.Recordf(2, "foo bar")
    42  	require.EqualError(t, h.Error(), `failure regexp "foo" matched output: foo bar #2`)
    43  }
    44  
    45  func TestReorderHistory(t *testing.T) {
    46  	datadriven.RunTest(t, "testdata/reorder_history", func(t *testing.T, d *datadriven.TestData) string {
    47  		switch d.Cmd {
    48  		case "reorder":
    49  			lines := difflib.SplitLines(string(d.Input))
    50  			lines = reorderHistory(lines)
    51  			return strings.Join(lines, "")
    52  		default:
    53  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    54  		}
    55  	})
    56  }