Skip to content

Commit

Permalink
[-] use testify package for assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Oct 6, 2024
1 parent 55bbc94 commit 84f2e8f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 265 deletions.
173 changes: 42 additions & 131 deletions currency_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package currency

import (
"errors"
"fmt"
"strconv"
"testing"

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

type output struct {
Expand Down Expand Up @@ -87,6 +88,7 @@ var newTests = []struct {
}

func TestNew(t *testing.T) {
asserter := assert.New(t)
for _, nT := range newTests {
cur, err := New(
nT.inp.main,
Expand All @@ -96,44 +98,24 @@ func TestNew(t *testing.T) {
nT.inp.fulabel,
nT.inp.fushare)
if err != nil {
if !errors.Is(err, nT.out.err) {
t.Fatal(err)
}
asserter.ErrorIs(err, nT.out.err)
continue
}

if cur.Main != nT.out.main {
t.Log("Expected:", nT.out.main, "got:", cur.Main)
t.Fail()
}

if cur.Fractional != nT.out.fractional {
t.Log("Expected:", nT.out.fractional, "got:", cur.Fractional)
t.Fail()
}
asserter.Equal(nT.out.main, cur.Main)
asserter.Equal(nT.out.fractional, cur.Fractional)
asserter.Equal(nT.out.float, cur.Float64())
asserter.Equal(nT.out.totalfractional, cur.FractionalTotal())

cur.PrefixSymbol = true
str := cur.String()
if str != nT.out.str {
t.Log("Expected:", nT.out.str, "got:", str)
t.Fail()
}

if cur.Float64() != nT.out.float {
t.Log("Expected:", nT.out.float, "got:", cur.Float64())
t.Fail()
}
asserter.Equal(nT.out.str, cur.String())

ft := cur.FractionalTotal()
if ft != nT.out.totalfractional {
t.Log("Expected:", nT.out.totalfractional, "got:", ft)
t.Fail()
}
}

}

func TestNewFractional(t *testing.T) {
asserter := assert.New(t)
for _, nT := range newTests {
cur, err := NewFractional(
nT.inp.totalfractional,
Expand All @@ -143,43 +125,23 @@ func TestNewFractional(t *testing.T) {
nT.inp.fushare)

if err != nil {
if !errors.Is(err, nT.out.err) {
t.Fatal(err)
}
asserter.ErrorIs(err, nT.out.err)
continue
}

if cur.Main != nT.out.main {
t.Log("Expected:", nT.out.main, "got:", cur.Main)
t.Fail()
}

if cur.Fractional != nT.out.fractional {
t.Log("Expected:", nT.out.fractional, "got:", cur.Fractional)
t.Fail()
}
asserter.Equal(nT.out.main, cur.Main)
asserter.Equal(nT.out.fractional, cur.Fractional)
asserter.Equal(nT.out.float, cur.Float64())
asserter.Equal(nT.out.totalfractional, cur.FractionalTotal())

cur.PrefixSymbol = true
str := cur.String()
if str != nT.out.str {
t.Log("Expected:", nT.out.str, "got:", str)
t.Fail()
}

if cur.Float64() != nT.out.float {
t.Log("Expected:", nT.out.float, "got:", cur.Float64())
t.Fail()
}

ft := cur.FractionalTotal()
if ft != nT.out.totalfractional {
t.Log("Expected:", nT.out.totalfractional, "got:", ft)
t.Fail()
}
asserter.Equal(nT.out.str, cur.String())
}
}

func TestParseStr(t *testing.T) {
asserter := assert.New(t)

for _, nT := range newTests {
cur, err := ParseString(
nT.inp.str,
Expand All @@ -189,51 +151,25 @@ func TestParseStr(t *testing.T) {
nT.inp.fushare)

if err != nil {
if !errors.Is(err, nT.out.err) {
t.Fatal(err)
}
asserter.ErrorIs(err, nT.out.err)
continue
}

if cur.Main != nT.out.main {
t.Log("Expected:", nT.out.main, "got:", cur.Main)
t.Fail()
}

if cur.Fractional != nT.out.fractional {
t.Log("Expected:", nT.out.fractional, "got:", cur.Fractional)
t.Fail()
}
asserter.Equal(nT.out.main, cur.Main)
asserter.Equal(nT.out.fractional, cur.Fractional)
asserter.Equal(nT.out.float, cur.Float64())
asserter.Equal(nT.out.totalfractional, cur.FractionalTotal())

cur.PrefixSymbol = true
str := cur.String()
if str != nT.out.str {
t.Log("Expected:", nT.out.str, "got:", str)
t.Fail()
}

if cur.Float64() != nT.out.float {
t.Log("Expected:", nT.out.float, "got:", cur.Float64())
t.Fail()
}

ft := cur.FractionalTotal()
if ft != nT.out.totalfractional {
t.Log("Expected:", nT.out.totalfractional, "got:", ft)
t.Fail()
}
asserter.Equal(nT.out.str, cur.String())
}

// code: "INR",
// symbol: "₹",
// fulabel: "paise",
_, err := ParseString("", "INR", "₹", "paise", 0)
if !errors.Is(err, strconv.ErrSyntax) {
t.Error(err)
}
asserter.ErrorIs(err, strconv.ErrSyntax)
}

func TestParseFloat64(t *testing.T) {
asserter := assert.New(t)
for _, nT := range newTests {
cur, err := ParseFloat64(
nT.inp.float,
Expand All @@ -243,43 +179,21 @@ func TestParseFloat64(t *testing.T) {
nT.inp.fushare)

if err != nil {
if !errors.Is(err, nT.out.err) {
t.Fatal(err)
}
asserter.ErrorIs(err, nT.out.err)
continue
}

if cur.Main != nT.out.main {
t.Log("Expected:", nT.out.main, "got:", cur.Main)
t.Fail()
}

if cur.Fractional != nT.out.fractional {
t.Log("Expected:", nT.out.fractional, "got:", cur.Fractional)
t.Fail()
}
asserter.Equal(nT.out.main, cur.Main)
asserter.Equal(nT.out.fractional, cur.Fractional)
asserter.Equal(nT.out.float, cur.Float64())
asserter.Equal(nT.out.totalfractional, cur.FractionalTotal())

cur.PrefixSymbol = true
str := cur.String()
if str != nT.out.str {
t.Log("Expected:", nT.out.str, "got:", str)
t.Fail()
}

if cur.Float64() != nT.out.float {
t.Log("Expected:", nT.out.float, "got:", cur.Float64())
t.Fail()
}

ft := cur.FractionalTotal()
if ft != nT.out.totalfractional {
t.Log("Expected:", nT.out.totalfractional, "got:", ft)
t.Fail()
}
asserter.Equal(nT.out.str, cur.String())
}
}

func TestFormat(t *testing.T) {
asserter := assert.New(t)
c, _ := New(12, 75, "INR", "₹", "paise", 100)
list := []struct {
Verb string
Expand Down Expand Up @@ -329,64 +243,61 @@ func TestFormat(t *testing.T) {
c.SuffixSymbol = l.Suffix

formatstr := "%" + l.Verb
str := fmt.Sprintf(formatstr, c)
if str != l.Expected {
t.Errorf("Format string: %s, Expected '%s', got '%s'", formatstr, l.Expected, str)
}
asserter.Equal(l.Expected, fmt.Sprintf(formatstr, c))
}
}

func BenchmarkNew(t *testing.B) {
for i := 0; i < t.N; i++ {
New(10, 50, "INR", "₹", "paise", 100)
_, _ = New(10, 50, "INR", "₹", "paise", 100)
}
}

func BenchmarkNewFractional(t *testing.B) {
for i := 0; i < t.N; i++ {
NewFractional(1005, "INR", "₹", "paise", 100)
_, _ = NewFractional(1005, "INR", "₹", "paise", 100)
}
}

func BenchmarkParseFloat64(t *testing.B) {
for i := 0; i < t.N; i++ {
ParseFloat64(10.05, "INR", "₹", "paise", 100)
_, _ = ParseFloat64(10.05, "INR", "₹", "paise", 100)
}
}

func BenchmarkParseString(t *testing.B) {
for i := 0; i < t.N; i++ {
ParseString("10.05", "INR", "₹", "paise", 100)
_, _ = ParseString("10.05", "INR", "₹", "paise", 100)
}
}

func BenchmarkString(t *testing.B) {
cur1, _ := New(10, 5, "INR", "₹", "paise", 100)
cur1.PrefixSymbol = true
for i := 0; i < t.N; i++ {
cur1.String()
_ = cur1.String()
}
}

func BenchmarkStringNoPrefix(t *testing.B) {
cur1, _ := New(10, 5, "INR", "₹", "paise", 100)
for i := 0; i < t.N; i++ {
cur1.StringWithoutSymbols()
_ = cur1.StringWithoutSymbols()
}
}

func BenchmarkFloat64(t *testing.B) {
cur1, _ := New(10, 5, "INR", "₹", "paise", 100)
for i := 0; i < t.N; i++ {
cur1.Float64()
_ = cur1.Float64()
}
}

func BenchmarkFractionalTotal(t *testing.B) {
cur1, _ := New(1, 0, "INR", "₹", "paise", 100)

for i := 0; i < t.N; i++ {
cur1.FractionalTotal()
_ = cur1.FractionalTotal()
}
}

Expand Down
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/naughtygopher/currency/v2

go 1.14

require github.com/stretchr/testify v1.9.0
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 84f2e8f

Please sign in to comment.