github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/history/history_test.go (about)

     1  package history
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  var testHistoryItems = []string{
    11  	"out: the quick brown #fox",
    12  	"out: jumped over",
    13  	"out: the lazy dog",
    14  }
    15  
    16  // TestHistory is a dummy history struct for testing
    17  type TestHistory struct {
    18  	list []string
    19  }
    20  
    21  func NewTestHistory() *TestHistory {
    22  	h := new(TestHistory)
    23  	h.list = testHistoryItems
    24  	return h
    25  }
    26  
    27  // Write item to history file. eg ~/.murex_history
    28  func (h *TestHistory) Write(s string) (int, error) {
    29  	h.list = append(h.list, s)
    30  	return len(h.list), nil
    31  }
    32  
    33  // GetLine returns a specific line from the history file
    34  func (h *TestHistory) GetLine(i int) (string, error) {
    35  	if i < 0 {
    36  		return "", errors.New("cannot use a negative index when requesting historic commands")
    37  	}
    38  	if i < len(h.list) {
    39  		return h.list[i], nil
    40  	}
    41  	return "", errors.New("index requested greater than number of items in history")
    42  }
    43  
    44  // Len returns the number of items in the history file
    45  func (h *TestHistory) Len() int {
    46  	return len(h.list)
    47  }
    48  
    49  // Dump returns the entire history file
    50  func (h *TestHistory) Dump() interface{} {
    51  	return h.list
    52  }
    53  
    54  func TestTestHistory(t *testing.T) {
    55  	count.Tests(t, 1)
    56  
    57  	h := NewTestHistory()
    58  	if h.Len() != len(testHistoryItems) {
    59  		t.Error("test history doesn't contain the number of items it is expecting")
    60  	}
    61  }