github.com/fumiama/NanoBot@v0.0.0-20231122134259-c22d8183efca/shell_test.go (about)

     1  package nano
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_parse(t *testing.T) {
    12  	shellTests := [...]struct {
    13  		shell    string
    14  		expected []string
    15  	}{
    16  		{`rm -rf /*`, []string{"rm", "-rf", "/*"}},
    17  		{`echo   "cat cat" -n`, []string{"echo", "cat cat", "-n"}},
    18  		{`shutdown	halt	init`, []string{"shutdown", "halt", "init"}},
    19  		{`test test2`, []string{"test", "test2"}},
    20  	}
    21  	for i, v := range shellTests {
    22  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    23  			out := ParseShell(v.shell)
    24  			assert.Equal(t, v.expected, out)
    25  		})
    26  	}
    27  }
    28  
    29  func Test_registerFlag(t *testing.T) {
    30  	type args struct {
    31  		RF    bool   `flag:"rf"`
    32  		File  string `flag:"file"`
    33  		Count int    `flag:"count"`
    34  	}
    35  	got := args{}
    36  	expected := args{
    37  		RF:    true,
    38  		File:  "123",
    39  		Count: 10,
    40  	}
    41  	fs := registerFlag(reflect.TypeOf(args{}), reflect.ValueOf(&got))
    42  	err := fs.Parse([]string{"-rf", "-file=123", "-count", "10"})
    43  	assert.NoError(t, err)
    44  	assert.Equal(t, expected, got)
    45  }