github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/liner/width_test.go (about)

     1  package liner
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  )
     7  
     8  func accent(in []rune) []rune {
     9  	var out []rune
    10  	for _, r := range in {
    11  		out = append(out, r)
    12  		out = append(out, '\u0301')
    13  	}
    14  	return out
    15  }
    16  
    17  type testCase struct {
    18  	s      []rune
    19  	glyphs int
    20  }
    21  
    22  var testCases = []testCase{
    23  	{[]rune("query"), 5},
    24  	{[]rune("私"), 2},
    25  	{[]rune("hello世界"), 9},
    26  }
    27  
    28  func TestCountGlyphs(t *testing.T) {
    29  	for _, testCase := range testCases {
    30  		count := countGlyphs(testCase.s)
    31  		if count != testCase.glyphs {
    32  			t.Errorf("ASCII count incorrect. %d != %d", count, testCase.glyphs)
    33  		}
    34  		count = countGlyphs(accent(testCase.s))
    35  		if count != testCase.glyphs {
    36  			t.Errorf("Accent count incorrect. %d != %d", count, testCase.glyphs)
    37  		}
    38  	}
    39  }
    40  
    41  func compare(a, b []rune, name string, t *testing.T) {
    42  	if len(a) != len(b) {
    43  		t.Errorf(`"%s" != "%s" in %s"`, string(a), string(b), name)
    44  		return
    45  	}
    46  	for i := range a {
    47  		if a[i] != b[i] {
    48  			t.Errorf(`"%s" != "%s" in %s"`, string(a), string(b), name)
    49  			return
    50  		}
    51  	}
    52  }
    53  
    54  func TestPrefixGlyphs(t *testing.T) {
    55  	for _, testCase := range testCases {
    56  		for i := 0; i <= len(testCase.s); i++ {
    57  			iter := strconv.Itoa(i)
    58  			out := getPrefixGlyphs(testCase.s, i)
    59  			compare(out, testCase.s[:i], "ascii prefix "+iter, t)
    60  			out = getPrefixGlyphs(accent(testCase.s), i)
    61  			compare(out, accent(testCase.s[:i]), "accent prefix "+iter, t)
    62  		}
    63  		out := getPrefixGlyphs(testCase.s, 999)
    64  		compare(out, testCase.s, "ascii prefix overflow", t)
    65  		out = getPrefixGlyphs(accent(testCase.s), 999)
    66  		compare(out, accent(testCase.s), "accent prefix overflow", t)
    67  
    68  		out = getPrefixGlyphs(testCase.s, -3)
    69  		if len(out) != 0 {
    70  			t.Error("ascii prefix negative")
    71  		}
    72  		out = getPrefixGlyphs(accent(testCase.s), -3)
    73  		if len(out) != 0 {
    74  			t.Error("accent prefix negative")
    75  		}
    76  	}
    77  }
    78  
    79  func TestSuffixGlyphs(t *testing.T) {
    80  	for _, testCase := range testCases {
    81  		for i := 0; i <= len(testCase.s); i++ {
    82  			iter := strconv.Itoa(i)
    83  			out := getSuffixGlyphs(testCase.s, i)
    84  			compare(out, testCase.s[len(testCase.s)-i:], "ascii suffix "+iter, t)
    85  			out = getSuffixGlyphs(accent(testCase.s), i)
    86  			compare(out, accent(testCase.s[len(testCase.s)-i:]), "accent suffix "+iter, t)
    87  		}
    88  		out := getSuffixGlyphs(testCase.s, 999)
    89  		compare(out, testCase.s, "ascii suffix overflow", t)
    90  		out = getSuffixGlyphs(accent(testCase.s), 999)
    91  		compare(out, accent(testCase.s), "accent suffix overflow", t)
    92  
    93  		out = getSuffixGlyphs(testCase.s, -3)
    94  		if len(out) != 0 {
    95  			t.Error("ascii suffix negative")
    96  		}
    97  		out = getSuffixGlyphs(accent(testCase.s), -3)
    98  		if len(out) != 0 {
    99  			t.Error("accent suffix negative")
   100  		}
   101  	}
   102  }