Skip to content

Commit

Permalink
Separates utils to own file. Tests trimEmpty. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariano Gappa committed Sep 23, 2018
1 parent fcb2047 commit 7c18efc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 46 deletions.
45 changes: 0 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
)

type database struct {
Expand Down Expand Up @@ -187,46 +185,3 @@ func runSQL(quitContext context.Context, db database, sql string, key string, pr

return result
}

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 []string
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()
}
2 changes: 1 addition & 1 deletion test-docker-compose.yml
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"
Expand Down
55 changes: 55 additions & 0 deletions utils.go
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()
}
38 changes: 38 additions & 0 deletions utils_test.go
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)
}
})
}
}

0 comments on commit 7c18efc

Please sign in to comment.