github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_id_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/hashgraph/hedera-protobufs-go/services"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestUnitTokenIDFromString(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	tokID := TokenID{
    40  		Shard: 1,
    41  		Realm: 2,
    42  		Token: 3,
    43  	}
    44  
    45  	gotTokID, err := TokenIDFromString(tokID.String())
    46  	require.NoError(t, err)
    47  	assert.Equal(t, tokID.Token, gotTokID.Token)
    48  }
    49  
    50  func TestUnitTokenIDChecksumFromString(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	id, err := TokenIDFromString("0.0.123-rmkyk")
    54  	require.NoError(t, err)
    55  
    56  	client, err := _NewMockClient()
    57  	client.SetLedgerID(*NewLedgerIDTestnet())
    58  	require.NoError(t, err)
    59  	id.ToStringWithChecksum(*client)
    60  	sol := id.ToSolidityAddress()
    61  	TokenIDFromSolidityAddress(sol)
    62  	id.Validate(client)
    63  	pb := id._ToProtobuf()
    64  	_TokenIDFromProtobuf(pb)
    65  
    66  	idByte := id.ToBytes()
    67  	TokenIDFromBytes(idByte)
    68  
    69  	id.Compare(TokenID{Token: 32})
    70  
    71  	assert.Equal(t, id.Token, uint64(123))
    72  }
    73  
    74  func TestUnitTokenIDChecksumToString(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	id := AccountID{
    78  		Shard:   50,
    79  		Realm:   150,
    80  		Account: 520,
    81  	}
    82  	assert.Equal(t, "50.150.520", id.String())
    83  }
    84  
    85  func TestUnitTokenIDFromStringEVM(t *testing.T) {
    86  	t.Parallel()
    87  
    88  	id, err := TokenIDFromString("0.0.434")
    89  	require.NoError(t, err)
    90  
    91  	require.Equal(t, "0.0.434", id.String())
    92  }
    93  
    94  func TestUnitTokenIDProtobuf(t *testing.T) {
    95  	t.Parallel()
    96  
    97  	id, err := TokenIDFromString("0.0.434")
    98  	require.NoError(t, err)
    99  
   100  	pb := id._ToProtobuf()
   101  
   102  	require.Equal(t, pb, &services.TokenID{
   103  		ShardNum: 0,
   104  		RealmNum: 0,
   105  		TokenNum: 434,
   106  	})
   107  
   108  	pbFrom := _TokenIDFromProtobuf(pb)
   109  
   110  	require.Equal(t, id, *pbFrom)
   111  }