github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readline/cache_test.go (about)

     1  package readline
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  // string
    11  
    12  func TestCacheStringMethods(t *testing.T) {
    13  	rl := NewInstance()
    14  	rl.MaxCacheSize = 10
    15  	nTests := 20
    16  	c := new(cacheString)
    17  	c.Init(rl)
    18  
    19  	count.Tests(t, (2*nTests)+2)
    20  
    21  	for i := 0; i < 20; i++ {
    22  		sLine := fmt.Sprintf("line %d", i)
    23  		rLine := []rune(sLine)
    24  		sValue := fmt.Sprintf("value %d", i)
    25  
    26  		r := c.Get(rLine)
    27  		if string(r) == sValue {
    28  			t.Fatalf(`Pre-Append error: c.Get(%s) == "%s"`, sLine, string(r))
    29  		}
    30  
    31  		c.Append(rLine, sValue)
    32  
    33  		r = c.Get(rLine)
    34  		if string(r) != sValue {
    35  			t.Fatalf(`Post-Append error: c.Get(%s) == "%s"`, sLine, string(r))
    36  		}
    37  	}
    38  
    39  	c.Init(rl)
    40  	if c.size != 0 || len(c.values["line 1"]) > 0 {
    41  		t.Error("cacheString failed to reinitialize correctly")
    42  		t.Logf("  size:                %d", c.size)
    43  		t.Logf(`  c.values["line 1"]: "%s"`, string(c.values["line 1"]))
    44  	}
    45  }
    46  
    47  func TestCacheStringOutOfBounds(t *testing.T) {
    48  	count.Tests(t, 1)
    49  
    50  	rl := NewInstance()
    51  	rl.MaxCacheSize = 10
    52  
    53  	c := new(cacheString)
    54  	c.Init(rl)
    55  
    56  	for i := 0; i < 20; i++ {
    57  		line := []rune(fmt.Sprintf("line %d", i))
    58  		value := fmt.Sprintf("value %d", i)
    59  		c.Append(line, value)
    60  	}
    61  }
    62  
    63  // []rune
    64  
    65  func TestCacheSliceRuneMethods(t *testing.T) {
    66  	rl := NewInstance()
    67  	rl.MaxCacheSize = 10
    68  	nTests := 20
    69  	c := new(cacheSliceRune)
    70  	c.Init(rl)
    71  
    72  	count.Tests(t, (2*nTests)+2)
    73  
    74  	for i := 0; i < 20; i++ {
    75  		sLine := fmt.Sprintf("line %d", i)
    76  		rLine := []rune(sLine)
    77  		sValue := fmt.Sprintf("value %d", i)
    78  		rValue := []rune(sValue)
    79  
    80  		r := c.Get(rLine)
    81  		if string(r) == sValue {
    82  			t.Fatalf(`Pre-Append error: c.Get(%s) == "%s"`, sLine, string(r))
    83  		}
    84  
    85  		c.Append(rLine, rValue)
    86  
    87  		r = c.Get(rLine)
    88  		if string(r) != sValue {
    89  			t.Fatalf(`Post-Append error: c.Get(%s) == "%s"`, sLine, string(r))
    90  		}
    91  	}
    92  
    93  	c.Init(rl)
    94  	if c.size != 0 || len(c.values["line 1"]) > 0 {
    95  		t.Error("cacheString failed to reinitialize correctly")
    96  		t.Logf("  size:                %d", c.size)
    97  		t.Logf(`  c.values["line 1"]: "%s"`, string(c.values["line 1"]))
    98  	}
    99  }
   100  
   101  func TestCacheSlineRuneOutOfBounds(t *testing.T) {
   102  	count.Tests(t, 1)
   103  
   104  	rl := NewInstance()
   105  	rl.MaxCacheSize = 10
   106  
   107  	c := new(cacheSliceRune)
   108  	c.Init(rl)
   109  
   110  	for i := 0; i < 20; i++ {
   111  		line := []rune(fmt.Sprintf("line %d", i))
   112  		value := []rune(fmt.Sprintf("value %d", i))
   113  		c.Append(line, value)
   114  	}
   115  }