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

#110 - Fix Slice bug producing a \n when provided with an empty slice #115

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 7 additions & 3 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func ListFiles(path string) *Pipe {
// Slice creates a pipe containing each element of the supplied slice of
// strings, one per line.
func Slice(s []string) *Pipe {
if len(s) == 0 {
return NewPipe()
}
return Echo(strings.Join(s, "\n") + "\n")
}

Expand Down Expand Up @@ -545,9 +548,10 @@ func (p *Pipe) First(n int) *Pipe {
// easier to read:
//
// 10 apple
// 4 banana
// 2 orange
// 1 kumquat
//
// 4 banana
// 2 orange
// 1 kumquat
func (p *Pipe) Freq() *Pipe {
freq := map[string]int{}
type frequency struct {
Expand Down
12 changes: 12 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,18 @@ func TestSliceProducesElementsOfSpecifiedSliceOnePerLine(t *testing.T) {
}
}

func TestSliceGivenEmptySliceProducesEmptyPipe(t *testing.T) {
t.Parallel()
want := ""
got, err := script.Slice([]string{}).String()
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}

func TestStdinReadsFromProgramStandardInput(t *testing.T) {
t.Parallel()
// dummy test to prove coverage
Expand Down