Skip to content

Commit b5abca3

Browse files
authored
Merge pull request knqyf263#242 from knqyf263/parameter-parsing-bug-fixes
fix: 🐛 Fixed parsing of parameters, added tests
2 parents ac91ac6 + 66fdce2 commit b5abca3

File tree

4 files changed

+198
-5
lines changed

4 files changed

+198
-5
lines changed

Diff for: dialog/params.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func insertParams(command string, params map[string]string) string {
2929

3030
// SearchForParams returns variables from a command
3131
func SearchForParams(lines []string) map[string]string {
32-
re := `<([\S].+?[\S])>`
32+
re := `<([\S]+?)>`
3333
if len(lines) == 1 {
3434
r, _ := regexp.Compile(re)
3535

@@ -41,10 +41,14 @@ func SearchForParams(lines []string) map[string]string {
4141
extracted := map[string]string{}
4242
for _, p := range params {
4343
splitted := strings.Split(p[1], "=")
44-
if len(splitted) == 1 {
45-
extracted[p[0]] = ""
46-
} else {
47-
extracted[p[0]] = splitted[1]
44+
key := splitted[0]
45+
_, param_exists := extracted[key]
46+
47+
// Set to empty if no value is provided and param is not already set
48+
if len(splitted) == 1 && !param_exists {
49+
extracted[key] = ""
50+
} else if len(splitted) > 1 {
51+
extracted[key] = splitted[1]
4852
}
4953
}
5054
return extracted

Diff for: dialog/params_test.go

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package dialog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-test/deep"
7+
)
8+
9+
func TestSearchForParams(t *testing.T) {
10+
command := "<a=1> <b> hello"
11+
12+
params := map[string]string{
13+
"a": "1",
14+
"b": "",
15+
}
16+
17+
got := SearchForParams([]string{command})
18+
19+
for key, value := range params {
20+
if got[key] != value {
21+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
22+
}
23+
}
24+
25+
for key, value := range got {
26+
if params[key] != value {
27+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
28+
}
29+
}
30+
}
31+
32+
func TestSearchForParams_WithNoParams(t *testing.T) {
33+
command := "no params"
34+
35+
got := SearchForParams([]string{command})
36+
37+
if got != nil {
38+
t.Fatalf("wanted nil, got '%v'", got)
39+
}
40+
}
41+
42+
func TestSearchForParams_WithMultipleParams(t *testing.T) {
43+
command := "<a=1> <b> <c=3>"
44+
45+
params := map[string]string{
46+
"a": "1",
47+
"b": "",
48+
"c": "3",
49+
}
50+
51+
got := SearchForParams([]string{command})
52+
53+
for key, value := range params {
54+
if got[key] != value {
55+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
56+
}
57+
}
58+
59+
for key, value := range got {
60+
if params[key] != value {
61+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
62+
}
63+
}
64+
}
65+
66+
func TestSearchForParams_WithEmptyCommand(t *testing.T) {
67+
command := ""
68+
69+
got := SearchForParams([]string{command})
70+
71+
if got != nil {
72+
t.Fatalf("wanted nil, got '%v'", got)
73+
}
74+
}
75+
76+
func TestSearchForParams_WithNewline(t *testing.T) {
77+
command := "<a=1> <b> hello\n<c=3>"
78+
79+
params := map[string]string{
80+
"a": "1",
81+
"b": "",
82+
"c": "3",
83+
}
84+
85+
got := SearchForParams([]string{command})
86+
87+
for key, value := range params {
88+
if got[key] != value {
89+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
90+
}
91+
}
92+
93+
for key, value := range got {
94+
if params[key] != value {
95+
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
96+
}
97+
}
98+
}
99+
100+
func TestSearchForParams_InvalidParamFormat(t *testing.T) {
101+
command := "<a=1 <b> hello"
102+
want := map[string]string{
103+
"b": "",
104+
}
105+
got := SearchForParams([]string{command})
106+
107+
if diff := deep.Equal(want, got); diff != nil {
108+
t.Fatal(diff)
109+
}
110+
}
111+
112+
func TestSearchForParams_MultipleParamsSameKey(t *testing.T) {
113+
command := "<a=1> <a=2> <a=3>"
114+
want := map[string]string{
115+
"a": "3",
116+
}
117+
got := SearchForParams([]string{command})
118+
119+
if diff := deep.Equal(want, got); diff != nil {
120+
t.Fatal(diff)
121+
}
122+
}
123+
124+
func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) {
125+
command := "<a=1> <a=2> <a=3>"
126+
want := map[string]string{
127+
"a": "3",
128+
}
129+
got := SearchForParams([]string{command})
130+
131+
if diff := deep.Equal(want, got); diff != nil {
132+
t.Fatal(diff)
133+
}
134+
}
135+
136+
func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *testing.T) {
137+
command := "<a=1> <a=2> <a=3>\n<b=4>"
138+
want := map[string]string{
139+
"a": "3",
140+
"b": "4",
141+
}
142+
got := SearchForParams([]string{command})
143+
144+
if diff := deep.Equal(want, got); diff != nil {
145+
t.Fatal(diff)
146+
}
147+
}
148+
149+
func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *testing.T) {
150+
command := "<a=1> <a=2 <a=3>"
151+
want := map[string]string{
152+
"a": "3",
153+
}
154+
got := SearchForParams([]string{command})
155+
156+
if diff := deep.Equal(want, got); diff != nil {
157+
t.Fatal(diff)
158+
}
159+
}
160+
161+
func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines(t *testing.T) {
162+
command := "<a=1> <a=2> <a=3 \n<b=4>"
163+
want := map[string]string{
164+
"a": "2",
165+
"b": "4",
166+
}
167+
168+
got := SearchForParams([]string{command})
169+
170+
if diff := deep.Equal(want, got); diff != nil {
171+
t.Fatal(diff)
172+
}
173+
}
174+
175+
func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines2(t *testing.T) {
176+
command := "<a=1> <a=2> <a=3>\n<b=4"
177+
want := map[string]string{
178+
"a": "3",
179+
}
180+
181+
got := SearchForParams([]string{command})
182+
183+
if diff := deep.Equal(want, got); diff != nil {
184+
t.Fatal(diff)
185+
}
186+
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727

2828
require (
2929
github.com/alessio/shellescape v1.4.1 // indirect
30+
github.com/go-test/deep v1.1.0 // indirect
3031
github.com/golang/protobuf v1.2.0 // indirect
3132
github.com/google/go-querystring v1.0.0 // indirect
3233
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1717
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1818
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
1919
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
20+
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
21+
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
2022
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
2123
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2224
github.com/google/go-github v15.0.0+incompatible h1:jlPg2Cpsxb/FyEV/MFiIE9tW/2RAevQNZDPeHbf5a94=

0 commit comments

Comments
 (0)