github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/stellar_test.go (about) 1 package stellar 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/keybase/client/go/libkb" 8 "github.com/keybase/stellarnet" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type fmtTest struct { 13 amount string 14 precTwo bool 15 16 // "": both 'round' and 'truncate' are expected to return the same result 17 // "round": round the value 18 // "truncate": truncate the value 19 rounding string 20 21 out string 22 valid bool 23 } 24 25 var fmtTests = []fmtTest{ 26 {amount: "0", precTwo: false, out: "0", valid: true}, 27 {amount: "0.00", precTwo: false, out: "0", valid: true}, 28 {amount: "0.0000000", precTwo: false, out: "0", valid: true}, 29 {amount: "0", precTwo: true, out: "0.00", valid: true}, 30 {amount: "0.00", precTwo: true, out: "0.00", valid: true}, 31 {amount: "0.0000000", precTwo: true, out: "0.00", valid: true}, 32 {amount: "0.123", precTwo: false, out: "0.1230000", valid: true}, 33 {amount: "0.123", precTwo: true, out: "0.12", valid: true}, 34 {amount: "123", precTwo: false, out: "123", valid: true}, 35 {amount: "123", precTwo: true, out: "123.00", valid: true}, 36 {amount: "123.456", precTwo: false, out: "123.4560000", valid: true}, 37 {amount: "1234.456", precTwo: false, out: "1,234.4560000", valid: true}, 38 {amount: "1234.456", precTwo: true, rounding: "round", out: "1,234.46", valid: true}, 39 {amount: "1234.456", precTwo: true, rounding: "truncate", out: "1,234.45", valid: true}, 40 {amount: "1234.1234567", precTwo: false, out: "1,234.1234567", valid: true}, 41 {amount: "123123123.1234567", precTwo: false, out: "123,123,123.1234567", valid: true}, 42 {amount: "123123123.1234567", precTwo: true, out: "123,123,123.12", valid: true}, 43 {amount: "9123123123.1234567", precTwo: false, out: "9,123,123,123.1234567", valid: true}, 44 {amount: "89123123123.1234567", precTwo: false, out: "89,123,123,123.1234567", valid: true}, 45 {amount: "456456456123123123.1234567", precTwo: false, out: "456,456,456,123,123,123.1234567", valid: true}, 46 {amount: "-0.123", precTwo: false, out: "-0.1230000", valid: true}, 47 {amount: "-0.123", precTwo: true, out: "-0.12", valid: true}, 48 {amount: "-123", precTwo: false, out: "-123", valid: true}, 49 {amount: "-123", precTwo: true, out: "-123.00", valid: true}, 50 {amount: "-123.456", precTwo: false, out: "-123.4560000", valid: true}, 51 {amount: "-1234.456", precTwo: false, out: "-1,234.4560000", valid: true}, 52 {amount: "-1234.456", precTwo: true, rounding: "round", out: "-1,234.46", valid: true}, 53 {amount: "-1234.456", precTwo: true, rounding: "truncate", out: "-1,234.45", valid: true}, 54 {amount: "-1234.1234567", precTwo: false, out: "-1,234.1234567", valid: true}, 55 {amount: "-123123123.1234567", precTwo: false, out: "-123,123,123.1234567", valid: true}, 56 {amount: "-123123123.1234567", precTwo: true, out: "-123,123,123.12", valid: true}, 57 {amount: "-9123123123.1234567", precTwo: false, out: "-9,123,123,123.1234567", valid: true}, 58 {amount: "-89123123123.1234567", precTwo: false, out: "-89,123,123,123.1234567", valid: true}, 59 {amount: "-456456456123123123.1234567", precTwo: false, out: "-456,456,456,123,123,123.1234567", valid: true}, 60 {amount: "123123", precTwo: true, out: "123,123.00", valid: true}, 61 {amount: "123123", precTwo: false, out: "123,123.00", valid: true}, 62 // error cases 63 {amount: "", out: "", valid: false}, 64 {amount: "garbage", out: "", valid: false}, 65 {amount: "3/4", out: "", valid: false}, 66 {amount: "1.234e5", out: "", valid: false}, 67 {amount: "132E5", out: "", valid: false}, 68 {amount: "132.5 3", out: "", valid: false}, 69 } 70 71 func TestFormatAmount(t *testing.T) { 72 tc := libkb.SetupTest(t, "fmt", 1) 73 defer tc.Cleanup() 74 75 for i, test := range fmtTests { 76 switch test.rounding { 77 case "", "round", "truncate": 78 default: 79 t.Fatalf("%v: invalid rounding '%v'", i, test.rounding) 80 } 81 for _, rounding := range []stellarnet.FmtRoundingBehavior{stellarnet.Round, stellarnet.Truncate} { 82 if test.rounding == "round" && rounding == stellarnet.Truncate { 83 continue 84 } 85 if test.rounding == "truncate" && rounding == stellarnet.Round { 86 continue 87 } 88 desc := fmt.Sprintf("amount: %v (2pt prec %v) (rounding %v)", test.amount, test.precTwo, rounding) 89 x, err := FormatAmount(libkb.NewMetaContextForTest(tc), test.amount, test.precTwo, rounding) 90 if test.valid { 91 require.NoError(t, err, "%v => error: %v", desc, err) 92 require.Equal(t, test.out, x, "%v => %q, expected: %q", desc, x, test.out) 93 } else { 94 require.Errorf(t, err, "%v is supposed to be invalid input", desc) 95 require.Equal(t, test.out, x) 96 } 97 } 98 } 99 }