github.com/aergoio/aergo@v1.3.1/cmd/aergocli/util/unit_test.go (about)

     1  package util
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPasreUnit(t *testing.T) {
    11  	amount, err := ParseUnit("1 aergo")
    12  	assert.NoError(t, err, "parsing aergo")
    13  	t.Log(amount)
    14  	amount, err = ParseUnit("101 Aergo")
    15  	assert.NoError(t, err, "parsing Aergo")
    16  	t.Log(amount)
    17  	amount, err = ParseUnit("101 AERGO")
    18  	assert.NoError(t, err, "parsing AERGO")
    19  	t.Log(amount)
    20  	amount, err = ParseUnit("123 aer")
    21  	assert.NoError(t, err, "parsing aer")
    22  	assert.Equal(t, new(big.Int).SetUint64(123), amount, "123 aer")
    23  	amount, err = ParseUnit("4567 Aer")
    24  	assert.NoError(t, err, "parsing Aer")
    25  	assert.Equal(t, new(big.Int).SetUint64(4567), amount, "4567 Aer")
    26  	amount, err = ParseUnit("101 AER")
    27  	assert.NoError(t, err, "parsing AER")
    28  	assert.Equal(t, new(big.Int).SetUint64(101), amount, "101 AER")
    29  	amount, err = ParseUnit("1 gaer")
    30  	assert.NoError(t, err, "parsing gaer")
    31  	assert.Equal(t, new(big.Int).SetUint64(1000000000), amount, "1 gaer")
    32  	amount, err = ParseUnit("1010")
    33  	assert.Equal(t, new(big.Int).SetUint64(1010), amount, "1010")
    34  	assert.NoError(t, err, "parsing implicit unit")
    35  	t.Log(amount)
    36  }
    37  
    38  func TestPasreDecimalUnit(t *testing.T) {
    39  	amount, err := ParseUnit("1.01 aergo")
    40  	assert.NoError(t, err, "parsing point aergo")
    41  	assert.Equal(t, new(big.Int).SetUint64(1010000000000000000), amount, "converting result")
    42  	amount, err = ParseUnit("1.01 gaer")
    43  	assert.NoError(t, err, "parsing point gaer")
    44  	assert.Equal(t, new(big.Int).SetUint64(1010000000), amount, "converting result")
    45  	amount, err = ParseUnit("0.123456789012345678 aergo")
    46  	assert.NoError(t, err, "parsing point")
    47  	t.Log(amount)
    48  	amount, err = ParseUnit("0.100000000000000001 aergo")
    49  	assert.NoError(t, err, "parsing point max length of decimal")
    50  	t.Log(amount)
    51  	amount, err = ParseUnit("499999999.100000000000000001 aergo")
    52  	assert.NoError(t, err, "parsing point max length of decimal")
    53  	t.Log(amount)
    54  	amount, err = ParseUnit("499999999100000000000000001 aer")
    55  	assert.NoError(t, err, "parsing point max length of decimal")
    56  	t.Log(amount)
    57  }
    58  func TestFailPasreUnit(t *testing.T) {
    59  	amount, err := ParseUnit("0.0000000000000000001 aergo")
    60  	assert.Error(t, err, "exceed max length of decimal")
    61  	t.Log(amount)
    62  	amount, err = ParseUnit("499999999100000000000000001.1 aer")
    63  	assert.Error(t, err, "parsing point max length of decimal")
    64  	amount, err = ParseUnit("1 aergoa")
    65  	assert.Error(t, err, "parsing aergoa")
    66  	amount, err = ParseUnit("1 aerg")
    67  	assert.Error(t, err, "parsing aerg")
    68  	amount, err = ParseUnit("1 aaergo")
    69  	assert.Error(t, err, "parsing aaergo")
    70  	amount, err = ParseUnit("1 aergo ")
    71  	assert.Error(t, err, "check fail")
    72  	amount, err = ParseUnit("1aergo.1aer")
    73  	assert.Error(t, err, "check fail")
    74  	amount, err = ParseUnit("0.1")
    75  	assert.Error(t, err, "default unit assumed aergo")
    76  	amount, err = ParseUnit("0.1.1")
    77  	assert.Error(t, err, "only one dot is allowed")
    78  }
    79  
    80  func TestConvertUnit(t *testing.T) {
    81  	result, err := ConvertUnit(new(big.Int).SetUint64(1000000000000000000), "aergo")
    82  	assert.NoError(t, err, "convert 1 aergo")
    83  	t.Log(result)
    84  	result, err = ConvertUnit(new(big.Int).SetUint64(1020300000000000000), "aergo")
    85  	assert.NoError(t, err, "convert 1.0203 aergo")
    86  	t.Log(result)
    87  	result, err = ConvertUnit(new(big.Int).SetUint64(1000000000), "gaer")
    88  	assert.NoError(t, err, "convert 1 gaer")
    89  	t.Log(result)
    90  	result, err = ConvertUnit(new(big.Int).SetUint64(1), "gaer")
    91  	assert.NoError(t, err, "convert 0.000000001 gaer")
    92  	assert.Equal(t, "0.000000001 gaer", result)
    93  	result, err = ConvertUnit(new(big.Int).SetUint64(10), "gaer")
    94  	assert.NoError(t, err, "convert 0.00000001 gaer")
    95  	assert.Equal(t, "0.00000001 gaer", result)
    96  	t.Log(result)
    97  	result, err = ConvertUnit(new(big.Int).SetUint64(0), "gaer")
    98  	assert.NoError(t, err, "convert 0 gaer")
    99  	assert.Equal(t, "0 gaer", result)
   100  	t.Log(result)
   101  	result, err = ConvertUnit(new(big.Int).SetUint64(1), "aer")
   102  	assert.NoError(t, err, "convert 1 aer")
   103  	assert.Equal(t, "1 aer", result)
   104  	t.Log(result)
   105  	result, err = ConvertUnit(new(big.Int).SetUint64(1000000000000000000), "gaer")
   106  	assert.NoError(t, err, "convert 1000000000 gaer")
   107  	t.Log(result)
   108  }