github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/util/strings_test.go (about)

     1  package util
     2  
     3  import "testing"
     4  
     5  var findContextTests = []struct {
     6  	text          string
     7  	pos           int
     8  	lineno, colno int
     9  	line          string
    10  }{
    11  	{"a\nb", 2, 1, 0, "b"},
    12  }
    13  
    14  func TestFindContext(t *testing.T) {
    15  	for _, tt := range findContextTests {
    16  		lineno, colno, line := FindContext(tt.text, tt.pos)
    17  		if lineno != tt.lineno || colno != tt.colno || line != tt.line {
    18  			t.Errorf("FindContext(%v, %v) => (%v, %v, %v), want (%v, %v, %v)",
    19  				tt.text, tt.pos, lineno, colno, line, tt.lineno, tt.colno, tt.line)
    20  		}
    21  	}
    22  }
    23  
    24  var SubstringByRuneTests = []struct {
    25  	s         string
    26  	low, high int
    27  	wantedStr string
    28  	wantedErr error
    29  }{
    30  	{"Hello world", 1, 4, "ell", nil},
    31  	{"你好世界", 0, 0, "", nil},
    32  	{"你好世界", 1, 1, "", nil},
    33  	{"你好世界", 1, 2, "好", nil},
    34  	{"你好世界", 1, 4, "好世界", nil},
    35  	{"你好世界", -1, -1, "", ErrIndexOutOfRange},
    36  	{"你好世界", 0, 5, "", ErrIndexOutOfRange},
    37  	{"你好世界", 5, 5, "", ErrIndexOutOfRange},
    38  }
    39  
    40  func TestSubstringByRune(t *testing.T) {
    41  	for _, tt := range SubstringByRuneTests {
    42  		s, e := SubstringByRune(tt.s, tt.low, tt.high)
    43  		if s != tt.wantedStr || e != tt.wantedErr {
    44  			t.Errorf("SubstringByRune(%q, %v, %d) => (%q, %v), want (%q, %v)",
    45  				tt.s, tt.low, tt.high, s, e, tt.wantedStr, tt.wantedErr)
    46  		}
    47  	}
    48  }
    49  
    50  var NthRuneTests = []struct {
    51  	s          string
    52  	n          int
    53  	wantedRune rune
    54  	wantedErr  error
    55  }{
    56  	{"你好世界", -1, 0, ErrIndexOutOfRange},
    57  	{"你好世界", 0, '你', nil},
    58  	{"你好世界", 4, 0, ErrIndexOutOfRange},
    59  }
    60  
    61  func TestNthRune(t *testing.T) {
    62  	for _, tt := range NthRuneTests {
    63  		r, e := NthRune(tt.s, tt.n)
    64  		if r != tt.wantedRune || e != tt.wantedErr {
    65  			t.Errorf("NthRune(%q, %v) => (%q, %v), want (%q, %v)",
    66  				tt.s, tt.n, r, e, tt.wantedRune, tt.wantedErr)
    67  		}
    68  	}
    69  }
    70  
    71  var EOLSOLTests = []struct {
    72  	s                         string
    73  	wantFirstEOL, wantLastSOL int
    74  }{
    75  	{"0", 1, 0},
    76  	{"\n12", 0, 1},
    77  	{"01\n", 2, 3},
    78  	{"01\n34", 2, 3},
    79  }
    80  
    81  func TestEOLSOL(t *testing.T) {
    82  	for _, tc := range EOLSOLTests {
    83  		eol := FindFirstEOL(tc.s)
    84  		if eol != tc.wantFirstEOL {
    85  			t.Errorf("FindFirstEOL(%q) => %d, want %d", tc.s, eol, tc.wantFirstEOL)
    86  		}
    87  		sol := FindLastSOL(tc.s)
    88  		if sol != tc.wantLastSOL {
    89  			t.Errorf("FindLastSOL(%q) => %d, want %d", tc.s, sol, tc.wantLastSOL)
    90  		}
    91  	}
    92  }
    93  
    94  var MatchSubseqTests = []struct {
    95  	s, p string
    96  	want bool
    97  }{
    98  	{"elvish", "e", true},
    99  	{"elvish", "elh", true},
   100  	{"elvish", "sh", true},
   101  	{"elves/elvish", "l/e", true},
   102  	{"elves/elvish", "e/e", true},
   103  	{"elvish", "le", false},
   104  	{"elvish", "evii", false},
   105  }
   106  
   107  func TestMatchSubseq(t *testing.T) {
   108  	for _, tc := range MatchSubseqTests {
   109  		b := MatchSubseq(tc.s, tc.p)
   110  		if b != tc.want {
   111  			t.Errorf("MatchSubseq(%q, %q) -> %v, want %v", tc.s, tc.p, b, tc.want)
   112  		}
   113  	}
   114  }