Skip to content

Commit

Permalink
Rewrite tests using testify/require.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juraj Bubniak committed Jan 2, 2020
1 parent 05cd555 commit 51292da
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 257 deletions.
30 changes: 9 additions & 21 deletions bban/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package bban

import (
"testing"

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

var (
Expand Down Expand Up @@ -55,9 +57,7 @@ func TestCharTypeValidate(t *testing.T) {
for _, tc := range charTypeTests {
t.Run(tc.in, func(t *testing.T) {
result := tc.charType.Validate(tc.in)
if tc.want != result {
t.Errorf("expected %v got %v", tc.want, result)
}
require.Equal(t, tc.want, result)
})
}
}
Expand All @@ -67,9 +67,7 @@ func TestPartValidate(t *testing.T) {
t.Run(tc.val, func(t *testing.T) {
part := NewPart(tc.length, tc.charType, tc.entryType)
result := part.Validate(tc.val)
if tc.want != result {
t.Errorf("expected %v got %v", tc.want, result)
}
require.Equal(t, tc.want, result)
})
}
}
Expand All @@ -78,12 +76,8 @@ func TestNewPart(t *testing.T) {
for _, tc := range newPartTests {
t.Run(tc.want.String(), func(t *testing.T) {
part := tc.new(4, Num)
if tc.want != part.EntryType {
t.Errorf("expected %v got %v", tc.want, part.EntryType)
}
if part.String() != part.EntryType.String() {
t.Errorf("expected %v got %v", part.String(), part.EntryType.String())
}
require.Equal(t, tc.want, part.EntryType)
require.Equal(t, part.String(), part.EntryType.String())
})
}
}
Expand All @@ -92,21 +86,15 @@ func TestPartString(t *testing.T) {
for _, tc := range newPartTests {
t.Run(tc.want.String(), func(t *testing.T) {
part := tc.new(3, AlphaNum)
if part.String() != part.EntryType.String() {
t.Errorf("expected %v got %v", part.String(), part.EntryType.String())
}
require.Equal(t, part.String(), part.EntryType.String())
})
}
}

func TestStructureLength(t *testing.T) {
st1 := NewStructure()
if 0 != st1.Length() {
t.Errorf("expected 0 got %v", st1.Length())
}
require.Equal(t, 0, st1.Length())

st2 := NewStructure(Part{Length: 2}, Part{Length: 4})
if 6 != st2.Length() {
t.Errorf("expected 0 got %v", st2.Length())
}
require.Equal(t, 6, st2.Length())
}
88 changes: 22 additions & 66 deletions country/country_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,44 @@ package country

import (
"testing"

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

func TestCountryExists(t *testing.T) {
code := "SK"
if ok := Exists(code); !ok {
t.Errorf("expected country %v to exist", code)
}
require.True(t, Exists("SK"))
}

func TestCountryNotPresent(t *testing.T) {
code := "YY"
ok := Exists(code)
if ok {
t.Errorf("country %v should not exist", code)
}
require.False(t, Exists("YY"))
}

func TestValidCountry(t *testing.T) {
alpha2 := "GB"
alpha3 := "GBR"
name := "United Kingdom"
c, ok := Get(alpha2)
if !ok {
t.Errorf("expected country %v to exist", alpha2)
}
if alpha2 != c.Alpha2Code {
t.Errorf("expected %v got %v", alpha2, c.Alpha2Code)
}
if alpha3 != c.Alpha3Code {
t.Errorf("expected %v got %v", alpha3, c.Alpha3Code)
}
if name != c.Name {
t.Errorf("expected %v got %v", name, c.Name)
}
if c.Name != c.String() {
t.Errorf("expected %v got %v", c.Name, c.String())
}
c, ok := Get("GB")
require.True(t, ok)
require.Equal(t, "GB", c.Alpha2Code)
require.Equal(t, "GBR", c.Alpha3Code)
require.Equal(t, "United Kingdom", c.Name)
require.Equal(t, c.Name, c.String())
}

func TestInvalidCountry(t *testing.T) {
code := "XX"
alpha2 := ""
alpha3 := ""
name := ""
c, ok := Get(code)
if ok {
t.Errorf("country %v should not exist", code)
}
if alpha2 != c.Alpha2Code {
t.Errorf("expected %v got %v", alpha2, c.Alpha2Code)
}
if alpha3 != c.Alpha3Code {
t.Errorf("expected %v got %v", alpha3, c.Alpha3Code)
}
if name != c.Name {
t.Errorf("expected %v got %v", name, c.Name)
}
if c.Name != c.String() {
t.Errorf("expected %v got %v", c.Name, c.String())
}
c, ok := Get("XX")
require.False(t, ok)
require.Equal(t, "", c.Alpha2Code)
require.Equal(t, "", c.Alpha3Code)
require.Equal(t, "", c.Name)
require.Equal(t, c.Name, c.String())
}

func TestValidBbanStructure(t *testing.T) {
code := "FR"
structure, ok := GetBbanStructure(code)
if !ok {
t.Errorf("bban structure for %v should exist", code)
}

if want := 23; want != structure.Length() {
t.Errorf("expected %v got %v", want, structure.Length())
}
structure, ok := GetBbanStructure("FR")
require.True(t, ok)
require.Equal(t, 23, structure.Length())
}

func TestInvalidBbanStructure(t *testing.T) {
code := "XXX"
structure, ok := GetBbanStructure(code)
if ok {
t.Errorf("bban structure for %v should not exist", code)
}
want := 0
if want != structure.Length() {
t.Errorf("expected %v got %v", want, structure.Length())
}
structure, ok := GetBbanStructure("XXX")
require.False(t, ok)
require.Equal(t, 0, structure.Length())
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/jbub/banking

go 1.13

require github.com/stretchr/testify v1.4.0
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 51292da

Please sign in to comment.