github.com/decred/politeia@v1.4.0/politeiawww/legacy/dcrdata_test.go (about) 1 // Copyright (c) 2017-2020 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package legacy 6 7 import ( 8 "errors" 9 "testing" 10 11 "github.com/decred/politeia/util" 12 ) 13 14 func TestDcrStringToAmount(t *testing.T) { 15 testCases := []struct { 16 input string 17 output uint64 18 expectedError error 19 }{ 20 { 21 "0.1", 22 1e7, 23 nil, 24 }, 25 { 26 "0.15", 27 1.5e7, 28 nil, 29 }, 30 { 31 "000000.10000", 32 1e7, 33 nil, 34 }, 35 { 36 ".1", 37 1e7, 38 nil, 39 }, 40 { 41 "1", 42 1e8, 43 nil, 44 }, 45 { 46 "1.0", 47 1e8, 48 nil, 49 }, 50 { 51 "500", 52 5e10, 53 nil, 54 }, 55 } 56 57 // test 58 for _, testCase := range testCases { 59 result, err := util.DcrStringToAtoms(testCase.input) 60 if !errors.Is(err, testCase.expectedError) { 61 t.Errorf("Expected %v for input %s, got %v.", 62 testCase.expectedError, testCase.input, err) 63 } 64 if err == nil { 65 if result != testCase.output { 66 t.Errorf("Expected %v for input %s, got %v.", testCase.output, 67 testCase.input, result) 68 } 69 } 70 } 71 }