Skip to content
Myr

filter_test.go

This file

Path

filter/filter_test.go

59 lines · 1.3 KiB · Go

Last changed by

fix(filter): wrong highlight when option has grapheme clusters (#799)Carlos Alexandro Beckerbefore adoption3745655

History of this file

Take it

1package filter23import (4	"reflect"5	"testing"67	"github.com/charmbracelet/x/ansi"8)910func TestMatchedRanges(t *testing.T) {11	for name, tt := range map[string]struct {12		in  []int13		out [][2]int14	}{15		"empty": {16			in:  []int{},17			out: [][2]int{},18		},19		"one char": {20			in:  []int{1},21			out: [][2]int{{1, 1}},22		},23		"2 char range": {24			in:  []int{1, 2},25			out: [][2]int{{1, 2}},26		},27		"multiple char range": {28			in:  []int{1, 2, 3, 4, 5, 6},29			out: [][2]int{{1, 6}},30		},31		"multiple char ranges": {32			in:  []int{1, 2, 3, 5, 6, 10, 11, 12, 13, 23, 24, 40, 42, 43, 45, 52},33			out: [][2]int{{1, 3}, {5, 6}, {10, 13}, {23, 24}, {40, 40}, {42, 43}, {45, 45}, {52, 52}},34		},35	} {36		t.Run(name, func(t *testing.T) {37			match := matchedRanges(tt.in)38			if !reflect.DeepEqual(match, tt.out) {39				t.Errorf("expected %v, got %v", tt.out, match)40			}41		})42	}43}4445func TestByteToChar(t *testing.T) {46	stStr := "\x1b[90m\ue615\x1b[39m \x1b[3m\x1b[32mDow\x1b[0m\x1b[90m\x1b[39m\x1b[3wnloads"47	str := " Downloads"48	rng := [2]int{4, 7}49	expect := "Dow"5051	if got := str[rng[0]:rng[1]]; got != expect {52		t.Errorf("expected %q, got %q", expect, got)53	}5455	start, stop := bytePosToVisibleCharPos(str, rng)56	if got := ansi.Strip(ansi.Cut(stStr, start, stop)); got != expect {57		t.Errorf("expected %+q, got %+q", expect, got)58	}59}