Skip to content

Commit

Permalink
Fix comma condition and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-klotho committed Nov 21, 2023
1 parent 8bfcfd0 commit b281ac5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/infra/iac3/map_marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (m TsMap) String() string {
i := 0
for k, v := range m {
buf.WriteString(fmt.Sprintf("%s: %v", k, v))
if i <= keys {
buf.WriteRune(',')
if i < keys-1 {
buf.WriteString(", ")
}
i++
}
Expand All @@ -44,8 +44,8 @@ func (l TsList) String() string {
buf.WriteRune('[')
for i, v := range l {
fmt.Fprintf(&buf, "%v", v)
if i < len(l) {
buf.WriteRune(',')
if i < len(l)-1 {
buf.WriteString(", ")
}
}
buf.WriteRune(']')
Expand Down
65 changes: 65 additions & 0 deletions pkg/infra/iac3/map_marshaller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package iac3

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTsMap_String(t *testing.T) {
tests := []struct {
name string
m TsMap
want string
}{
{
name: "empty map",
m: TsMap{},
want: "{}",
},
{
name: "map with one entry",
m: TsMap{"foo": "bar"},
want: "{foo: bar}",
},
{
name: "map with multiple entries",
m: TsMap{"foo": "bar", "baz": "qux"},
want: "{foo: bar, baz: qux}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.m.String(), tt.want)
})
}
}

func TestTsList_String(t *testing.T) {
tests := []struct {
name string
l TsList
want string
}{
{
name: "empty list",
l: TsList{},
want: "[]",
},
{
name: "list with one entry",
l: TsList{"foo"},
want: "[foo]",
},
{
name: "list with multiple entries",
l: TsList{"foo", "bar"},
want: "[foo, bar]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.l.String(), tt.want)
})
}
}

0 comments on commit b281ac5

Please sign in to comment.