github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/hbar_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestUnitHbarFromTinybar(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	tinybar := HbarUnits.Hbar._NumberOfTinybar()
    38  
    39  	hbar := HbarFromTinybar(tinybar)
    40  
    41  	assert.Equal(t, tinybar, hbar.tinybar)
    42  
    43  	tinybar = MaxHbar.tinybar
    44  
    45  	hbar = HbarFromTinybar(tinybar)
    46  
    47  	assert.Equal(t, tinybar, hbar.tinybar)
    48  
    49  	tinybar = MinHbar.tinybar
    50  
    51  	hbar = HbarFromTinybar(tinybar)
    52  
    53  	assert.Equal(t, tinybar, hbar.tinybar)
    54  }
    55  
    56  func TestUnitHbarUnit(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	tinybar := HbarUnits.Kilobar._NumberOfTinybar()
    60  
    61  	hbar := HbarFromTinybar(tinybar)
    62  
    63  	hbar2, err := HbarFromString(hbar.ToString(HbarUnits.Kilobar))
    64  	require.NoError(t, err)
    65  	assert.Equal(t, hbar2.tinybar, hbar.tinybar)
    66  
    67  	tinybar = HbarUnits.Gigabar._NumberOfTinybar()
    68  
    69  	hbar = HbarFromTinybar(tinybar)
    70  
    71  	hbar2, err = HbarFromString(hbar.ToString(HbarUnits.Gigabar))
    72  	require.NoError(t, err)
    73  	assert.Equal(t, hbar2.tinybar, hbar.tinybar)
    74  
    75  	tinybar = HbarUnits.Microbar._NumberOfTinybar()
    76  
    77  	hbar = HbarFromTinybar(tinybar)
    78  
    79  	hbar2, err = HbarFromString(hbar.ToString(HbarUnits.Microbar))
    80  	require.NoError(t, err)
    81  	assert.Equal(t, hbar2.tinybar, hbar.tinybar)
    82  
    83  	hbar2, err = HbarFromString("-5.123 Gℏ")
    84  	require.NoError(t, err)
    85  	assert.Equal(t, hbar2.tinybar, int64(-512300000000000000))
    86  
    87  	hbar2, err = HbarFromString("5")
    88  	require.NoError(t, err)
    89  	assert.Equal(t, hbar2.ToString(HbarUnits.Hbar), "5 ℏ")
    90  
    91  	hbar2, err = HbarFromString("+5.123 ℏ")
    92  	require.NoError(t, err)
    93  	assert.Equal(t, hbar2.ToString(HbarUnits.Millibar), "5123 mℏ")
    94  
    95  	hbar2, err = HbarFromString("1.151 uℏ")
    96  	assert.Error(t, err)
    97  
    98  	hbar2, err = HbarFromString("1.151.")
    99  	assert.Error(t, err)
   100  }