Skip to content

Commit

Permalink
[patch] minor refactor by reusing New and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Oct 5, 2024
1 parent f7af9df commit 30f7ab7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 46 deletions.
45 changes: 22 additions & 23 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
push:
branches: ["master"]
pull_request:
branches: ["master"]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.23"
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.23"

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Tests
run: |
go install github.com/mattn/goveralls@latest
go test -race -covermode atomic -coverprofile=covprofile ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: covprofile
- name: Tests
run: |
go install github.com/mattn/goveralls@latest
go test -race -covermode atomic -coverprofile=covprofile ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: covprofile
20 changes: 5 additions & 15 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type Currency struct {
FUShare uint `json:"fuShare,omitempty"`

// fuDigits is the number of digits in FUShare-1 (i.e. number of digits in the maximum value which the fractional unit can have, e.g. 99 paise, 2 digits)
fuDigits int `json:"fuDigits,omitempty"`
fuDigits int
// magnitude is the fraction which sets the magnitude required for the rounding function
magnitude float64 `json:"magnitude,omitempty"`
magnitude float64
// PrefixSymbol if true will add the symbol as a prefix to the string representation of currency. e.g. ₹1.5
PrefixSymbol bool `json:"alwaysAddPrefix,omitempty"`
// SuffixSymbol if true will add the symbol as a suffix to the string representation of currency. e.g. 1.5₹
Expand Down Expand Up @@ -137,11 +137,11 @@ func ParseString(value string, code, symbol, funame string, fushare uint) (*Curr

// ParseFloat64 will parse a float value into currency.
func ParseFloat64(value float64, code, symbol, funame string, fushare uint) (*Currency, error) {
if fushare == 0 {
fus := int(fushare)
if fus == 0 {
return nil, ErrInvalidFUS
}

fus := int(fushare)
fudigits := digits(fus - 1)

mag := float64(5.0)
Expand All @@ -160,17 +160,7 @@ func ParseFloat64(value float64, code, symbol, funame string, fushare uint) (*Cu

m := main + (fractional / fus)
f := fractional % fus

return &Currency{
Code: code,
Symbol: symbol,
Main: m,
Fractional: f,
FUName: funame,
FUShare: fushare,
fuDigits: fudigits,
magnitude: mag,
}, nil
return New(m, f, code, symbol, funame, fushare)
}

// FractionalTotal returns the total value in fractional int.
Expand Down
105 changes: 97 additions & 8 deletions currency_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package currency

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

Expand All @@ -11,6 +13,7 @@ type output struct {
totalfractional int
str string
float float64
err error
}

type input struct {
Expand Down Expand Up @@ -40,7 +43,7 @@ var newTests = []struct {
symbol: "₹",
fulabel: "paise",
fushare: 100},
output{10, 50, 1050, "₹10.50", 10.50},
output{10, 50, 1050, "₹10.50", 10.50, nil},
},
{
input{
Expand All @@ -53,7 +56,7 @@ var newTests = []struct {
symbol: "₹",
fulabel: "paise",
fushare: 100},
output{-10, 50, -1050, "-₹10.50", -10.50},
output{-10, 50, -1050, "-₹10.50", -10.50, nil},
},
{
input{
Expand All @@ -66,7 +69,20 @@ var newTests = []struct {
symbol: "₹",
fulabel: "paise",
fushare: 100},
output{0, -50, -50, "-₹0.50", -0.50},
output{0, -50, -50, "-₹0.50", -0.50, nil},
},
{
input{
main: 1,
fractional: 100,
totalfractional: 0,
code: "INR",
str: "1.00",
symbol: "₹",
fulabel: "paise",
fushare: 0,
},
output{0, 0, 0, "", 0, ErrInvalidFUS},
},
}

Expand All @@ -79,9 +95,11 @@ func TestNew(t *testing.T) {
nT.inp.symbol,
nT.inp.fulabel,
nT.inp.fushare)

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

if cur.Main != nT.out.main {
Expand Down Expand Up @@ -125,7 +143,10 @@ func TestNewFractional(t *testing.T) {
nT.inp.fushare)

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

if cur.Main != nT.out.main {
Expand Down Expand Up @@ -168,7 +189,10 @@ func TestParseStr(t *testing.T) {
nT.inp.fushare)

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

if cur.Main != nT.out.main {
Expand Down Expand Up @@ -199,6 +223,14 @@ func TestParseStr(t *testing.T) {
t.Fail()
}
}

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

func TestParseFloat64(t *testing.T) {
Expand All @@ -211,7 +243,10 @@ func TestParseFloat64(t *testing.T) {
nT.inp.fushare)

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

if cur.Main != nT.out.main {
Expand Down Expand Up @@ -354,3 +389,57 @@ func BenchmarkFractionalTotal(t *testing.B) {
cur1.FractionalTotal()
}
}

func Test_digits(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
want int
}{
{
name: "single positive",
args: args{
n: 1,
},
want: 1,
},
{
name: "single negative",
args: args{
n: -1,
},
want: 1,
},
{
name: "more digits positive",
args: args{
n: 100,
},
want: 3,
},
{
name: "more digits negative",
args: args{
n: -100,
},
want: 3,
},
{
name: "zero",
args: args{
n: 0,
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := digits(tt.args.n); got != tt.want {
t.Errorf("digits() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 30f7ab7

Please sign in to comment.