github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/cmds/core/elvish/util/wcwidth_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  var wcwidthTests = []struct {
     8  	in     rune
     9  	wanted int
    10  }{
    11  	{'\u0301', 0}, // Combining acute accent
    12  	{'a', 1},
    13  	{'Ω', 1},
    14  	{'好', 2},
    15  	{'か', 2},
    16  }
    17  
    18  func TestWcwidth(t *testing.T) {
    19  	for _, tt := range wcwidthTests {
    20  		out := Wcwidth(tt.in)
    21  		if out != tt.wanted {
    22  			t.Errorf("wcwidth(%q) => %v, want %v", tt.in, out, tt.wanted)
    23  		}
    24  	}
    25  }
    26  
    27  func TestOverrideWcwidth(t *testing.T) {
    28  	r := '❱'
    29  	oldw := Wcwidth(r)
    30  	w := oldw + 1
    31  
    32  	OverrideWcwidth(r, w)
    33  	if Wcwidth(r) != w {
    34  		t.Errorf("Wcwidth(%q) != %d after OverrideWcwidth", r, w)
    35  	}
    36  	UnoverrideWcwidth(r)
    37  	if Wcwidth(r) != oldw {
    38  		t.Errorf("Wcwidth(%q) != %d after UnoverrideWcwidth", r, oldw)
    39  	}
    40  }
    41  
    42  func TestTrimWcwidth(t *testing.T) {
    43  	if TrimWcwidth("abc", 2) != "ab" {
    44  		t.Errorf("TrimWcwidth #1 fails")
    45  	}
    46  	if TrimWcwidth("你好", 3) != "你" {
    47  		t.Errorf("TrimWcwidth #2 fails")
    48  	}
    49  }
    50  
    51  func TestForceWcwidth(t *testing.T) {
    52  	for i, c := range []struct {
    53  		s    string
    54  		w    int
    55  		want string
    56  	}{
    57  		// Triming
    58  		{"abc", 2, "ab"},
    59  		{"你好", 2, "你"},
    60  		// Padding
    61  		{"abc", 4, "abc "},
    62  		{"你好", 5, "你好 "},
    63  		// Trimming and Padding
    64  		{"你好", 3, "你 "},
    65  	} {
    66  		if got := ForceWcwidth(c.s, c.w); got != c.want {
    67  			t.Errorf("ForceWcwidth #%d fails", i)
    68  		}
    69  	}
    70  }
    71  
    72  func TestTrimEachLineWcwidth(t *testing.T) {
    73  	if TrimEachLineWcwidth("abcdefg\n你好", 3) != "abc\n你" {
    74  		t.Errorf("TestTrimEachLineWcwidth fails")
    75  	}
    76  }