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

     1  package spellcheck_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	_ "github.com/lmorg/murex/builtins"
     8  	"github.com/lmorg/murex/config"
     9  	"github.com/lmorg/murex/config/defaults"
    10  	"github.com/lmorg/murex/lang"
    11  	"github.com/lmorg/murex/lang/types"
    12  	"github.com/lmorg/murex/test/count"
    13  	"github.com/lmorg/murex/utils/ansi"
    14  	"github.com/lmorg/murex/utils/spellcheck"
    15  	"github.com/lmorg/murex/utils/spellcheck/userdictionary"
    16  )
    17  
    18  func configDefaults(c *config.Config) {
    19  	c.Define("shell", "spellcheck-enabled", config.Properties{
    20  		Description: "Enable spellchecking in the interactive prompt",
    21  		Default:     false,
    22  		DataType:    types.Boolean,
    23  		Global:      true,
    24  	})
    25  
    26  	c.Define("shell", "spellcheck-func", config.Properties{
    27  		Description: "Code block to run as part of the spellchecker (STDIN the line, STDOUT is array for misspelt words)",
    28  		Default:     "{ -> aspell list }",
    29  		DataType:    types.CodeBlock,
    30  		Global:      true,
    31  	})
    32  
    33  	c.Define("shell", "spellcheck-user-dictionary", config.Properties{
    34  		Description: "An array of words not to count as misspellings",
    35  		Default:     userdictionary.Get(),
    36  		DataType:    types.Json,
    37  		Global:      true,
    38  		GoFunc: config.GoFuncProperties{
    39  			Read:  userdictionary.Read,
    40  			Write: userdictionary.Write,
    41  		},
    42  	})
    43  }
    44  
    45  func TestSpellcheckCrLf(t *testing.T) {
    46  	if os.Getenv("MUREX_TEST_SKIP_SPELLCHECK") != "" {
    47  		t.Skip("Environmental variable `MUREX_TEST_SKIP_SPELLCHECK` set")
    48  		return
    49  	}
    50  
    51  	count.Tests(t, 1)
    52  	lang.InitEnv()
    53  	//defaults.Defaults(lang.ShellProcess.Config, false)
    54  	configDefaults(lang.ShellProcess.Config)
    55  
    56  	err := lang.ShellProcess.Config.Set("shell", "spellcheck-enabled", true, nil)
    57  	if err != nil {
    58  		t.Fatalf("Unable to set spellcheck-enabled config: %s", err)
    59  	}
    60  
    61  	err = lang.ShellProcess.Config.Set("shell", "spellcheck-func", `{ -> jsplit ' ' -> suffix "\n" }`, nil)
    62  	if err != nil {
    63  		t.Fatalf("Unable to set spellcheck-func config: %s", err)
    64  	}
    65  
    66  	line := "the quick brown fox"
    67  	newLine, err := spellcheck.String(line)
    68  	ansiLine := ansi.ExpandConsts("{UNDERLINE}the{UNDEROFF} {UNDERLINE}quick{UNDEROFF} {UNDERLINE}brown{UNDEROFF} {UNDERLINE}fox{UNDEROFF}")
    69  
    70  	if newLine != ansiLine {
    71  		t.Error("spellcheck output doesn't match expected:")
    72  		t.Logf("  Expected: '%s'", ansiLine)
    73  		t.Logf("  Actual:   '%s'", newLine)
    74  	}
    75  
    76  	if err != nil {
    77  		t.Errorf("spellcheck produced an error: %s", err.Error())
    78  	}
    79  }
    80  
    81  func TestSpellcheckZeroLenStr(t *testing.T) {
    82  	if os.Getenv("MUREX_TEST_SKIP_SPELLCHECK") != "" {
    83  		t.Skip("Environmental variable `MUREX_TEST_SKIP_SPELLCHECK` set")
    84  		return
    85  	}
    86  
    87  	count.Tests(t, 1)
    88  	lang.InitEnv()
    89  	//defaults.Defaults(lang.ShellProcess.Config, false)
    90  	configDefaults(lang.ShellProcess.Config)
    91  
    92  	err := lang.ShellProcess.Config.Set("shell", "spellcheck-enabled", true, nil)
    93  	if err != nil {
    94  		t.Fatalf("Unable to set spellcheck-enabled config: %s", err)
    95  	}
    96  
    97  	err = lang.ShellProcess.Config.Set("shell", "spellcheck-func", `{ -> jsplit '\s' -> append '' }`, nil)
    98  	if err != nil {
    99  		t.Fatalf("Unable to set spellcheck-func config: %s", err)
   100  	}
   101  
   102  	line := "the quick brown fox"
   103  	newLine, err := spellcheck.String(line)
   104  	ansiLine := ansi.ExpandConsts("{UNDERLINE}the{UNDEROFF} {UNDERLINE}quick{UNDEROFF} {UNDERLINE}brown{UNDEROFF} {UNDERLINE}fox{UNDEROFF}")
   105  
   106  	if newLine != ansiLine {
   107  		t.Error("spellcheck output doesn't match expected:")
   108  		t.Logf("  Expected: '%s'", ansiLine)
   109  		t.Logf("  Actual:   '%s'", newLine)
   110  	}
   111  
   112  	if err != nil {
   113  		t.Errorf("spellcheck produced an error: %s", err.Error())
   114  	}
   115  }
   116  
   117  func TestSpellcheckVariable(t *testing.T) {
   118  	count.Tests(t, 1)
   119  	lang.InitEnv()
   120  	defaults.Config(lang.ShellProcess.Config, false)
   121  
   122  	err := lang.ShellProcess.Config.Set("shell", "spellcheck-enabled", true, nil)
   123  	if err != nil {
   124  		t.Fatalf("Unable to set spellcheck-enabled config: %s", err)
   125  	}
   126  
   127  	err = lang.ShellProcess.Config.Set("shell", "spellcheck-func", `{ -> jsplit ' ' -> suffix "\n" }`, nil)
   128  	if err != nil {
   129  		t.Fatalf("Unable to set spellcheck-func config: %s", err)
   130  	}
   131  
   132  	os.Setenv("MUREX_TEST_SPELLCHECK_TEST", "quick")
   133  
   134  	line := "$the $MUREX_TEST_SPELLCHECK_TEST $brown $fox"
   135  	newLine, err := spellcheck.String(line)
   136  	ansiLine := ansi.ExpandConsts("{UNDERLINE}$the{UNDEROFF} {UNDERLINE}$MUREX_TEST_SPELLCHECK_TEST{UNDEROFF} {UNDERLINE}$brown{UNDEROFF} {UNDERLINE}$fox{UNDEROFF}")
   137  
   138  	if newLine != ansiLine {
   139  		t.Error("spellcheck output doesn't match expected:")
   140  		t.Logf("  Expected: '%s'", ansiLine)
   141  		t.Logf("  Actual:   '%s'", newLine)
   142  	}
   143  
   144  	if err != nil {
   145  		t.Errorf("spellcheck produced an error: %s", err.Error())
   146  	}
   147  }