-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add test case for internal/errors/file.go (#893)
* feat: add test case and comment Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com> * fix: test case name Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com> * Update internal/errors/file_test.go Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com> * Update internal/errors/file_test.go Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com> * Update internal/errors/file_test.go Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com> * fix: apply suggestion Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com> * 🤖 Update license headers / Format go codes and yaml files Signed-off-by: vdaas-ci <ci@vdaas.org> Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com> Co-authored-by: vdaas-ci <ci@vdaas.org>
- Loading branch information
1 parent
8adf43c
commit ab1ec89
Showing
2 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// | ||
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt ) | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
package errors | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestErrWatchDirNotFound(t *testing.T) { | ||
type want struct { | ||
want error | ||
} | ||
type test struct { | ||
name string | ||
want want | ||
checkFunc func(want, error) error | ||
beforeFunc func() | ||
afterFunc func() | ||
} | ||
defaultCheckFunc := func(w want, got error) error { | ||
if !Is(got, w.want) { | ||
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
{ | ||
name: "return ErrWatchDirNotFound error", | ||
want: want{ | ||
want: New("fs watcher watch dir not found"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
if test.beforeFunc != nil { | ||
test.beforeFunc() | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc() | ||
} | ||
if test.checkFunc == nil { | ||
test.checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := ErrWatchDirNotFound | ||
if err := test.checkFunc(test.want, got); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestErrFileAlreadyExists(t *testing.T) { | ||
type args struct { | ||
path string | ||
} | ||
type want struct { | ||
want error | ||
} | ||
type test struct { | ||
name string | ||
args args | ||
want want | ||
checkFunc func(want, error) error | ||
beforeFunc func(args) | ||
afterFunc func(args) | ||
} | ||
defaultCheckFunc := func(w want, got error) error { | ||
if !Is(got, w.want) { | ||
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
{ | ||
name: "return ErrFileAlreadyExists error with the file path is 'metadata.json'", | ||
args: args{ | ||
path: "metadata.json", | ||
}, | ||
want: want{ | ||
want: New("file already exists: metadata.json"), | ||
}, | ||
}, | ||
{ | ||
name: "return ErrFileAlreadyExists error with the file path is empty", | ||
args: args{ | ||
path: "", | ||
}, | ||
want: want{ | ||
want: New("file already exists: "), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
if test.beforeFunc != nil { | ||
test.beforeFunc(test.args) | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc(test.args) | ||
} | ||
if test.checkFunc == nil { | ||
test.checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := ErrFileAlreadyExists(test.args.path) | ||
if err := test.checkFunc(test.want, got); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestErrPathNotSpecified(t *testing.T) { | ||
type want struct { | ||
want error | ||
} | ||
type test struct { | ||
name string | ||
want want | ||
checkFunc func(want, error) error | ||
beforeFunc func() | ||
afterFunc func() | ||
} | ||
defaultCheckFunc := func(w want, got error) error { | ||
if !Is(got, w.want) { | ||
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
{ | ||
name: "return ErrPathNotSpecified error", | ||
want: want{ | ||
want: New("the path is not specified"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
if test.beforeFunc != nil { | ||
test.beforeFunc() | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc() | ||
} | ||
if test.checkFunc == nil { | ||
test.checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := ErrPathNotSpecified | ||
if err := test.checkFunc(test.want, got); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} |