Skip to content
Myr

chunklist_test.go

This file

Path

src/chunklist_test.go

116 lines · 2.8 KiB · Go

Last changed by

Not known.

History of this file

Take it

1package fzf23import (4	"fmt"5	"testing"67	"github.com/junegunn/fzf/src/util"8)910func TestChunkList(t *testing.T) {11	// FIXME global12	sortCriteria = []criterion{byScore, byLength}1314	cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {15		item.text = util.ToChars(s)16		return true17	})1819	// Snapshot20	snapshot, count, _ := cl.Snapshot(0)21	if len(snapshot) > 0 || count > 0 {22		t.Error("Snapshot should be empty now")23	}2425	// Add some data26	cl.Push([]byte("hello"))27	cl.Push([]byte("world"))2829	// Previously created snapshot should remain the same30	if len(snapshot) > 0 {31		t.Error("Snapshot should not have changed")32	}3334	// But the new snapshot should contain the added items35	snapshot, count, _ = cl.Snapshot(0)36	if len(snapshot) != 1 && count != 2 {37		t.Error("Snapshot should not be empty now")38	}3940	// Check the content of the ChunkList41	chunk1 := snapshot[0]42	if chunk1.count != 2 {43		t.Error("Snapshot should contain only two items")44	}45	if chunk1.items[0].text.ToString() != "hello" ||46		chunk1.items[1].text.ToString() != "world" {47		t.Error("Invalid data")48	}49	if chunk1.IsFull() {50		t.Error("Chunk should not have been marked full yet")51	}5253	// Add more data54	for i := range chunkSize * 2 {55		cl.Push(fmt.Appendf(nil, "item %d", i))56	}5758	// Previous snapshot should remain the same59	if len(snapshot) != 1 {60		t.Error("Snapshot should stay the same")61	}6263	// New snapshot64	snapshot, count, _ = cl.Snapshot(0)65	if len(snapshot) != 3 || !snapshot[0].IsFull() ||66		!snapshot[1].IsFull() || snapshot[2].IsFull() || count != chunkSize*2+2 {67		t.Error("Expected two full chunks and one more chunk")68	}69	if snapshot[2].count != 2 {70		t.Error("Unexpected number of items")71	}7273	cl.Push([]byte("hello"))74	cl.Push([]byte("world"))7576	lastChunkCount := snapshot[len(snapshot)-1].count77	if lastChunkCount != 2 {78		t.Error("Unexpected number of items:", lastChunkCount)79	}80}8182func TestChunkListTail(t *testing.T) {83	cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {84		item.text = util.ToChars(s)85		return true86	})87	total := chunkSize*2 + chunkSize/288	for i := range total {89		cl.Push(fmt.Appendf(nil, "item %d", i))90	}9192	snapshot, count, changed := cl.Snapshot(0)93	assertCount := func(expected int, shouldChange bool) {94		if count != expected || CountItems(snapshot) != expected {95			t.Errorf("Unexpected count: %d (expected: %d)", count, expected)96		}97		if changed != shouldChange {98			t.Error("Unexpected change status")99		}100	}101	assertCount(total, false)102103	tail := chunkSize + chunkSize/2104	snapshot, count, changed = cl.Snapshot(tail)105	assertCount(tail, true)106107	snapshot, count, changed = cl.Snapshot(tail)108	assertCount(tail, false)109110	snapshot, count, changed = cl.Snapshot(0)111	assertCount(tail, false)112113	tail = chunkSize / 2114	snapshot, count, changed = cl.Snapshot(tail)115	assertCount(tail, true)116}