github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/bchain/baseparser_test.go (about) 1 package bchain 2 3 import ( 4 "encoding/json" 5 "math/big" 6 "testing" 7 ) 8 9 func NewBaseParser(adp int) *BaseParser { 10 return &BaseParser{ 11 AmountDecimalPoint: adp, 12 } 13 } 14 15 var amounts = []struct { 16 a *big.Int 17 s string 18 adp int 19 alternative string 20 }{ 21 {big.NewInt(123456789), "1.23456789", 8, "!"}, 22 {big.NewInt(2), "0.00000002", 8, "!"}, 23 {big.NewInt(300000000), "3", 8, "!"}, 24 {big.NewInt(498700000), "4.987", 8, "!"}, 25 {big.NewInt(567890), "0.00000000000056789", 18, "!"}, 26 {big.NewInt(-100000000), "-1", 8, "!"}, 27 {big.NewInt(-8), "-0.00000008", 8, "!"}, 28 {big.NewInt(-89012345678), "-890.12345678", 8, "!"}, 29 {big.NewInt(-12345), "-0.00012345", 8, "!"}, 30 {big.NewInt(12345678), "0.123456789012", 8, "0.12345678"}, // test of truncation of too many decimal places 31 } 32 33 func TestBaseParser_AmountToDecimalString(t *testing.T) { 34 for _, tt := range amounts { 35 t.Run(tt.s, func(t *testing.T) { 36 if got := NewBaseParser(tt.adp).AmountToDecimalString(tt.a); got != tt.s && got != tt.alternative { 37 t.Errorf("BaseParser.AmountToDecimalString() = %v, want %v", got, tt.s) 38 } 39 }) 40 } 41 } 42 43 func TestBaseParser_AmountToBigInt(t *testing.T) { 44 for _, tt := range amounts { 45 t.Run(tt.s, func(t *testing.T) { 46 got, err := NewBaseParser(tt.adp).AmountToBigInt(json.Number(tt.s)) 47 if err != nil { 48 t.Errorf("BaseParser.AmountToBigInt() error = %v", err) 49 return 50 } 51 if got.Cmp(tt.a) != 0 { 52 t.Errorf("BaseParser.AmountToBigInt() = %v, want %v", got, tt.a) 53 } 54 }) 55 } 56 }