Skip to content
Myr

util_test.go

This file

Path

src/util/util_test.go

171 lines · 3.7 KiB · Go

Last changed by

Fix --gap-line cutting a grapheme clusterJunegunn Choibefore adoption961793c

History of this file

Take it

1package util23import (4	"math"5	"strings"6	"testing"7)89func TestConstrain(t *testing.T) {10	if Constrain(-3, -1, 3) != -1 {11		t.Error("Expected", -1)12	}13	if Constrain(2, -1, 3) != 2 {14		t.Error("Expected", 2)15	}1617	if Constrain(5, -1, 3) != 3 {18		t.Error("Expected", 3)19	}20}2122func TestAsUint16(t *testing.T) {23	if AsUint16(5) != 5 {24		t.Error("Expected", 5)25	}26	if AsUint16(-10) != 0 {27		t.Error("Expected", 0)28	}29	if AsUint16(math.MaxUint16) != math.MaxUint16 {30		t.Error("Expected", math.MaxUint16)31	}32	if AsUint16(math.MinInt32) != 0 {33		t.Error("Expected", 0)34	}35	if AsUint16(math.MinInt16) != 0 {36		t.Error("Expected", 0)37	}38	if AsUint16(math.MaxUint16+1) != math.MaxUint16 {39		t.Error("Expected", math.MaxUint16)40	}41}4243func TestOnce(t *testing.T) {44	o := Once(false)45	if o() {46		t.Error("Expected: false")47	}48	if !o() {49		t.Error("Expected: true")50	}51	if !o() {52		t.Error("Expected: true")53	}5455	o = Once(true)56	if !o() {57		t.Error("Expected: true")58	}59	if o() {60		t.Error("Expected: false")61	}62	if o() {63		t.Error("Expected: false")64	}65}6667func TestRunesWidth(t *testing.T) {68	for _, args := range [][]int{69		{100, 5, -1},70		{3, 4, 3},71		{0, 1, 0},72	} {73		width, overflowIdx := RunesWidth([]rune("hello"), 0, 0, args[0])74		if width != args[1] {75			t.Errorf("Expected width: %d, actual: %d", args[1], width)76		}77		if overflowIdx != args[2] {78			t.Errorf("Expected overflow index: %d, actual: %d", args[2], overflowIdx)79		}80	}81	for _, input := range []struct {82		s string83		w int84	}{85		{"▶", 1},86		{"▶️", 2},87	} {88		width, _ := RunesWidth([]rune(input.s), 0, 0, 100)89		if width != input.w {90			t.Errorf("Expected width of %s: %d, actual: %d", input.s, input.w, width)91		}92	}93}9495func TestTruncate(t *testing.T) {96	truncated, width := Truncate("가나다라마", 7)97	if string(truncated) != "가나다" {98		t.Errorf("Expected: 가나다, actual: %s", string(truncated))99	}100	if width != 6 {101		t.Errorf("Expected: 6, actual: %d", width)102	}103}104105func TestRepeatToFill(t *testing.T) {106	if RepeatToFill("abcde", 10, 50) != strings.Repeat("abcde", 5) {107		t.Error("Expected:", strings.Repeat("abcde", 5))108	}109	if RepeatToFill("abcde", 10, 42) != strings.Repeat("abcde", 4)+"abcde"[:2] {110		t.Error("Expected:", strings.Repeat("abcde", 4)+"abcde"[:2])111	}112113	// Should not cut a grapheme cluster in half114	for _, test := range []struct {115		str      string116		limit    int117		expected string118	}{119		{"a\u0301b", 1, "a\u0301"},120		{"a\u0301b", 3, "a\u0301ba\u0301"},121		{"a\u0301b", 4, "a\u0301ba\u0301b"},122		{"a\u4e00", 2, "a"},123		{"a\u4e00", 4, "a\u4e00a"},124		{"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 1, "-"},125		{"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 2, "-"},126		{"-\U0001f468\u200d\U0001f469\u200d\U0001f467", 4, "-\U0001f468\u200d\U0001f469\u200d\U0001f467-"},127	} {128		actual := RepeatToFill(test.str, StringWidth(test.str), test.limit)129		if actual != test.expected {130			t.Errorf("Expected: %q, actual: %q", test.expected, actual)131		}132	}133}134135func TestStringWidth(t *testing.T) {136	w := StringWidth("─")137	if w != 1 {138		t.Errorf("Expected: %d, Actual: %d", 1, w)139	}140}141142func TestCompareVersions(t *testing.T) {143	assert := func(a, b string, expected int) {144		if result := CompareVersions(a, b); result != expected {145			t.Errorf("Expected: %d, Actual: %d", expected, result)146		}147	}148149	assert("2", "1", 1)150	assert("2", "2", 0)151	assert("2", "10", -1)152153	assert("2.1", "2.2", -1)154	assert("2.1", "2.1.1", -1)155156	assert("1.2.3", "1.2.2", 1)157	assert("1.2.3", "1.2.3", 0)158	assert("1.2.3", "1.2.3.0", 0)159	assert("1.2.3", "1.2.4", -1)160161	// Different number of parts162	assert("1.0.0", "1", 0)163	assert("1.0.0", "1.0", 0)164	assert("1.0.0", "1.0.0", 0)165	assert("1.0", "1.0.0", 0)166	assert("1", "1.0.0", 0)167	assert("1.0.0", "1.0.0.1", -1)168	assert("1.0.0.1.0", "1.0.0.1", 0)169170	assert("", "3.4.5", -1)171}