Permalink
Browse files

issue #26: improve tests

  • Loading branch information...
kamilsk committed Apr 30, 2018
1 parent e177522 commit 61587e9e4116b08cc44314f292e759f161e4e1c2
@@ -10,6 +10,10 @@ cmd-completion:
cd $(PKG_DIR) && go run $(GO_FILES) completion bash
cd $(PKG_DIR) && go run $(GO_FILES) completion zsh

.PHONY: cmd-completion-help
cmd-completion-help:
cd $(PKG_DIR) && go run $(GO_FILES) completion --help

.PHONY: cmd-urls
cmd-urls:
cd $(PKG_DIR) && go run -race $(GO_FILES) urls -v https://kamil.samigullin.info/
@@ -0,0 +1,51 @@
package cmd

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCompletion(t *testing.T) {
buf := bytes.NewBuffer(nil)
cmd := completionCmd
cmd.SetOutput(buf)
defer cmd.SetOutput(nil)
{
tests := []struct {
name string
args []string
checker func(assert.TestingT, error, ...interface{}) bool
}{
{"empty args", nil, assert.Error},
{"args with invalid format", []string{"shell"}, assert.Error},
{"args with valid format (bash)", []string{bashFormat}, assert.NoError},
{"args with valid format (zsh) ", []string{zshFormat}, assert.NoError},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
tc.checker(t, cmd.Args(cmd, tc.args))
})
}
}
{
tests := []struct {
name string
format string
expected string
}{
{"Bash", bashFormat, "# bash completion for check"},
{"Zsh", zshFormat, "#compdef check"},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
buf.Reset()
assert.NoError(t, cmd.RunE(cmd, []string{tc.format}))
assert.Contains(t, buf.String(), tc.expected)
})
}
}
}

This file was deleted.

Oops, something went wrong.
@@ -1,83 +1,32 @@
package cmd

import (
"bytes"
"fmt"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"html/template"
"net/http"
"net/http/httptest"
)

func TestCompletion(t *testing.T) {
buf := bytes.NewBuffer(nil)
completionCmd.SetOutput(buf)
defer completionCmd.SetOutput(nil)
{
assert.Error(t, completionCmd.Args(completionCmd, nil))
assert.Error(t, completionCmd.Args(completionCmd, []string{"shell"}))
assert.NoError(t, completionCmd.Args(completionCmd, []string{bashFormat}))
assert.NoError(t, completionCmd.Args(completionCmd, []string{zshFormat}))
}
{
buf.Reset()
assert.NoError(t, completionCmd.RunE(completionCmd, []string{bashFormat}))
assert.Contains(t, buf.String(), "# bash completion for check")
}
{
buf.Reset()
assert.NoError(t, completionCmd.RunE(completionCmd, []string{zshFormat}))
assert.Contains(t, buf.String(), "#compdef check")
}
}

func TestURLs(t *testing.T) {
buf := bytes.NewBuffer(nil)
site, closer := site()
urlsCmd.SetOutput(buf)
defer closer()
defer urlsCmd.SetOutput(nil)
{
buf.Reset()
assert.NoError(t, urlsCmd.RunE(urlsCmd, []string{site.URL + "/"}))
assert.Contains(t, buf.String(), fmt.Sprintf("[200] %s/", site.URL))
}
{
buf.Reset()
verbose := urlsCmd.Flag("verbose")
verbose.Value.Set("true")
assert.NoError(t, urlsCmd.RunE(urlsCmd, []string{site.URL + "/"}))
verbose.Value.Set(verbose.DefValue)
}
}
var (
html = `
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Links</title>
</head>
<body>
<a href="{{ .Href }}">{{ .Text }}</a>
</body>
`
tpl = template.Must(template.New("links").Parse(html))
)

func Test_client(t *testing.T) {
tests := []struct {
name string
cmd func() *cobra.Command
expected string
}{
{"without parent", func() *cobra.Command {
return &cobra.Command{Use: "child"}
}, "child"},
{"without version", func() *cobra.Command {
parent := &cobra.Command{Use: "parent"}
child := &cobra.Command{Use: "child"}
parent.AddCommand(child)
return child
}, "parent"},
{"with version", func() *cobra.Command {
parent := &cobra.Command{Use: "parent"}
child := &cobra.Command{Use: "child"}
version := &cobra.Command{Use: "version", Version: "ver."}
parent.AddCommand(child, version)
return child
}, "parent/ver."},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, tc.expected, client(tc.cmd()))
})
}
func site() (server *httptest.Server, closer func()) {
main := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
tpl.Execute(rw, struct {
Href string
Text string
}{"/", "Home"})
}))
return main, main.Close
}
@@ -0,0 +1,37 @@
package cmd

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func Test_client(t *testing.T) {
tests := []struct {
name string
cmd func() *cobra.Command
expected string
}{
{"without parent", func() *cobra.Command { return &cobra.Command{Use: "child"} }, "child"},
{"without version", func() *cobra.Command {
parent := &cobra.Command{Use: "parent"}
child := &cobra.Command{Use: "child"}
parent.AddCommand(child)
return child
}, "parent"},
{"with version", func() *cobra.Command {
parent := &cobra.Command{Use: "parent"}
child := &cobra.Command{Use: "child"}
version := &cobra.Command{Use: "version", Version: "ver."}
parent.AddCommand(child, version)
return child
}, "parent/ver."},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, tc.expected, client(tc.cmd()))
})
}
}
@@ -0,0 +1,30 @@
package cmd

import (
"bytes"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestURLs(t *testing.T) {
buf := bytes.NewBuffer(nil)
cmd := urlsCmd
cmd.SetOutput(buf)
defer cmd.SetOutput(nil)
site, closer := site()
defer closer()
{
buf.Reset()
assert.NoError(t, cmd.RunE(cmd, []string{site.URL + "/"}))
assert.Contains(t, buf.String(), fmt.Sprintf("[200] %s/", site.URL))
}
{
buf.Reset()
verbose := cmd.Flag("verbose")
verbose.Value.Set("true")
assert.NoError(t, cmd.RunE(cmd, []string{site.URL + "/"}))
verbose.Value.Set(verbose.DefValue)
}
}
@@ -1 +1,52 @@
package errors_test

import (
"testing"

"github.com/kamilsk/check/errors"
"github.com/stretchr/testify/assert"
)

func TestRecover(t *testing.T) {
tests := []struct {
name string
panic func()
checker func(assert.TestingT, interface{}, ...interface{}) bool
}{
{"error with stack trace", func() { panic(errors.Errorf("panic")) }, assert.NotEmpty},
{"error without stack trace", func() { panic(errors.Simple("panic")) }, assert.Empty},
{"not error panic", func() { panic("panic") }, assert.Empty},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
var err error
assert.NotPanics(t, func() {
defer errors.Recover(&err)
tc.panic()
})
assert.Error(t, err)
tc.checker(t, errors.StackTrace(err))
})
}
}

func TestWrapper(t *testing.T) {
tests := []struct {
name string
wrap func(error) error
}{
{"wrap by WithMessage", func(err error) error { return errors.WithMessage(err, "wrapped") }},
{"wrap by Wrapf", func(err error) error { return errors.Wrapf(err, "wrapped") }},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
var err error
err = tc.wrap(errors.Simple("test"))
assert.Error(t, err)
err = tc.wrap(nil)
assert.NoError(t, err)
})
}
}
@@ -132,10 +132,8 @@ var echoLinks = func(site1, site2 string) func(http.ResponseWriter, *http.Reques

{Href: "#anchor", Text: "anchor"},
{Href: "/#/tab", Text: "some tab"},
//issue#35, fixes issue#30
{Href: "mailto:test@my.email", Text: "test@my.email"},
{Href: "tel:+01234567", Text: "+01234567"},
// other bad urls
{Href: ":bad", Text: "something bad"},
}
tpl.Execute(rw, links)
@@ -16,14 +16,14 @@ func TestPrinter(t *testing.T) {
name string
printer func() *availability.Printer
report func() availability.Reporter
hasError bool
checker func(assert.TestingT, error, ...interface{}) bool
expected string
}{
{
"empty report",
func() *availability.Printer { return availability.NewPrinter() },
func() availability.Reporter { return nil },
true,
assert.Error,
"",
},
{
@@ -38,7 +38,7 @@ func TestPrinter(t *testing.T) {
m.On("Sites").Return(pipe)
return m
},
false,
assert.NoError,
`report ":bad" has error`,
},
{
@@ -53,7 +53,7 @@ func TestPrinter(t *testing.T) {
m.On("Sites").Return(pipe)
return m
},
false,
assert.NoError,
"- [0] something happened `<nil>`",
},
{
@@ -89,19 +89,15 @@ func TestPrinter(t *testing.T) {
m.On("Sites").Return(pipe)
return m
},
false,
assert.NoError,
"[200] https://kamil.samigullin.info/",
},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
buf.Reset()
var checker = assert.NoError
if tc.hasError {
checker = assert.Error
}
checker(t, tc.printer().For(tc.report()).Print())
tc.checker(t, tc.printer().For(tc.report()).Print())
assert.Contains(t, buf.String(), tc.expected)
})
}
File renamed without changes.

0 comments on commit 61587e9

Please sign in to comment.