github.com/mithrandie/csvq@v1.18.1/lib/excmd/argument_scanner_test.go (about)

     1  package excmd
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type argumentScannerResult struct {
     9  	Text        string
    10  	ElementType ElementType
    11  }
    12  
    13  var argumentScannerScanTests = []struct {
    14  	Input  string
    15  	Expect []argumentScannerResult
    16  	Error  string
    17  }{
    18  	{
    19  		Input:  "",
    20  		Expect: []argumentScannerResult(nil),
    21  	},
    22  	{
    23  		Input: "arg",
    24  		Expect: []argumentScannerResult{
    25  			{Text: "arg", ElementType: FixedString},
    26  		},
    27  	},
    28  	{
    29  		Input: "\\$",
    30  		Expect: []argumentScannerResult{
    31  			{Text: "$", ElementType: FixedString},
    32  		},
    33  	},
    34  	{
    35  		Input: "\\@",
    36  		Expect: []argumentScannerResult{
    37  			{Text: "@", ElementType: FixedString},
    38  		},
    39  	},
    40  	{
    41  		Input: "arg\\@arg\\\\\\$arg\\arg",
    42  		Expect: []argumentScannerResult{
    43  			{Text: "arg@arg\\\\$arg\\arg", ElementType: FixedString},
    44  		},
    45  	},
    46  	{
    47  		Input: "@var",
    48  		Expect: []argumentScannerResult{
    49  			{Text: "var", ElementType: Variable},
    50  		},
    51  	},
    52  	{
    53  		Input: "@%var",
    54  		Expect: []argumentScannerResult{
    55  			{Text: "var", ElementType: EnvironmentVariable},
    56  		},
    57  	},
    58  	{
    59  		Input: "@%`var\\\\var\\`var`",
    60  		Expect: []argumentScannerResult{
    61  			{Text: "var\\var`var", ElementType: EnvironmentVariable},
    62  		},
    63  	},
    64  	{
    65  		Input: "@#var",
    66  		Expect: []argumentScannerResult{
    67  			{Text: "var", ElementType: RuntimeInformation},
    68  		},
    69  	},
    70  	{
    71  		Input: "${print @a}",
    72  		Expect: []argumentScannerResult{
    73  			{Text: "print @a", ElementType: CsvqExpression},
    74  		},
    75  	},
    76  	{
    77  		Input: "${print 'a\\{bc\\\\\\}de'}",
    78  		Expect: []argumentScannerResult{
    79  			{Text: "print 'a{bc\\\\}de'", ElementType: CsvqExpression},
    80  		},
    81  	},
    82  	{
    83  		Input: "cmd --option arg1 'arg 2' arg3",
    84  		Expect: []argumentScannerResult{
    85  			{Text: "cmd --option arg1 'arg 2' arg3", ElementType: FixedString},
    86  		},
    87  	},
    88  	{
    89  		Input: "arg${print @a}arg",
    90  		Expect: []argumentScannerResult{
    91  			{Text: "arg", ElementType: FixedString},
    92  			{Text: "print @a", ElementType: CsvqExpression},
    93  			{Text: "arg", ElementType: FixedString},
    94  		},
    95  	},
    96  	{
    97  		Input: "@%`var",
    98  		Error: "environment variable not terminated",
    99  	},
   100  	{
   101  		Input: "arg@%",
   102  		Error: "invalid variable symbol",
   103  	},
   104  	{
   105  		Input: "arg$arg",
   106  		Error: "invalid command symbol",
   107  	},
   108  	{
   109  		Input: "arg${print @a",
   110  		Error: "command not terminated",
   111  	},
   112  }
   113  
   114  func TestArgumentScanner_Scan(t *testing.T) {
   115  	for _, v := range argumentScannerScanTests {
   116  		scanner := new(ArgumentScanner).Init(v.Input)
   117  		var args []argumentScannerResult
   118  		for scanner.Scan() {
   119  			args = append(args, argumentScannerResult{Text: scanner.Text(), ElementType: scanner.ElementType()})
   120  		}
   121  		err := scanner.Err()
   122  
   123  		if err != nil {
   124  			if v.Error == "" {
   125  				t.Errorf("unexpected error %q for %q", err.Error(), v.Input)
   126  			} else if v.Error != err.Error() {
   127  				t.Errorf("error %q, want error %q for %q", err.Error(), v.Error, v.Input)
   128  			}
   129  			continue
   130  		}
   131  		if v.Error != "" {
   132  			t.Errorf("no error, want error %q for %q", v.Error, v.Input)
   133  			continue
   134  		}
   135  
   136  		if !reflect.DeepEqual(args, v.Expect) {
   137  			t.Errorf("result = %#v, want %#v for %q", args, v.Expect, v.Input)
   138  		}
   139  	}
   140  }