-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separates utils to own file. Tests trimEmpty. #16
- Loading branch information
Mariano Gappa
committed
Sep 23, 2018
1 parent
fcb2047
commit 7c18efc
Showing
4 changed files
with
94 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
version: '3' | ||
services: | ||
blah-test: | ||
test: | ||
image: golang:1.11 | ||
volumes: | ||
- "$PWD:/go/src/sql" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func println(w io.Writer, s string) { | ||
printLock.Lock() | ||
defer printLock.Unlock() | ||
fmt.Fprintln(w, s) | ||
} | ||
|
||
func readInput(r io.Reader) string { | ||
ls := []string{} | ||
var err error | ||
rd := bufio.NewReader(r) | ||
|
||
for { | ||
var s string | ||
s, err = rd.ReadString('\n') | ||
|
||
if err == io.EOF { | ||
return strings.Join(ls, " ") | ||
} | ||
s = strings.TrimSpace(s) | ||
if len(s) == 0 { | ||
continue | ||
} | ||
ls = append(ls, strings.TrimSpace(s)) | ||
} | ||
} | ||
|
||
func trimEmpty(s []string) []string { | ||
var r = make([]string, 0) | ||
for _, str := range s { | ||
if str != "" { | ||
r = append(r, str) | ||
} | ||
} | ||
return r | ||
} | ||
|
||
func awaitSignal(cancel context.CancelFunc) { | ||
signals := make(chan os.Signal) | ||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) | ||
<-signals | ||
cancel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestTrimEmpty(t *testing.T) { | ||
var ts = []struct { | ||
name string | ||
input []string | ||
expected []string | ||
}{ | ||
{ | ||
name: "Empty case", | ||
input: []string{}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Removes multiple empty from beginning, middle and end", | ||
input: []string{"", "a", "", "b", ""}, | ||
expected: []string{"a", "b"}, | ||
}, | ||
{ | ||
name: "Doesn't remove spaces", | ||
input: []string{" ", "a", "", "b", ""}, | ||
expected: []string{" ", "a", "b"}, | ||
}, | ||
} | ||
for _, tc := range ts { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var actual = trimEmpty(tc.input) | ||
if !reflect.DeepEqual(tc.expected, actual) { | ||
t.Errorf("Expected %v but got %v", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |