github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/spellcheck/highlight_test.go (about)

     1  package spellcheck
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/config"
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/lang/types"
     9  	"github.com/lmorg/murex/test/count"
    10  	"github.com/lmorg/murex/utils/json"
    11  )
    12  
    13  func TestHighlighter(t *testing.T) {
    14  	tests := []struct {
    15  		Line     string
    16  		Words    []string
    17  		Expected string
    18  	}{
    19  		{
    20  			Line:     `The quick brown fox jumped over the lazy dog`,
    21  			Words:    []string{`quick`},
    22  			Expected: `The _quick_ brown fox jumped over the lazy dog`,
    23  		},
    24  		{
    25  			Line:     `The quick brown fox jumped over the lazy dog`,
    26  			Words:    []string{`quick`, `the`},
    27  			Expected: `The _quick_ brown fox jumped over _the_ lazy dog`,
    28  		},
    29  		{
    30  			Line:     `The quick brown fox jumped over the lazy dog`,
    31  			Words:    []string{`dog`},
    32  			Expected: `The quick brown fox jumped over the lazy _dog_`,
    33  		},
    34  		{
    35  			Line:     `The quick brown fox jumped over the lazy dog`,
    36  			Words:    []string{`The`},
    37  			Expected: `_The_ quick brown fox jumped over the lazy dog`,
    38  		},
    39  		{
    40  			Line:     `The quick brown fox jumped over the lazy dog`,
    41  			Words:    []string{`foobar`},
    42  			Expected: `The quick brown fox jumped over the lazy dog`,
    43  		},
    44  		{
    45  			Line:     `The quick brown fox jumped over the lazy dog`,
    46  			Words:    []string{`own`},
    47  			Expected: `The quick brown fox jumped over the lazy dog`,
    48  		},
    49  		///
    50  		{
    51  			Line:     `Hello, 世界, world`,
    52  			Words:    []string{`Hello`},
    53  			Expected: `_Hello_, 世界, world`,
    54  		},
    55  		{
    56  			Line:     `Hello, 世界, world`,
    57  			Words:    []string{`world`},
    58  			Expected: `Hello, 世界, _world_`,
    59  		},
    60  		{
    61  			Line:     `Hello, 世界, world`,
    62  			Words:    []string{`世界`},
    63  			Expected: `Hello, _世界_, world`,
    64  		},
    65  		{
    66  			Line:     `Hello, 世界☺, world`,
    67  			Words:    []string{`世界`},
    68  			Expected: `Hello, _世界_☺, world`,
    69  		},
    70  		/// ignore single character terms
    71  		{
    72  			Line:     `Hello, 世 界☺, world`,
    73  			Words:    []string{`世`},
    74  			Expected: `Hello, 世 界☺, world`,
    75  		},
    76  		{
    77  			Line:     `The quick brown fox jumped o ver the lazy dog`,
    78  			Words:    []string{`o`, `ver`},
    79  			Expected: `The quick brown fox jumped o _ver_ the lazy dog`,
    80  		},
    81  		{
    82  			Line:     `The quick brown fox jumped ov er the lazy dog`,
    83  			Words:    []string{`ov`, `er`},
    84  			Expected: `The quick brown fox jumped _ov_ _er_ the lazy dog`,
    85  		},
    86  		///
    87  		{
    88  			Line:     `foo`,
    89  			Words:    []string{`foo`},
    90  			Expected: `_foo_`,
    91  		},
    92  		{
    93  			Line:     `foo `,
    94  			Words:    []string{`foo`},
    95  			Expected: `_foo_ `,
    96  		},
    97  		{
    98  			Line:     `foo f`,
    99  			Words:    []string{`foo`},
   100  			Expected: `_foo_ f`,
   101  		},
   102  		{
   103  			Line:     `foo fo`,
   104  			Words:    []string{`foo`},
   105  			Expected: `_foo_ fo`,
   106  		},
   107  		{
   108  			Line:     `foo foo`,
   109  			Words:    []string{`foo`},
   110  			Expected: `_foo_ _foo_`,
   111  		},
   112  		{
   113  			Line:     `foo foob`,
   114  			Words:    []string{`foo`},
   115  			Expected: `_foo_ foob`,
   116  		},
   117  		{
   118  			Line:     `foo foobar`,
   119  			Words:    []string{`foo`},
   120  			Expected: `_foo_ foobar`,
   121  		},
   122  		/// bug fix:
   123  		{
   124  			Line:     `am ammend`,
   125  			Words:    []string{`ammend`},
   126  			Expected: `am _ammend_`,
   127  		},
   128  	}
   129  
   130  	count.Tests(t, len(tests))
   131  
   132  	lang.InitEnv()
   133  	lang.ShellProcess.Config.Define("shell", "color", config.Properties{
   134  		Description: "ANSI escape sequences in Murex builtins to highlight syntax errors, history completions, {SGR} variables, etc",
   135  		//Default:     (runtime.GOOS != "windows" && isInteractive),
   136  		Default:  true,
   137  		DataType: types.Boolean,
   138  		Global:   true,
   139  	})
   140  
   141  	var highlight = &highlightT{
   142  		start: "_",
   143  		end:   "_",
   144  	}
   145  
   146  	for i, test := range tests {
   147  		actual := test.Line
   148  		for _, word := range test.Words {
   149  			highlighter(&actual, []rune(word), highlight)
   150  		}
   151  
   152  		if actual != test.Expected {
   153  			t.Errorf("Mismatch in test %d", i)
   154  			t.Logf("  Line:     '%s'", test.Line)
   155  			t.Logf("  Words:     %s", json.LazyLogging(test.Words))
   156  			t.Logf("  Expected: '%s'", test.Expected)
   157  			t.Logf("  Actual:   '%s'", actual)
   158  		}
   159  	}
   160  }