-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_test.go
166 lines (157 loc) · 3.9 KB
/
statement_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-FileCopyrightText: 2021 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//go:build !integration
package awwan
import (
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestParseStatement(t *testing.T) {
type testCase struct {
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,
args: []string{`a b`, `c`},
raw: []byte(` a\ b c`),
},
}, {
raw: []byte(`#get:user:group `),
expError: `ParseStatement: "#get:": missing arguments`,
}, {
raw: []byte(`#get:user:group src`),
expError: `ParseStatement: "#get:": missing destination file`,
}, {
raw: []byte(`#get:user:group src dst`),
exp: &Statement{
kind: statementKindGet,
owner: `user:group`,
args: []string{`src`, `dst`},
raw: []byte(`src dst`),
},
}, {
raw: []byte(`#get:+800 src dst`),
expError: `ParseStatement: "#get:": strconv.ParseUint: parsing "800": invalid syntax`,
}, {
raw: []byte(`#get:user:group+561 src dst`),
exp: &Statement{
kind: statementKindGet,
owner: `user:group`,
mode: 369,
args: []string{`src`, `dst`},
raw: []byte(`src dst`),
},
}, {
raw: []byte(`#get:user:group+0561 src dst`),
exp: &Statement{
kind: statementKindGet,
owner: `user:group`,
mode: 0561,
args: []string{`src`, `dst`},
raw: []byte(`src dst`),
},
}, {
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: `),
expError: `ParseStatement: "#put:": missing arguments`,
}, {
raw: []byte(`#put: src `),
expError: `ParseStatement: "#put:": missing destination file`,
}, {
raw: []byte(`#put: src dst dst2 `),
expError: `ParseStatement: "#put:": too many arguments`,
}, {
raw: []byte(`#put: a bc\ `),
exp: &Statement{
kind: statementKindPut,
args: []string{`a`, `bc`},
raw: []byte(` a bc\`),
},
}, {
raw: []byte(`#put! `),
expError: `ParseStatement: "#put!": missing arguments`,
}, {
raw: []byte(`#put! src `),
expError: `ParseStatement: "#put!": missing destination file`,
}, {
raw: []byte(`#put! src dst dst2 `),
expError: `ParseStatement: "#put!": too many arguments`,
}, {
raw: []byte(`#put! a bc `),
exp: &Statement{
kind: statementKindSudoPut,
args: []string{`a`, `bc`},
raw: []byte(` a bc`),
},
}, {
raw: []byte(`#require:\ a\ `),
exp: &Statement{
kind: statementKindRequire,
cmd: ` a`,
raw: []byte(`\ a\`),
},
}, {
raw: []byte(`#requ: a `),
exp: &Statement{
kind: statementKindComment,
raw: []byte(`#requ: a`),
},
}, {
raw: []byte(`#local: echo "a"`),
exp: &Statement{
kind: statementKindLocal,
cmd: `echo`,
args: []string{`a`},
raw: []byte(` echo "a"`),
},
}}
var (
c testCase
got *Statement
err error
)
for _, c = range cases {
t.Logf(`ParseStatement: %s`, c.raw)
got, err = ParseStatement(c.raw)
if err != nil {
test.Assert(t, `error`, c.expError, err.Error())
continue
}
test.Assert(t, string(c.raw), c.exp, got)
}
}