From 11319bfe205da4ecef6d5aff70e1b644c1292b20 Mon Sep 17 00:00:00 2001 From: Takumi Kanada Date: Sun, 20 Mar 2022 23:28:17 +0900 Subject: [PATCH] Fix test --- internal/cmd/cmd/cmd_test.go | 22 ++++-------------- internal/cmd/repl/repl_test.go | 6 ++--- internal/cmd/setup/setup_test.go | 24 ++++--------------- internal/config/config_test.go | 3 +++ internal/deepl/deepl_test.go | 28 ++++++++-------------- internal/deepl/translate_test.go | 31 +++++++------------------ internal/validation/validation.go | 4 ++-- internal/validation/validation_test.go | 32 +++++++++++++++++--------- test/helper.go | 18 --------------- 9 files changed, 54 insertions(+), 114 deletions(-) delete mode 100644 test/helper.go diff --git a/internal/cmd/cmd/cmd_test.go b/internal/cmd/cmd/cmd_test.go index 705056f..0604493 100644 --- a/internal/cmd/cmd/cmd_test.go +++ b/internal/cmd/cmd/cmd_test.go @@ -8,6 +8,7 @@ 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) { @@ -15,42 +16,36 @@ func TestRun(t *testing.T) { 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, }, } @@ -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) }) } } diff --git a/internal/cmd/repl/repl_test.go b/internal/cmd/repl/repl_test.go index 2cfa90e..32a5f02 100644 --- a/internal/cmd/repl/repl_test.go +++ b/internal/cmd/repl/repl_test.go @@ -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) { @@ -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()) }) } } diff --git a/internal/cmd/setup/setup_test.go b/internal/cmd/setup/setup_test.go index c64a08d..19f8b4e 100644 --- a/internal/cmd/setup/setup_test.go +++ b/internal/cmd/setup/setup_test.go @@ -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", }, @@ -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) }) } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index f09d24b..7cb2630 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "path/filepath" "testing" @@ -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) } diff --git a/internal/deepl/deepl_test.go b/internal/deepl/deepl_test.go index 14f54f9..10c8441 100644 --- a/internal/deepl/deepl_test.go +++ b/internal/deepl/deepl_test.go @@ -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()) }) } @@ -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)) } } diff --git a/internal/deepl/translate_test.go b/internal/deepl/translate_test.go index aad384b..5a09d2e 100644 --- a/internal/deepl/translate_test.go +++ b/internal/deepl/translate_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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.") - } -} diff --git a/internal/validation/validation.go b/internal/validation/validation.go index da9b8f5..079fa85 100644 --- a/internal/validation/validation.go +++ b/internal/validation/validation.go @@ -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 { diff --git a/internal/validation/validation_test.go b/internal/validation/validation_test.go index f124c1d..7ebd343 100644 --- a/internal/validation/validation_test.go +++ b/internal/validation/validation_test.go @@ -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()) + } +} diff --git a/test/helper.go b/test/helper.go deleted file mode 100644 index c5bb81c..0000000 --- a/test/helper.go +++ /dev/null @@ -1,18 +0,0 @@ -package test - -import ( - "testing" -) - -func AssertError(t *testing.T, got, want error) { - t.Helper() - if got != want { - t.Errorf("got error is %q, want %q", got, want) - } - if got == nil { - if want == nil { - return - } - t.Fatal("expected to get an error.") - } -}