github.com/chriswalz/complete@v1.1.2/args_test.go (about)

     1  package complete
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestArgs(t *testing.T) {
    10  	t.Parallel()
    11  	tests := []struct {
    12  		line          string
    13  		completed     string
    14  		last          string
    15  		lastCompleted string
    16  	}{
    17  		{
    18  			line:          "a b c",
    19  			completed:     "b",
    20  			last:          "c",
    21  			lastCompleted: "b",
    22  		},
    23  		{
    24  			line:          "a b ",
    25  			completed:     "b",
    26  			last:          "",
    27  			lastCompleted: "b",
    28  		},
    29  		{
    30  			line:          "",
    31  			completed:     "",
    32  			last:          "",
    33  			lastCompleted: "",
    34  		},
    35  		{
    36  			line:          "a",
    37  			completed:     "",
    38  			last:          "a",
    39  			lastCompleted: "",
    40  		},
    41  		{
    42  			line:          "a ",
    43  			completed:     "",
    44  			last:          "",
    45  			lastCompleted: "",
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		t.Run(tt.line, func(t *testing.T) {
    51  
    52  			a := newArgs(tt.line)
    53  
    54  			if got, want := strings.Join(a.Completed, " "), tt.completed; got != want {
    55  				t.Errorf("%s failed: Completed = %q, want %q", t.Name(), got, want)
    56  			}
    57  			if got, want := a.Last, tt.last; got != want {
    58  				t.Errorf("Last = %q, want %q", got, want)
    59  			}
    60  			if got, want := a.LastCompleted, tt.lastCompleted; got != want {
    61  				t.Errorf("%s failed: LastCompleted = %q, want %q", t.Name(), got, want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestArgs_From(t *testing.T) {
    68  	t.Parallel()
    69  	tests := []struct {
    70  		line         string
    71  		from         int
    72  		newLine      string
    73  		newCompleted string
    74  	}{
    75  		{
    76  			line:         "a b c",
    77  			from:         0,
    78  			newLine:      "b c",
    79  			newCompleted: "b",
    80  		},
    81  		{
    82  			line:         "a b c",
    83  			from:         1,
    84  			newLine:      "c",
    85  			newCompleted: "",
    86  		},
    87  		{
    88  			line:         "a b c",
    89  			from:         2,
    90  			newLine:      "",
    91  			newCompleted: "",
    92  		},
    93  		{
    94  			line:         "a b c",
    95  			from:         3,
    96  			newLine:      "",
    97  			newCompleted: "",
    98  		},
    99  		{
   100  			line:         "a b c ",
   101  			from:         0,
   102  			newLine:      "b c ",
   103  			newCompleted: "b c",
   104  		},
   105  		{
   106  			line:         "a b c ",
   107  			from:         1,
   108  			newLine:      "c ",
   109  			newCompleted: "c",
   110  		},
   111  		{
   112  			line:         "a b c ",
   113  			from:         2,
   114  			newLine:      "",
   115  			newCompleted: "",
   116  		},
   117  		{
   118  			line:         "",
   119  			from:         0,
   120  			newLine:      "",
   121  			newCompleted: "",
   122  		},
   123  		{
   124  			line:         "",
   125  			from:         1,
   126  			newLine:      "",
   127  			newCompleted: "",
   128  		},
   129  	}
   130  
   131  	for _, tt := range tests {
   132  		t.Run(fmt.Sprintf("%s/%d", tt.line, tt.from), func(t *testing.T) {
   133  
   134  			a := newArgs(tt.line)
   135  			n := a.from(tt.from)
   136  
   137  			if got, want := strings.Join(n.All, " "), tt.newLine; got != want {
   138  				t.Errorf("%s failed: all = %q, want %q", t.Name(), got, want)
   139  			}
   140  			if got, want := strings.Join(n.Completed, " "), tt.newCompleted; got != want {
   141  				t.Errorf("%s failed: completed = %q, want %q", t.Name(), got, want)
   142  			}
   143  		})
   144  	}
   145  }
   146  
   147  func TestArgs_Directory(t *testing.T) {
   148  	t.Parallel()
   149  	initTests()
   150  
   151  	tests := []struct {
   152  		line      string
   153  		directory string
   154  	}{
   155  		{
   156  			line:      "a b c",
   157  			directory: "./",
   158  		},
   159  		{
   160  			line:      "a b c /tm",
   161  			directory: "/",
   162  		},
   163  		{
   164  			line:      "a b c /tmp",
   165  			directory: "/tmp/",
   166  		},
   167  		{
   168  			line:      "a b c /tmp ",
   169  			directory: "./",
   170  		},
   171  		{
   172  			line:      "a b c ./",
   173  			directory: "./",
   174  		},
   175  		{
   176  			line:      "a b c ./dir",
   177  			directory: "./dir/",
   178  		},
   179  		{
   180  			line:      "a b c dir",
   181  			directory: "dir/",
   182  		},
   183  		{
   184  			line:      "a b c ./di",
   185  			directory: "./",
   186  		},
   187  		{
   188  			line:      "a b c ./dir ",
   189  			directory: "./",
   190  		},
   191  		{
   192  			line:      "a b c ./di",
   193  			directory: "./",
   194  		},
   195  		{
   196  			line:      "a b c ./a.txt",
   197  			directory: "./",
   198  		},
   199  		{
   200  			line:      "a b c ./a.txt/x",
   201  			directory: "./",
   202  		},
   203  	}
   204  
   205  	for _, tt := range tests {
   206  		t.Run(tt.line, func(t *testing.T) {
   207  
   208  			a := newArgs(tt.line)
   209  
   210  			if got, want := a.Directory(), tt.directory; got != want {
   211  				t.Errorf("%s failed: directory = %q, want %q", t.Name(), got, want)
   212  			}
   213  		})
   214  	}
   215  }