Skip to content

Commit

Permalink
improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mrt committed Feb 18, 2017
1 parent 800a342 commit bd8de43
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: go

script:
- go test -v
- go test -cover -v . ./cmd/gocha
- go test -bench .
53 changes: 53 additions & 0 deletions cmd/gocha/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"io"

"github.com/t-mrt/gocha"
"gopkg.in/alecthomas/kingpin.v2"
)

const (
ExitCodeOK = iota
ExitCodeError
)

type CLI struct {
outStream io.Writer
errStream io.Writer
}

func (c *CLI) Run(args []string) int {
var app = kingpin.New("gocha", "Random strings generater based on a pattern.")

var pattern = app.Arg("pattern", "Regular expression").Required().String()
var num = app.Flag("number-of-lines", "Number of lines").Short('n').Int()

var exitCode int
kingpin.CommandLine.Writer(c.errStream).Terminate(func(i int) {
exitCode = i
})
kingpin.MustParse(app.Parse(args[1:]))

if exitCode == ExitCodeError {
return ExitCodeError
}

err, g := gocha.New(*pattern)

if err != nil {
fmt.Fprintf(c.errStream, "gocha: %v\n", err.Error())
return ExitCodeError
}

if *num <= 0 {
*num = 1
}

for i := 0; i < *num; i++ {
fmt.Fprintln(c.outStream, g.Gen())
}

return ExitCodeOK
}
73 changes: 73 additions & 0 deletions cmd/gocha/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bytes"
"fmt"
"strings"
"testing"
)

func TestRun(t *testing.T) {
t.Run("ExitCodeOK", func(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
args := strings.Split("gocha -n 1 '[a]{3}'", " ")
status := cli.Run(args)

if status != ExitCodeOK {
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeOK)
}

expected := fmt.Sprintf("")
if errStream.String() != expected {
t.Errorf("Output=%q, want %q", errStream.String(), expected)
}

expected = fmt.Sprintf("'aaa'\n")
if outStream.String() != expected {
t.Errorf("Output=%q, want %q", outStream.String(), expected)
}
})

t.Run("ExitCodeError", func(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
args := strings.Split("gocha -n 1 '.{100000}'", " ")
status := cli.Run(args)

if status != ExitCodeError {
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeOK)
}

expected := fmt.Sprintf("gocha: error parsing regexp: invalid repeat count: `{100000}`\n")
if errStream.String() != expected {
t.Errorf("Output=%q, want %q", errStream.String(), expected)
}

expected = fmt.Sprintf("")
if outStream.String() != expected {
t.Errorf("Output=%q, want %q", outStream.String(), expected)
}
})

t.Run("RequirePattern", func(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
args := strings.Split("gocha -n 1", " ")
status := cli.Run(args)

if status != ExitCodeError {
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeError)
}

expected := fmt.Sprintf("gocha.test: error: required argument 'pattern' not provided, try --help\n")
if errStream.String() != expected {
t.Errorf("Output=%q, want %q", errStream.String(), expected)
}

expected = fmt.Sprintf("")
if outStream.String() != expected {
t.Errorf("Output=%q, want %q", outStream.String(), expected)
}
})
}
43 changes: 1 addition & 42 deletions cmd/gocha/main.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
package main

import (
"fmt"
"io"
"os"

"github.com/t-mrt/gocha"
"gopkg.in/alecthomas/kingpin.v2"
)

const (
ExitCodeOK = iota
ExitCodeError
)

type CLI struct {
outStream io.Writer
errStream io.Writer
}

func (c *CLI) Run(args []string) int {

var pattern = kingpin.Arg("pattern", "Regular expression").Required().String()
var num = kingpin.Flag("number-of-lines", "Number of lines").Short('n').Int()
kingpin.Parse()

err, g := gocha.New(*pattern)

if err != nil {
fmt.Fprintln(c.outStream, "gocha: error: Invalid regular expression")
return ExitCodeError
}

if *num <= 0 {
*num = 1
}

for i := 0; i < *num; i++ {
fmt.Fprintln(c.outStream, g.Gen())
}

return ExitCodeOK
}
import "os"

func main() {
cli := &CLI{outStream: os.Stdout, errStream: os.Stderr}
Expand Down

0 comments on commit bd8de43

Please sign in to comment.