Skip to content

Commit

Permalink
Fix golangci-lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed May 2, 2022
1 parent 3a94aee commit 1de7283
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 15 deletions.
6 changes: 4 additions & 2 deletions checkdigit.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ func NewJAN13() Provider {
// NewITF returns a new Provider that implemented GTIN-14 calculator.
func NewITF() Provider {
return &gtin{
digit: 14,
digit: 14,
posCorr: false,
}
}

Expand All @@ -140,6 +141,7 @@ func NewUPC() Provider {
// NewSSCC returns a new Provider that implemented GTIN-18 calculator.
func NewSSCC() Provider {
return &gtin{
digit: 18,
digit: 18,
posCorr: false,
}
}
24 changes: 24 additions & 0 deletions checkdigit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,107 @@ import (
)

func TestNewLuhn(t *testing.T) {
t.Parallel()

p := checkdigit.NewLuhn()
if p == nil {
t.Error("failed to generate luhn provider")
}
}

func TestNewDamm(t *testing.T) {
t.Parallel()

p := checkdigit.NewDamm()
if p == nil {
t.Error("failed to generate damm provider")
}
}

func TestNewVerhoeff(t *testing.T) {
t.Parallel()

p := checkdigit.NewVerhoeff()
if p == nil {
t.Error("failed to generate verhoeff provider")
}
}

func TestNewISBN10(t *testing.T) {
t.Parallel()

v := checkdigit.NewISBN10()
if v == nil {
t.Error("failed to generate isbn10 verifier")
}
}

func TestNewISBN13(t *testing.T) {
t.Parallel()

p := checkdigit.NewISBN13()
if p == nil {
t.Error("failed to generate isbn13 provider")
}
}

func TestNewEAN8(t *testing.T) {
t.Parallel()

p := checkdigit.NewEAN8()
if p == nil {
t.Error("failed to generate ean8 provider")
}
}

func TestNewEAN13(t *testing.T) {
t.Parallel()

p := checkdigit.NewEAN13()
if p == nil {
t.Error("failed to generate ean13 provider")
}
}

func TestNewJAN8(t *testing.T) {
t.Parallel()

p := checkdigit.NewJAN8()
if p == nil {
t.Error("failed to generate jan8 provider")
}
}

func TestNewJAN13(t *testing.T) {
t.Parallel()

p := checkdigit.NewJAN13()
if p == nil {
t.Error("failed to generate jan13 provider")
}
}

func TestNewITF(t *testing.T) {
t.Parallel()

p := checkdigit.NewITF()
if p == nil {
t.Error("failed to generate itf provider")
}
}

func TestNewUPC(t *testing.T) {
t.Parallel()

p := checkdigit.NewUPC()
if p == nil {
t.Error("failed to generate upc provider")
}
}

func TestNewSSCC(t *testing.T) {
t.Parallel()

p := checkdigit.NewSSCC()
if p == nil {
t.Error("failed to generate sscc provider")
Expand Down
14 changes: 7 additions & 7 deletions cmd/checkdigit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func main() {
flag.Usage = usage

var pname string
flag.StringVar(&pname, "p", "luhn", "")
flag.StringVar(&pname, "provider", "luhn", "")
flag.StringVar(&pname, "provider", "luhn", "")
flag.Parse()

p := takeProvider(pname)
if p == nil {
provider := takeProvider(pname)
if provider == nil {
fmt.Fprintf(os.Stderr, "Unimplemented %s provider\n", pname)
os.Exit(errExitCode)
}
Expand All @@ -36,19 +36,20 @@ func main() {

switch args[0] {
case generateCmd:
if err := generate(p, args[1]); err != nil {
if err := generate(provider, args[1]); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(errExitCode)
}
case verifyCmd:
if !verify(p, args[1]) {
if !verify(provider, args[1]) {
os.Exit(errExitCode)
}
default:
flag.Usage()
}
}

//nolint: cyclop
func takeProvider(name string) checkdigit.Provider {
switch strings.ToLower(strings.ReplaceAll(name, "-", "")) {
case "luhn":
Expand Down Expand Up @@ -114,8 +115,7 @@ COMMANDS:
help Shows a list of commands or help for one command
GLOBAL OPTIONS
-p, --provider Select use provider [default: luhn]
`
-p, --provider Select use provider [default: luhn]`
fmt.Fprintln(os.Stderr, usage)
os.Exit(2)
}
10 changes: 10 additions & 0 deletions damm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestDamm_Verify(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out bool
Expand Down Expand Up @@ -36,7 +38,10 @@ func TestDamm_Verify(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

ret := checkdigit.NewDamm().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
Expand All @@ -46,6 +51,8 @@ func TestDamm_Verify(t *testing.T) {
}

func TestDamm_Generate(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out int
Expand Down Expand Up @@ -86,7 +93,10 @@ func TestDamm_Generate(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

r, err := checkdigit.NewDamm().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
Expand Down
10 changes: 10 additions & 0 deletions gtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestGtin_Verify(t *testing.T) {
t.Parallel()

cases := map[string]struct {
fn func() checkdigit.Provider
in string
Expand Down Expand Up @@ -55,7 +57,10 @@ func TestGtin_Verify(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

ret := c.fn().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
Expand All @@ -65,6 +70,8 @@ func TestGtin_Verify(t *testing.T) {
}

func TestGtin_Generate(t *testing.T) {
t.Parallel()

cases := map[string]struct {
fn func() checkdigit.Provider
in string
Expand Down Expand Up @@ -120,7 +127,10 @@ func TestGtin_Generate(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

r, err := c.fn().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
Expand Down
10 changes: 4 additions & 6 deletions isbn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ func (i10 isbn10) Verify(code string) bool {

sum, multiply := 0, 10
for _, n := range code {

var d int
var digit int
switch {
case n == 'X':
d = 10
digit = 10
case isNotNumber(n):
return false
default:
d = int(n - '0')
digit = int(n - '0')
}

sum += multiply * d
sum += multiply * digit
multiply--
}

Expand All @@ -41,7 +40,6 @@ func (i10 *isbn10) Generate(seed string) (int, error) {
sum, multiply := 0, 10

for _, n := range seed {

if isNotNumber(n) {
return 0, ErrInvalidArgument
}
Expand Down
20 changes: 20 additions & 0 deletions isbn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestIsbn10_Verify(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out bool
Expand Down Expand Up @@ -42,7 +44,10 @@ func TestIsbn10_Verify(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

ret := checkdigit.NewISBN10().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
Expand All @@ -52,6 +57,8 @@ func TestIsbn10_Verify(t *testing.T) {
}

func TestIsbn10_Generate(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out int
Expand Down Expand Up @@ -92,7 +99,10 @@ func TestIsbn10_Generate(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

r, err := checkdigit.NewISBN10().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
Expand All @@ -105,6 +115,8 @@ func TestIsbn10_Generate(t *testing.T) {
}

func TestIsbn13_Verify(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out bool
Expand Down Expand Up @@ -140,7 +152,10 @@ func TestIsbn13_Verify(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

ret := checkdigit.NewISBN13().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
Expand All @@ -150,6 +165,8 @@ func TestIsbn13_Verify(t *testing.T) {
}

func TestIsbn13_Generate(t *testing.T) {
t.Parallel()

cases := map[string]struct {
in string
out int
Expand Down Expand Up @@ -190,7 +207,10 @@ func TestIsbn13_Generate(t *testing.T) {

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

r, err := checkdigit.NewISBN13().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
Expand Down
Loading

0 comments on commit 1de7283

Please sign in to comment.