From ceea6aa71baa32df8963dddefa6f8b77caeb5019 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 19 Oct 2024 10:39:18 -0400 Subject: [PATCH 1/4] Fix Bug In Slice That Produces NewLine For Empty Input --- script.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/script.go b/script.go index 1fb2978..9365688 100644 --- a/script.go +++ b/script.go @@ -182,6 +182,9 @@ func Post(url string) *Pipe { // Slice creates a pipe containing each element of s, one per line. func Slice(s []string) *Pipe { + if len(s) == 0 { + return NewPipe() + } return Echo(strings.Join(s, "\n") + "\n") } From 3117d5b110c948ae04038b5f7c96d66cea6ea1f8 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 19 Oct 2024 10:39:51 -0400 Subject: [PATCH 2/4] Add Unit Test --- script_test.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/script_test.go b/script_test.go index a58ff47..b4a534f 100644 --- a/script_test.go +++ b/script_test.go @@ -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 { + 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)) + } + }) } } From d1335f04b687072d49a10f8f44de296686fdaff3 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sat, 19 Oct 2024 14:40:12 -0400 Subject: [PATCH 3/4] Address Feedback From PR Review --- script_test.go | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/script_test.go b/script_test.go index b4a534f..a3f9124 100644 --- a/script_test.go +++ b/script_test.go @@ -1375,34 +1375,27 @@ func TestReadAutoCloser_ReadsAllDataFromSourceAndClosesItAutomatically(t *testin } } -func TestSlice_(t *testing.T) { +func TestSliceProducesElementsOfSpecifiedSliceOnePerLine(t *testing.T) { t.Parallel() - tests := []struct { - 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: "", - }, + want := "1\n2\n3\n" + got, err := script.Slice([]string{"1", "2", "3"}).String() + if err != nil { + t.Fatal(err) } - 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)) - } - }) + if !cmp.Equal(want, got) { + t.Error(cmp.Diff(want, got)) + } +} + +func TestSliceGivenEmptySliceProducesEmptyPipe(t *testing.T) { + t.Parallel() + want := "" + got, err := script.Slice([]string{}).String() + if err != nil { + t.Fatal(err) + } + if want != got { + t.Fatalf("want %q, got %q", want, got) } } From b87c72129eef2f5f483762164063adf64a2fe299 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Sun, 20 Oct 2024 09:10:56 -0400 Subject: [PATCH 4/4] Address Feedback From PR Review --- script.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script.go b/script.go index 9365688..2d39bdb 100644 --- a/script.go +++ b/script.go @@ -180,7 +180,8 @@ func Post(url string) *Pipe { return NewPipe().Post(url) } -// 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()