github.com/jmigpin/editor@v1.6.0/core/inlinecomplete_test.go (about)

     1  package core
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jmigpin/editor/util/iout/iorw"
     7  )
     8  
     9  func TestReadLastUntilStart(t *testing.T) {
    10  	type in struct {
    11  		str   string
    12  		index int
    13  	}
    14  	type out struct {
    15  		start int
    16  		str   string
    17  		ok    bool
    18  	}
    19  	type result struct {
    20  		in  in
    21  		out out
    22  	}
    23  	w := []result{
    24  		{in{"aa)bbb", 6}, out{3, "bbb", true}},
    25  		{in{"abc", 3}, out{0, "abc", true}},
    26  		{in{"", 0}, out{0, "", false}},
    27  		{in{"  ", 2}, out{0, "", false}},
    28  		{in{".", 1}, out{0, "", false}},
    29  	}
    30  	for _, u := range w {
    31  		rw := iorw.NewStringReaderAt(u.in.str)
    32  		start, str, ok := readLastUntilStart(rw, u.in.index)
    33  		if !(start == u.out.start && str == u.out.str && ok == u.out.ok) {
    34  			t.Fatal(start, str, ok, "expecting", u.out)
    35  		}
    36  	}
    37  }
    38  
    39  func TestFilterPrefixedAndExpand(t *testing.T) {
    40  	type in struct {
    41  		comps  []string
    42  		prefix string
    43  	}
    44  	type out struct {
    45  		expand      int
    46  		canComplete bool
    47  		comps       []string
    48  	}
    49  	type result struct {
    50  		in  in
    51  		out out
    52  	}
    53  	w := []result{
    54  		{
    55  			in{[]string{"aaa", "aaabbb"}, "aa"},
    56  			out{1, true, []string{"aaa", "aaabbb"}},
    57  		},
    58  		{
    59  			in{[]string{"aaa", "aAa"}, "aa"},
    60  			out{0, false, []string{"aaa", "aAa"}},
    61  		},
    62  		{
    63  			in{[]string{"aaa", "aAa"}, "aaa"},
    64  			out{0, false, []string{"aaa", "aAa"}},
    65  		},
    66  		{
    67  			in{[]string{"aAa"}, "aaa"},
    68  			out{0, true, []string{"aAa"}},
    69  		},
    70  		{
    71  			in{[]string{"aaabbbCCCe"}, "aaabbbC"},
    72  			out{3, true, []string{"aaabbbCCCe"}},
    73  		},
    74  	}
    75  	for _, u := range w {
    76  		expand, canComplete, comps := filterPrefixedAndExpand(u.in.comps, u.in.prefix)
    77  		if !(expand == u.out.expand &&
    78  			canComplete == u.out.canComplete &&
    79  			cmpStrSlices(comps, u.out.comps)) {
    80  			t.Fatal(expand, canComplete, comps, "expecting", u.out)
    81  		}
    82  	}
    83  }
    84  
    85  func TestInsertComplete(t *testing.T) {
    86  	type in struct {
    87  		comps []string
    88  		text  string
    89  		index int
    90  	}
    91  	type out struct {
    92  		newIndex  int
    93  		completed bool
    94  		comps     []string
    95  		text      string
    96  	}
    97  	type result struct {
    98  		in  in
    99  		out out
   100  	}
   101  	w := []result{
   102  		{
   103  			in{[]string{"aaa", "aaabbb"}, "aa", 2},
   104  			out{3, true, []string{"aaa", "aaabbb"}, "aaa"},
   105  		},
   106  		{
   107  			in{[]string{"aAa", "aaa"}, "aa", 2},
   108  			out{0, false, []string{"aAa", "aaa"}, "aa"},
   109  		},
   110  		{
   111  			in{[]string{"aAac"}, "aaac", 4},
   112  			out{4, true, []string{"aAac"}, "aAac"},
   113  		},
   114  		{
   115  			in{[]string{"aa"}, "aa", 2},
   116  			out{0, false, []string{"aa"}, "aa"},
   117  		},
   118  		{
   119  			in{[]string{"abc"}, "abc", 1},
   120  			out{3, true, []string{"abc"}, "abc"},
   121  		},
   122  		{
   123  			in{[]string{"u", "abcd", "abc"}, "a", 1},
   124  			out{3, true, []string{"abcd", "abc"}, "abc"},
   125  		},
   126  		{
   127  			in{[]string{"abcd", "abc"}, "abe", 1},
   128  			out{3, true, []string{"abcd", "abc"}, "abce"},
   129  		},
   130  		{
   131  			in{[]string{"aaBbbCcc"}, "aaCc", 4},
   132  			out{0, false, nil, "aaCc"},
   133  		},
   134  	}
   135  	for _, u := range w {
   136  		rw := iorw.NewBytesReadWriterAt([]byte(u.in.text))
   137  		newIndex, completed, comps, _ := insertComplete(u.in.comps, rw, u.in.index)
   138  		b, err := iorw.ReadFastFull(rw)
   139  		if err != nil {
   140  			t.Fatal(err)
   141  		}
   142  		text := string(b)
   143  		if !(cmpStrSlices(comps, u.out.comps) &&
   144  			text == u.out.text &&
   145  			newIndex == u.out.newIndex &&
   146  			completed == u.out.completed) {
   147  			t.Fatal(newIndex, completed, comps, text, "expecting", u.out)
   148  		}
   149  	}
   150  }
   151  
   152  //----------
   153  
   154  func cmpStrSlices(a, b []string) bool {
   155  	if len(a) != len(b) {
   156  		return false
   157  	}
   158  	for i, s := range a {
   159  		if s != b[i] {
   160  			return false
   161  		}
   162  	}
   163  	return true
   164  }