diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 1454fb4..a097ace 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/currency.go b/currency.go index 00e6dcd..2dfeadc 100644 --- a/currency.go +++ b/currency.go @@ -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₹ @@ -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) @@ -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. diff --git a/currency_test.go b/currency_test.go index 946cf7a..df60e1f 100644 --- a/currency_test.go +++ b/currency_test.go @@ -1,7 +1,9 @@ package currency import ( + "errors" "fmt" + "strconv" "testing" ) @@ -11,6 +13,7 @@ type output struct { totalfractional int str string float float64 + err error } type input struct { @@ -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{ @@ -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{ @@ -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}, }, } @@ -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 { @@ -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 { @@ -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 { @@ -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) { @@ -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 { @@ -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) + } + }) + } +}