Skip to content
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