Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
candy12t committed Mar 23, 2022
1 parent 7285f88 commit 11319bf
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 114 deletions.
22 changes: 4 additions & 18 deletions internal/cmd/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,44 @@ import (

"github.com/candy12t/deepl-cli/internal/build"
"github.com/candy12t/deepl-cli/internal/config"
"github.com/stretchr/testify/assert"
)

func TestRun(t *testing.T) {
tests := []struct {
name string
args []string
wantOut string
wantErr string
wantExitCode exitCode
}{
{
name: "--version",
args: strings.Split("deepl-cli --version", " "),
wantOut: fmt.Sprintf("deepl-cli version %s\n", build.Version),
wantErr: "",
wantExitCode: exitOK,
},
{
name: "-v",
args: strings.Split("deepl-cli -v", " "),
wantOut: fmt.Sprintf("deepl-cli version %s\n", build.Version),
wantErr: "",
wantExitCode: exitOK,
},
{
name: "unknown command",
args: strings.Split("deepl-cli hoge", " "),
wantOut: fmt.Sprintf("unknown command %q for \"deepl-cli\"\n", "hoge"),
wantErr: "",
wantExitCode: exitOK,
},
{
name: "repl",
args: strings.Split("deepl-cli repl", " "),
wantOut: fmt.Sprintf("Translate text from %s to %s\n>> ", "EN", "JA"),
wantErr: "",
wantExitCode: exitOK,
},
{
name: "repl",
name: "repl with options",
args: strings.Split("deepl-cli repl -s JA -t EN", " "),
wantOut: fmt.Sprintf("Translate text from %s to %s\n>> ", "JA", "EN"),
wantErr: "",
wantExitCode: exitOK,
},
}
Expand All @@ -70,17 +65,8 @@ func TestRun(t *testing.T) {
cli := NewCLI(inStream, outStream, errStream, conf)
code := cli.Run(tt.args)

if outStream.String() != tt.wantOut {
t.Errorf("got %q, want %q", outStream.String(), tt.wantOut)
}

if errStream.String() != tt.wantErr {
t.Errorf("got %q, want %q", errStream.String(), tt.wantErr)
}

if code != tt.wantExitCode {
t.Errorf("got %d, want %d", code, tt.wantExitCode)
}
assert.Equal(t, tt.wantOut, outStream.String())
assert.Equal(t, tt.wantExitCode, code)
})
}
}
6 changes: 2 additions & 4 deletions internal/cmd/repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/candy12t/deepl-cli/internal/deepl"
"github.com/candy12t/deepl-cli/internal/deepl/mock_deepl"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestRepl(t *testing.T) {
Expand Down Expand Up @@ -77,10 +78,7 @@ func TestRepl(t *testing.T) {
out := new(bytes.Buffer)
Repl(mockClient, tt.args.sourceLang, tt.args.targetLang, bytes.NewBufferString(tt.args.inputText), out)

got := out.String()
if got != tt.want {
t.Fatalf("repl output %q, want %q", got, tt.want)
}
assert.Equal(t, tt.want, out.String())
})
}
}
24 changes: 5 additions & 19 deletions internal/cmd/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,19 @@ import (
"testing"

"github.com/candy12t/deepl-cli/internal/config"
"github.com/stretchr/testify/assert"
)

func TestSetup(t *testing.T) {
tests := []struct {
name string
input []string
want *config.DeepLCLIConfig
want config.DeepLCLIConfig
}{
{
name: "setup",
input: []string{"test-auth-key", "fred", "EN", "JA"},
want: &config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: "test-auth-key",
},
DefaultLanguage: config.DefaultLanguage{
SourceLanguage: "EN",
TargetLanguage: "JA",
},
},
},
{
name: "setup validate",
input: []string{"test-auth-key", "hoge", "free", "EN", "JA"},
want: &config.DeepLCLIConfig{
input: []string{"test-auth-key", "EN", "JA"},
want: config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: "test-auth-key",
},
Expand All @@ -47,9 +35,7 @@ func TestSetup(t *testing.T) {
in := bytes.NewBufferString(strings.Join(tt.input, "\n"))
out := new(bytes.Buffer)
got := PromptSetup(in, out)
if *got != *tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, *got)
})
}
}
3 changes: 3 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"path/filepath"
"testing"

Expand Down Expand Up @@ -37,4 +38,6 @@ func TestWriteConfig(t *testing.T) {
filename := filepath.Join(t.TempDir(), "config.yaml")
err := conf.writeDeepLCLIConfig(filename)
assert.NoError(t, err)
data, _ := parseDeepLCLIConfigFile(filename)
fmt.Println(data)
}
28 changes: 9 additions & 19 deletions internal/deepl/deepl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@ import (
"net/url"
"testing"

"github.com/candy12t/deepl-cli/test"
"github.com/stretchr/testify/assert"
)

const defaultBaseURL = "https://api.deepl.com/v2"
const testAuthKey = "test-auth-key"

func TestNewClient(t *testing.T) {
t.Run("success new deepl client", func(t *testing.T) {
c, err := NewClient(testAuthKey)
test.AssertError(t, err, nil)

if got, want := c.BaseURL.String(), defaultBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
if assert.NoError(t, err) {
assert.Equal(t, "https://api.deepl.com/v2", c.BaseURL.String())
}
})

t.Run("failed new deepl client because missing deepl authkey", func(t *testing.T) {
_, err := NewClient("")
test.AssertError(t, err, ErrMissingAuthKey)
assert.EqualError(t, err, ErrMissingAuthKey.Error())
})
}

Expand All @@ -42,26 +39,19 @@ func setup() (*Client, *http.ServeMux, func()) {

func testHeader(t *testing.T, r *http.Request, header string, want string) {
t.Helper()
if got := r.Header.Get(header); got != want {
t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want)
}
assert.Equal(t, want, r.Header.Get(header))
}

func testMethod(t *testing.T, r *http.Request, want string) {
t.Helper()
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
assert.Equal(t, want, r.Method)
}

func testBody(t *testing.T, r *http.Request, want string) {
t.Helper()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != want {
t.Errorf("request Body is %s, want %s", got, want)
data, err := ioutil.ReadAll(r.Body)
if assert.NoError(t, err) {
assert.Equal(t, want, string(data))
}
}

Expand Down
31 changes: 8 additions & 23 deletions internal/deepl/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"
"net/http"
"path"
"reflect"
"testing"

"github.com/candy12t/deepl-cli/test"
"github.com/stretchr/testify/assert"
)

func TestTranslate(t *testing.T) {
Expand All @@ -32,8 +31,9 @@ func TestTranslate(t *testing.T) {
ctx := context.Background()
got, err := client.Translate(ctx, "hello", "EN", "JA")

test.AssertError(t, err, nil)
testTranslate(t, got, want)
if assert.NoError(t, err) {
assert.Equal(t, got, want)
}
})

t.Run("failed translate because unspecified target language", func(t *testing.T) {
Expand All @@ -56,10 +56,9 @@ func TestTranslate(t *testing.T) {
want := HTTPError{StatusCode: http.StatusBadRequest, RequestURL: u.String(), Message: `"Value for 'target_lang' not supported."`}

ctx := context.Background()
got, err := client.Translate(ctx, "hello", "EN", "")
_, err := client.Translate(ctx, "hello", "EN", "")

test.AssertError(t, err, want)
testTranslate(t, got, nil)
assert.EqualError(t, err, want.Error())
})

t.Run("failed translate because incorrect DeepL AuthKey", func(t *testing.T) {
Expand All @@ -80,22 +79,8 @@ func TestTranslate(t *testing.T) {
want := HTTPError{StatusCode: http.StatusForbidden, RequestURL: u.String(), Message: "403 Forbidden"}

ctx := context.Background()
got, err := client.Translate(ctx, "hello", "EN", "JA")
_, err := client.Translate(ctx, "hello", "EN", "JA")

test.AssertError(t, err, want)
testTranslate(t, got, nil)
assert.EqualError(t, err, want.Error())
})
}

func testTranslate(t *testing.T, got, want *Translate) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("translated text is %s, want %s", got, want)
}
if got == nil {
if want == nil {
return
}
t.Fatal("expected to get an translate response.")
}
}
4 changes: 2 additions & 2 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package validation

import (
"fmt"
"errors"
"strings"
)

var ErrTextLength = fmt.Errorf("Error: text length is 0")
var ErrTextLength = errors.New("Error: text length is 0")

func ValidText(text string) (string, error) {
if err := validTextLength(trimSpace(text)); err != nil {
Expand Down
32 changes: 21 additions & 11 deletions internal/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@ package validation
import (
"testing"

"github.com/candy12t/deepl-cli/test"
"github.com/stretchr/testify/assert"
)

func TestVaildText(t *testing.T) {
tests := []struct {
input string
wantText string
wantErr error
}{
{"\thello\t", "hello", nil},
{" hello world ", "hello world", nil},
{"\t hoge fuga \t", "hoge fuga", nil},
{"\t\n", "", ErrTextLength},
{" ", "", ErrTextLength},
{"\thello\t", "hello"},
{" hello world ", "hello world"},
{"\t hoge fuga \t", "hoge fuga"},
}

for _, tt := range tests {
text, err := ValidText(tt.input)
test.AssertError(t, err, tt.wantErr)

if tt.wantText != text {
t.Fatalf("validText(%q) returned %q, want %q", tt.input, text, tt.wantText)
if assert.NoError(t, err) {
assert.Equal(t, tt.wantText, text)
}
}
}

func TestInVaildText(t *testing.T) {
tests := []struct {
input string
wantErr error
}{
{"\t\n", ErrTextLength},
{" ", ErrTextLength},
}

for _, tt := range tests {
_, err := ValidText(tt.input)
assert.EqualError(t, err, tt.wantErr.Error())
}
}
18 changes: 0 additions & 18 deletions test/helper.go

This file was deleted.

0 comments on commit 11319bf

Please sign in to comment.