|
| 1 | +//go:build all || unit |
| 2 | + |
| 3 | +package hiero |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | + |
| 13 | +func TestUnitHbarAllowanceConstructor(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + ownerAccountID := *_MockHbarAllowance().OwnerAccountID |
| 17 | + spenderAccountID := *_MockHbarAllowance().SpenderAccountID |
| 18 | + amount := int64(100) |
| 19 | + |
| 20 | + allowance := NewHbarAllowance(ownerAccountID, spenderAccountID, amount) |
| 21 | + |
| 22 | + require.Equal(t, ownerAccountID, *allowance.OwnerAccountID) |
| 23 | + require.Equal(t, spenderAccountID, *allowance.SpenderAccountID) |
| 24 | + require.Equal(t, amount, allowance.Amount) |
| 25 | +} |
| 26 | + |
| 27 | +func TestUnitHbarAllowanceProtobufRoundTrip(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + allowance := _MockHbarAllowance() |
| 31 | + |
| 32 | + pb := allowance._ToProtobuf() |
| 33 | + require.NotNil(t, pb) |
| 34 | + |
| 35 | + allowanceFromProtobuf := _HbarAllowanceFromProtobuf(pb) |
| 36 | + require.Equal(t, allowance, allowanceFromProtobuf) |
| 37 | +} |
| 38 | + |
| 39 | +func TestUnitHbarAllowanceProtobufRoundTripNilOwner(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + allowance := _MockHbarAllowance() |
| 43 | + allowance.OwnerAccountID = nil |
| 44 | + |
| 45 | + pb := allowance._ToProtobuf() |
| 46 | + require.NotNil(t, pb) |
| 47 | + require.Nil(t, pb.Owner) |
| 48 | + |
| 49 | + allowanceFromProtobuf := _HbarAllowanceFromProtobuf(pb) |
| 50 | + require.Equal(t, allowance, allowanceFromProtobuf) |
| 51 | +} |
| 52 | + |
| 53 | +func TestUnitHbarAllowanceProtobufRoundTripNilSpender(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + allowance := _MockHbarAllowance() |
| 57 | + allowance.SpenderAccountID = nil |
| 58 | + |
| 59 | + pb := allowance._ToProtobuf() |
| 60 | + require.NotNil(t, pb) |
| 61 | + require.Nil(t, pb.Spender) |
| 62 | + |
| 63 | + allowanceFromProtobuf := _HbarAllowanceFromProtobuf(pb) |
| 64 | + require.Equal(t, allowance, allowanceFromProtobuf) |
| 65 | +} |
| 66 | + |
| 67 | +func TestUnitHbarAllowanceString(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + allowance := _MockHbarAllowance() |
| 71 | + require.Equal(t, "OwnerAccountID: 0.0.123, SpenderAccountID: 0.0.456, Amount: 100 tℏ", allowance.String()) |
| 72 | + |
| 73 | + allowance.OwnerAccountID = nil |
| 74 | + require.Equal(t, "SpenderAccountID: 0.0.456, Amount: 100 tℏ", allowance.String()) |
| 75 | + |
| 76 | + allowance = _MockHbarAllowance() |
| 77 | + allowance.SpenderAccountID = nil |
| 78 | + require.Equal(t, "OwnerAccountID: 0.0.123, Amount: 100 tℏ", allowance.String()) |
| 79 | + |
| 80 | + allowance.OwnerAccountID = nil |
| 81 | + require.Equal(t, "", allowance.String()) |
| 82 | +} |
| 83 | + |
| 84 | +func _MockHbarAllowance() HbarAllowance { |
| 85 | + ownerAccountID, _ := AccountIDFromString("0.0.123-esxsf") |
| 86 | + ownerAccountID.checksum = nil |
| 87 | + |
| 88 | + spenderAccountID, _ := AccountIDFromString("0.0.123-esxsf") |
| 89 | + spenderAccountID.Account = 456 |
| 90 | + spenderAccountID.checksum = nil |
| 91 | + |
| 92 | + return HbarAllowance{ |
| 93 | + OwnerAccountID: &ownerAccountID, |
| 94 | + SpenderAccountID: &spenderAccountID, |
| 95 | + Amount: 100, |
| 96 | + } |
| 97 | +} |
0 commit comments