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(slice): fix bug in slice that produces new line for empty input slice #216

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func Post(url string) *Pipe {

// Slice creates a pipe containing each element of s, one per line.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Slice creates a pipe containing each element of s, one per line.
// Slice creates a pipe containing each element of s, one per line. If s is
// empty or nil, then the pipe is empty.

func Slice(s []string) *Pipe {
if len(s) == 0 {
return NewPipe()
}
return Echo(strings.Join(s, "\n") + "\n")
}

Expand Down
33 changes: 26 additions & 7 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,15 +1375,34 @@ func TestReadAutoCloser_ReadsAllDataFromSourceAndClosesItAutomatically(t *testin
}
}

func TestSliceProducesElementsOfSpecifiedSliceOnePerLine(t *testing.T) {
func TestSlice_(t *testing.T) {
t.Parallel()
want := "1\n2\n3\n"
got, err := script.Slice([]string{"1", "2", "3"}).String()
if err != nil {
t.Fatal(err)
tests := []struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a shame to have a table test with just two cases, doesn't it? Especially as they're different cases, not two examples of the same behaviour.

Wouldn't you rather just structure this as two separate tests? That tends to make each test clearer, more focused, and easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

name string
input []string
want string
}{
{
name: "produces elements of specified slice one per line",
input: []string{"1", "2", "3"},
want: "1\n2\n3\n",
},
{
name: "given empty slice produces empty pipe",
input: []string{},
want: "",
},
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := script.Slice(test.input).String()
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(test.want, got) {
t.Error(cmp.Diff(test.want, got))
}
})
}
}

Expand Down
Loading