src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/compiler_test.go (about)

     1  package eval_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	. "src.elv.sh/pkg/eval"
     8  	"src.elv.sh/pkg/parse"
     9  )
    10  
    11  var autofixTests = []struct {
    12  	Name string
    13  	Code string
    14  
    15  	WantAutofixes []string
    16  }{
    17  	{
    18  		Name: "get variable from unimported builtin module",
    19  		Code: "echo $mod1:foo",
    20  
    21  		WantAutofixes: []string{"use mod1"},
    22  	},
    23  	{
    24  		Name: "set variable from unimported builtin module",
    25  		Code: "set mod1:foo = bar",
    26  
    27  		WantAutofixes: []string{"use mod1"},
    28  	},
    29  	{
    30  		Name: "tmp set variable from unimported builtin module",
    31  		Code: "tmp mod1:foo = bar",
    32  
    33  		WantAutofixes: []string{"use mod1"},
    34  	},
    35  	{
    36  		Name: "call command from unimported builtin module",
    37  		Code: "mod1:foo",
    38  
    39  		WantAutofixes: []string{"use mod1"},
    40  	},
    41  	{
    42  		Name: "no autofix for using variable from imported module",
    43  		Code: "use mod1; echo $mod1:foo",
    44  
    45  		WantAutofixes: nil,
    46  	},
    47  	{
    48  		Name: "no autofix for using variable from non-builtin module",
    49  		Code: "echo $mod-external:foo",
    50  
    51  		WantAutofixes: nil,
    52  	},
    53  }
    54  
    55  func TestAutofix(t *testing.T) {
    56  	ev := NewEvaler()
    57  	ev.AddModule("mod1", &Ns{})
    58  	for _, tc := range autofixTests {
    59  		t.Run(tc.Name, func(t *testing.T) {
    60  			_, autofixes, _ := ev.Check(parse.Source{Name: "[test]", Code: tc.Code}, nil)
    61  			if diff := cmp.Diff(tc.WantAutofixes, autofixes); diff != "" {
    62  				t.Errorf("autofixes (-want +got):\n%s", diff)
    63  			}
    64  		})
    65  	}
    66  }