Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Fixed parsing of parameters, added tests #242

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func insertParams(command string, params map[string]string) string {

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

Expand All @@ -41,10 +41,14 @@ func SearchForParams(lines []string) map[string]string {
extracted := map[string]string{}
for _, p := range params {
splitted := strings.Split(p[1], "=")
if len(splitted) == 1 {
extracted[p[0]] = ""
} else {
extracted[p[0]] = splitted[1]
key := splitted[0]
_, param_exists := extracted[key]

// Set to empty if no value is provided and param is not already set
if len(splitted) == 1 && !param_exists {
extracted[key] = ""
} else if len(splitted) > 1 {
extracted[key] = splitted[1]
}
}
return extracted
Expand Down
186 changes: 186 additions & 0 deletions dialog/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package dialog

import (
"testing"

"github.com/go-test/deep"
)

func TestSearchForParams(t *testing.T) {
command := "<a=1> <b> hello"

params := map[string]string{
"a": "1",
"b": "",
}

got := SearchForParams([]string{command})

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}
}

func TestSearchForParams_WithNoParams(t *testing.T) {
command := "no params"

got := SearchForParams([]string{command})

if got != nil {
t.Fatalf("wanted nil, got '%v'", got)
}
}

func TestSearchForParams_WithMultipleParams(t *testing.T) {
command := "<a=1> <b> <c=3>"

params := map[string]string{
"a": "1",
"b": "",
"c": "3",
}

got := SearchForParams([]string{command})

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}
}

func TestSearchForParams_WithEmptyCommand(t *testing.T) {
command := ""

got := SearchForParams([]string{command})

if got != nil {
t.Fatalf("wanted nil, got '%v'", got)
}
}

func TestSearchForParams_WithNewline(t *testing.T) {
command := "<a=1> <b> hello\n<c=3>"

params := map[string]string{
"a": "1",
"b": "",
"c": "3",
}

got := SearchForParams([]string{command})

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}
}

func TestSearchForParams_InvalidParamFormat(t *testing.T) {
command := "<a=1 <b> hello"
want := map[string]string{
"b": "",
}
got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKey(t *testing.T) {
command := "<a=1> <a=2> <a=3>"
want := map[string]string{
"a": "3",
}
got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) {
command := "<a=1> <a=2> <a=3>"
want := map[string]string{
"a": "3",
}
got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *testing.T) {
command := "<a=1> <a=2> <a=3>\n<b=4>"
want := map[string]string{
"a": "3",
"b": "4",
}
got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *testing.T) {
command := "<a=1> <a=2 <a=3>"
want := map[string]string{
"a": "3",
}
got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines(t *testing.T) {
command := "<a=1> <a=2> <a=3 \n<b=4>"
want := map[string]string{
"a": "2",
"b": "4",
}

got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines2(t *testing.T) {
command := "<a=1> <a=2> <a=3>\n<b=4"
want := map[string]string{
"a": "3",
}

got := SearchForParams([]string{command})

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/go-test/deep v1.1.0 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github v15.0.0+incompatible h1:jlPg2Cpsxb/FyEV/MFiIE9tW/2RAevQNZDPeHbf5a94=
Expand Down