github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/format_test.go (about)

     1  package autocomplete
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/test/count"
     7  )
     8  
     9  var (
    10  	testData = []string{
    11  		"foobar",
    12  		"foobar ",
    13  		"foobar:",
    14  		"foobar: ",
    15  		"foo bar",
    16  		"",
    17  		"1",
    18  		"123",
    19  		"(",
    20  		")",
    21  		"{",
    22  		"}",
    23  		"=",
    24  		"/",
    25  		"'",
    26  		`"`,
    27  		";",
    28  		"|",
    29  		"?",
    30  		"->",
    31  		"#",
    32  
    33  		"\\foobar",
    34  		"\\foobar ",
    35  		"foobar\\:",
    36  		"foobar:\\ ",
    37  		"foo\\ bar",
    38  		"\\1",
    39  		"1\\23",
    40  		"\\(",
    41  		"\\)",
    42  		"\\{",
    43  		"\\}",
    44  		"\\=",
    45  		"\\/",
    46  		"\\'",
    47  		`\"`,
    48  		"\\;",
    49  		"\\|",
    50  		"\\?",
    51  		"\\->",
    52  		"-\\>",
    53  		"\\-\\>",
    54  		"\\#",
    55  
    56  		`"foobar"`,
    57  		`"foobar "`,
    58  		`"foobar:"`,
    59  		`"foobar: "`,
    60  		`"foo bar"`,
    61  		`"foo bar "`,
    62  		`"1"`,
    63  		`"1 "`,
    64  		`"123"`,
    65  		`"123 "`,
    66  		`"("`,
    67  		`")"`,
    68  		`"{"`,
    69  		`"}"`,
    70  		`"="`,
    71  		`"/"`,
    72  		`"'"`,
    73  		`";"`,
    74  		`"|"`,
    75  		`"?"`,
    76  		`"->"`,
    77  		`"#"`,
    78  
    79  		`'foobar'`,
    80  		`'foobar '`,
    81  		`'foobar:'`,
    82  		`'foobar: '`,
    83  		`'foo bar'`,
    84  		`'foo bar '`,
    85  		`'1'`,
    86  		`'1 '`,
    87  		`'123'`,
    88  		`'123 '`,
    89  		`'('`,
    90  		`')'`,
    91  		`'{'`,
    92  		`'}'`,
    93  		`'='`,
    94  		`'/'`,
    95  		`'"'`,
    96  		`';'`,
    97  		`'|'`,
    98  		`'?'`,
    99  		`'->'`,
   100  		`'#'`,
   101  
   102  		`(foobar)`,
   103  		`(foobar )`,
   104  		`(foobar:)`,
   105  		`(foobar: )`,
   106  		`(foo bar)`,
   107  		`(foo bar )`,
   108  		`(1)`,
   109  		`(1 )`,
   110  		`(123)`,
   111  		`(123 )`,
   112  		`(()`,
   113  		`())`,
   114  		`({)`,
   115  		`(})`,
   116  		`(=)`,
   117  		`(/)`,
   118  		`(')`,
   119  		`(")`,
   120  		`(;)`,
   121  		`(|)`,
   122  		`(?)`,
   123  		`(->)`,
   124  		`(#)`,
   125  	}
   126  
   127  	expected = []string{
   128  		"foobar ",
   129  		"foobar\\ ",
   130  		"foobar: ",
   131  		"foobar:\\ ",
   132  		"foo\\ bar ",
   133  		" ",
   134  		"1 ",
   135  		"123 ",
   136  		"\\( ",
   137  		"\\) ",
   138  		"\\{ ",
   139  		"\\} ",
   140  		"=",
   141  		"/",
   142  		"\\' ",
   143  		`\" `,
   144  		"\\; ",
   145  		"\\| ",
   146  		"\\? ",
   147  		"-\\> ",
   148  		"\\# ",
   149  
   150  		"\\\\foobar ",
   151  		"\\\\foobar\\ ",
   152  		"foobar\\\\: ",
   153  		"foobar:\\\\\\ ",
   154  		"foo\\\\\\ bar ",
   155  		"\\\\1 ",
   156  		"1\\\\23 ",
   157  		"\\\\\\( ",
   158  		"\\\\\\) ",
   159  		"\\\\\\{ ",
   160  		"\\\\\\} ",
   161  		"\\\\=",
   162  		"\\\\/",
   163  		"\\\\\\' ",
   164  		`\\\" `,
   165  		"\\\\\\; ",
   166  		"\\\\\\| ",
   167  		"\\\\\\? ",
   168  		"\\\\-\\> ",
   169  		"-\\\\> ",
   170  		"\\\\-\\\\> ",
   171  		"\\\\\\# ",
   172  
   173  		`\"foobar\" `,
   174  		`\"foobar\ \" `,
   175  		`\"foobar:\" `,
   176  		`\"foobar:\ \" `,
   177  		`\"foo\ bar\" `,
   178  		`\"foo\ bar\ \" `,
   179  		`\"1\" `,
   180  		`\"1\ \" `,
   181  		`\"123\" `,
   182  		`\"123\ \" `,
   183  		`\"\(\" `,
   184  		`\"\)\" `,
   185  		`\"\{\" `,
   186  		`\"\}\" `,
   187  		`\"=\" `,
   188  		`\"/\" `,
   189  		`\"\'\" `,
   190  		`\"\;\" `,
   191  		`\"\|\" `,
   192  		`\"\?\" `,
   193  		`\"-\>\" `,
   194  		`\"\#\" `,
   195  
   196  		`\'foobar\' `,
   197  		`\'foobar\ \' `,
   198  		`\'foobar:\' `,
   199  		`\'foobar:\ \' `,
   200  		`\'foo\ bar\' `,
   201  		`\'foo\ bar\ \' `,
   202  		`\'1\' `,
   203  		`\'1\ \' `,
   204  		`\'123\' `,
   205  		`\'123\ \' `,
   206  		`\'\(\' `,
   207  		`\'\)\' `,
   208  		`\'\{\' `,
   209  		`\'\}\' `,
   210  		`\'=\' `,
   211  		`\'/\' `,
   212  		`\'\"\' `,
   213  		`\'\;\' `,
   214  		`\'\|\' `,
   215  		`\'\?\' `,
   216  		`\'-\>\' `,
   217  		`\'\#\' `,
   218  
   219  		`\(foobar\) `,
   220  		`\(foobar\ \) `,
   221  		`\(foobar:\) `,
   222  		`\(foobar:\ \) `,
   223  		`\(foo\ bar\) `,
   224  		`\(foo\ bar\ \) `,
   225  		`\(1\) `,
   226  		`\(1\ \) `,
   227  		`\(123\) `,
   228  		`\(123\ \) `,
   229  		`\(\(\) `,
   230  		`\(\)\) `,
   231  		`\(\{\) `,
   232  		`\(\}\) `,
   233  		`\(=\) `,
   234  		`\(/\) `,
   235  		`\(\'\) `,
   236  		`\(\"\) `,
   237  		`\(\;\) `,
   238  		`\(\|\) `,
   239  		`\(\?\) `,
   240  		`\(-\>\) `,
   241  		`\(\#\) `,
   242  	}
   243  )
   244  
   245  // Tests the order is preserved and that items can be added the map without
   246  // breaking the function
   247  func TestFormatSuggestionsOrder(t *testing.T) {
   248  	/*formatSuggestions := func(act *AutoCompleteT) {
   249  		//sortCompletions(act.Items)
   250  		formatSuggestionsArray(act.ParsedTokens, act.Items)
   251  		formatSuggestionsMap(act.ParsedTokens, &act.Definitions)
   252  	}*/
   253  
   254  	count.Tests(t, len(testData)*3)
   255  
   256  	tests := make([]string, len(testData))
   257  	copy(tests, testData)
   258  	original := make([]string, len(tests))
   259  	copy(original, tests)
   260  
   261  	act1 := AutoCompleteT{
   262  		Items:       tests,
   263  		Definitions: make(map[string]string),
   264  	}
   265  
   266  	FormatSuggestions(&act1)
   267  
   268  	for i := range tests {
   269  		if tests[i] != expected[i] {
   270  			t.Error("formatSuggestionsArray mismatch:")
   271  			t.Logf("  Original: '%s'", original[i])
   272  			t.Logf("  Expected: '%s'", expected[i])
   273  			t.Logf("  Actual:   '%s'", tests[i])
   274  			t.Logf("  Index:     %d", i)
   275  		}
   276  
   277  		act2 := AutoCompleteT{
   278  			Definitions: map[string]string{original[i]: "some data"},
   279  		}
   280  
   281  		FormatSuggestions(&act2)
   282  
   283  		if len(act2.Definitions) != 1 {
   284  			t.Errorf("Invalid test length in formatSuggestionsMap:")
   285  			t.Logf("  Expected:  %d", 1)
   286  			t.Logf("  Actual:    %d", len(act2.Definitions))
   287  			t.Logf("  Index:     %d", i)
   288  		}
   289  		for key := range act2.Definitions {
   290  			if key != expected[i] {
   291  				t.Error("formatSuggestionsMap mismatch:")
   292  				t.Logf("  Original: '%s'", original[i])
   293  				t.Logf("  Expected: '%s'", expected[i])
   294  				t.Logf("  Actual:   '%s'", key)
   295  				t.Logf("  Index:     %d", i)
   296  			}
   297  		}
   298  	}
   299  }
   300  
   301  // Checks the completions are correct albeit not ordered
   302  func TestFormatSuggestionsUnordered(t *testing.T) {
   303  	count.Tests(t, len(testData)*2)
   304  
   305  	original := make([]string, len(testData))
   306  	copy(original, testData)
   307  
   308  	for i := range testData {
   309  		act := &AutoCompleteT{
   310  			Items:       []string{testData[i]},
   311  			Definitions: map[string]string{testData[i]: "foobar"},
   312  		}
   313  
   314  		FormatSuggestions(act)
   315  
   316  		if act.Items[0] != expected[i] {
   317  			t.Error("formatSuggestionsArray mismatch:")
   318  			t.Logf("  Original: '%s'", original[i])
   319  			t.Logf("  Expected: '%s'", expected[i])
   320  			t.Logf("  Actual:   '%s'", testData[i])
   321  			t.Logf("  Index:     %d", i)
   322  		}
   323  
   324  		if len(act.Definitions) != 1 {
   325  			t.Errorf("formatSuggestionsMap len mismatch: %d", len(act.Definitions))
   326  			return
   327  		}
   328  
   329  		for key := range act.Definitions {
   330  			if key != expected[i] {
   331  				t.Error("formatSuggestionsMap mismatch:")
   332  				t.Logf("  Original: '%s'", original[i])
   333  				t.Logf("  Expected: '%s'", expected[i])
   334  				t.Logf("  Actual:   '%s'", key)
   335  				t.Logf("  Index:     %d", i)
   336  			}
   337  		}
   338  	}
   339  }