diff options
| author | Shulhan <ms@kilabit.info> | 2023-10-06 01:33:12 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-10-06 01:33:12 +0700 |
| commit | b22f2104dd822a890b8f8b8de5489215493c0812 (patch) | |
| tree | 08b4c281c95ff7f8693cddc3dce2ce25c46c57cf /statement_test.go | |
| parent | 006fe79d7523c47102462595d8e8664fc1910c6a (diff) | |
| download | awwan-b22f2104dd822a890b8f8b8de5489215493c0812.tar.xz | |
all: simplify parsing statement for magic command get
This changes minimize code duplication when parsing magic command
between "#get:" and "#get!" using single function parseStatementGetPut.
In the returned Statement, we changes where to store the source argument
into args[0] instead of in cmd for better readability and minimize
confusion in Copy/SudoCopy.
While at it, we add unit test for "#get:".
Unit test for "#get!" is not possible right now, because it will prompt
for password.
Diffstat (limited to 'statement_test.go')
| -rw-r--r-- | statement_test.go | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/statement_test.go b/statement_test.go index 7d13c9f..51e8481 100644 --- a/statement_test.go +++ b/statement_test.go @@ -11,19 +11,51 @@ import ( func TestParseStatement(t *testing.T) { type testCase struct { - exp *Statement - raw []byte + exp *Statement + expError string + raw []byte } var cases = []testCase{{ + raw: []byte(`#get: `), + expError: `ParseStatement: "#get:": missing arguments`, + }, { + raw: []byte(`#get: src `), + expError: `ParseStatement: "#get:": missing destination file`, + }, { + raw: []byte(`#get: src dst dst2 `), + expError: `ParseStatement: "#get:": too many arguments`, + }, { + raw: []byte(`#get: src dst`), + exp: &Statement{ + kind: statementKindGet, + args: []string{`src`, `dst`}, + raw: []byte(` src dst`), + }, + }, { raw: []byte(`#get: a\ b c`), exp: &Statement{ kind: statementKindGet, - cmd: `a b`, - args: []string{"c"}, + args: []string{`a b`, `c`}, raw: []byte(` a\ b c`), }, }, { + raw: []byte(`#get! `), + expError: `ParseStatement: "#get!": missing arguments`, + }, { + raw: []byte(`#get! src `), + expError: `ParseStatement: "#get!": missing destination file`, + }, { + raw: []byte(`#get! src dst dst2 `), + expError: `ParseStatement: "#get!": too many arguments`, + }, { + raw: []byte(`#get! a\ b c`), + exp: &Statement{ + kind: statementKindSudoGet, + args: []string{`a b`, `c`}, + raw: []byte(` a\ b c`), + }, + }, { raw: []byte(`#put: a b c\ `), exp: &Statement{ kind: statementKindPut, @@ -32,16 +64,6 @@ func TestParseStatement(t *testing.T) { raw: []byte(` a b c\`), }, }, { - raw: []byte(`#get! a\ b c`), - exp: &Statement{ - kind: statementKindSudoGet, - cmd: `a b`, - args: []string{ - "c", - }, - raw: []byte(` a\ b c`), - }, - }, { raw: []byte(`#put! a bc `), exp: &Statement{ kind: statementKindSudoPut, @@ -73,7 +95,8 @@ func TestParseStatement(t *testing.T) { for _, c = range cases { got, err = ParseStatement(c.raw) if err != nil { - t.Fatal(err) + test.Assert(t, `error`, c.expError, err.Error()) + continue } test.Assert(t, string(c.raw), c.exp, got) } |
