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

     1  package match
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestMatch(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	// Change to tests directory for testing completion of
    13  	// files and directories
    14  	err := os.Chdir("../tests")
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	type matcherTest struct {
    20  		prefix string
    21  		want   bool
    22  	}
    23  
    24  	tests := []struct {
    25  		m     Match
    26  		long  string
    27  		tests []matcherTest
    28  	}{
    29  		{
    30  			m:    Prefix,
    31  			long: "abcd",
    32  			tests: []matcherTest{
    33  				{prefix: "", want: true},
    34  				{prefix: "ab", want: true},
    35  				{prefix: "ac", want: false},
    36  			},
    37  		},
    38  		{
    39  			m:    Prefix,
    40  			long: "",
    41  			tests: []matcherTest{
    42  				{prefix: "ac", want: false},
    43  				{prefix: "", want: true},
    44  			},
    45  		},
    46  		{
    47  			m:    File,
    48  			long: "file.txt",
    49  			tests: []matcherTest{
    50  				{prefix: "", want: true},
    51  				{prefix: "f", want: true},
    52  				{prefix: "./f", want: true},
    53  				{prefix: "./.", want: false},
    54  				{prefix: "file.", want: true},
    55  				{prefix: "./file.", want: true},
    56  				{prefix: "file.txt", want: true},
    57  				{prefix: "./file.txt", want: true},
    58  				{prefix: "other.txt", want: false},
    59  				{prefix: "/other.txt", want: false},
    60  				{prefix: "/file.txt", want: false},
    61  				{prefix: "/fil", want: false},
    62  				{prefix: "/file.txt2", want: false},
    63  				{prefix: "/.", want: false},
    64  			},
    65  		},
    66  		{
    67  			m:    File,
    68  			long: "./file.txt",
    69  			tests: []matcherTest{
    70  				{prefix: "", want: true},
    71  				{prefix: "f", want: true},
    72  				{prefix: "./f", want: true},
    73  				{prefix: "./.", want: false},
    74  				{prefix: "file.", want: true},
    75  				{prefix: "./file.", want: true},
    76  				{prefix: "file.txt", want: true},
    77  				{prefix: "./file.txt", want: true},
    78  				{prefix: "other.txt", want: false},
    79  				{prefix: "/other.txt", want: false},
    80  				{prefix: "/file.txt", want: false},
    81  				{prefix: "/fil", want: false},
    82  				{prefix: "/file.txt2", want: false},
    83  				{prefix: "/.", want: false},
    84  			},
    85  		},
    86  		{
    87  			m:    File,
    88  			long: "/file.txt",
    89  			tests: []matcherTest{
    90  				{prefix: "", want: true},
    91  				{prefix: "f", want: false},
    92  				{prefix: "./f", want: false},
    93  				{prefix: "./.", want: false},
    94  				{prefix: "file.", want: false},
    95  				{prefix: "./file.", want: false},
    96  				{prefix: "file.txt", want: false},
    97  				{prefix: "./file.txt", want: false},
    98  				{prefix: "other.txt", want: false},
    99  				{prefix: "/other.txt", want: false},
   100  				{prefix: "/file.txt", want: true},
   101  				{prefix: "/fil", want: true},
   102  				{prefix: "/file.txt2", want: false},
   103  				{prefix: "/.", want: false},
   104  			},
   105  		},
   106  		{
   107  			m:    File,
   108  			long: "./",
   109  			tests: []matcherTest{
   110  				{prefix: "", want: true},
   111  				{prefix: ".", want: true},
   112  				{prefix: "./", want: true},
   113  				{prefix: "./.", want: false},
   114  			},
   115  		},
   116  	}
   117  
   118  	for _, tt := range tests {
   119  		for _, ttt := range tt.tests {
   120  			name := fmt.Sprintf("matcher=%T&long='%s'&prefix='%s'", tt.m, tt.long, ttt.prefix)
   121  			t.Run(name, func(t *testing.T) {
   122  				got := tt.m(tt.long, ttt.prefix)
   123  				if got != ttt.want {
   124  					t.Errorf("Failed %s: got = %t, want: %t", name, got, ttt.want)
   125  				}
   126  			})
   127  		}
   128  	}
   129  }