forked from hiero-ledger/hiero-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenTransferUnitTests.cc
More file actions
154 lines (130 loc) · 5.74 KB
/
TokenTransferUnitTests.cc
File metadata and controls
154 lines (130 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: Apache-2.0
#include "TokenTransfer.h"
#include "AccountId.h"
#include "TokenId.h"
#include <services/basic_types.pb.h>
#include <gtest/gtest.h>
using namespace Hiero;
class TokenTransferUnitTests : public ::testing::Test
{
protected:
[[nodiscard]] inline const TokenId& getTestTokenId() const { return mTokenId; }
[[nodiscard]] inline const AccountId& getTestAccountId() const { return mAccountId; }
[[nodiscard]] inline const int64_t& getTestAmount() const { return mAmount; }
[[nodiscard]] inline const uint32_t& getTestExpectedDecimals() const { return mExpectedDecimals; }
[[nodiscard]] inline bool getTestIsApproval() const { return mIsApproval; }
private:
const TokenId mTokenId = TokenId(10ULL);
const AccountId mAccountId = AccountId(200ULL);
const int64_t mAmount = 3000LL;
const uint32_t mExpectedDecimals = 40000U;
const bool mIsApproval = true;
};
//-----
TEST_F(TokenTransferUnitTests, DefaultConstruction)
{
// Given / When
const TokenTransfer tokenTransfer;
// Then
EXPECT_EQ(tokenTransfer.mTokenId, TokenId());
EXPECT_EQ(tokenTransfer.mAccountId, AccountId());
EXPECT_EQ(tokenTransfer.mAmount, 0ULL);
EXPECT_EQ(tokenTransfer.mExpectedDecimals, 0);
EXPECT_FALSE(tokenTransfer.mIsApproval);
}
//-----
TEST_F(TokenTransferUnitTests, ConstructWithTokenIdAccountIdAmountAndApproval)
{
// Given / When
const TokenTransfer tokenTransfer(getTestTokenId(), getTestAccountId(), getTestAmount(), getTestIsApproval());
// Then
EXPECT_EQ(tokenTransfer.mTokenId, getTestTokenId());
EXPECT_EQ(tokenTransfer.mAccountId, getTestAccountId());
EXPECT_EQ(tokenTransfer.mAmount, getTestAmount());
EXPECT_EQ(tokenTransfer.mExpectedDecimals, 0U);
EXPECT_EQ(tokenTransfer.mIsApproval, getTestIsApproval());
}
//-----
TEST_F(TokenTransferUnitTests, ConstructWithTokenIdAccountIdAmountExpectedDecimalsAndApproval)
{
// Given / When
const TokenTransfer tokenTransfer(
getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
// Then
EXPECT_EQ(tokenTransfer.mTokenId, getTestTokenId());
EXPECT_EQ(tokenTransfer.mAccountId, getTestAccountId());
EXPECT_EQ(tokenTransfer.mAmount, getTestAmount());
EXPECT_EQ(tokenTransfer.mExpectedDecimals, getTestExpectedDecimals());
EXPECT_EQ(tokenTransfer.mIsApproval, getTestIsApproval());
}
//-----
TEST_F(TokenTransferUnitTests, FromProtobuf)
{
// Given
proto::TokenTransferList tokenTransferList;
tokenTransferList.set_allocated_token(getTestTokenId().toProtobuf().release());
proto::AccountAmount* amount = tokenTransferList.add_transfers();
amount->set_allocated_accountid(getTestAccountId().toProtobuf().release());
amount->set_amount(getTestAmount());
amount->set_is_approval(getTestIsApproval());
auto decimalPtr = std::make_unique<google::protobuf::UInt32Value>();
decimalPtr->set_value(getTestExpectedDecimals());
tokenTransferList.set_allocated_expected_decimals(decimalPtr.release());
// When
const TokenTransfer tokenTransfer =
TokenTransfer::fromProtobuf(*amount, getTestTokenId(), tokenTransferList.expected_decimals().value());
// Then
EXPECT_EQ(tokenTransfer.mTokenId, getTestTokenId());
EXPECT_EQ(tokenTransfer.mAccountId, getTestAccountId());
EXPECT_EQ(tokenTransfer.mAmount, getTestAmount());
EXPECT_EQ(tokenTransfer.mExpectedDecimals, getTestExpectedDecimals());
EXPECT_EQ(tokenTransfer.mIsApproval, getTestIsApproval());
}
//-----
TEST_F(TokenTransferUnitTests, ToProtobuf)
{
// Given
const TokenTransfer tokenTransfer(
getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
// When
const std::unique_ptr<proto::AccountAmount> proto = tokenTransfer.toProtobuf();
// Then
EXPECT_EQ(proto->accountid().shardnum(), getTestAccountId().mShardNum);
EXPECT_EQ(proto->accountid().realmnum(), getTestAccountId().mRealmNum);
EXPECT_EQ(proto->accountid().accountnum(), getTestAccountId().mAccountNum);
EXPECT_EQ(proto->amount(), getTestAmount());
EXPECT_EQ(proto->is_approval(), getTestIsApproval());
}
//-----
TEST_F(TokenTransferUnitTests, OperatorEqualsSame)
{
// Given
// Default
TokenTransfer defaultTokenL;
TokenTransfer defaultTokenR;
// Identical
TokenTransfer equalTokenL(
getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
TokenTransfer equalTokenR(
getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
// Then
EXPECT_TRUE(defaultTokenL == defaultTokenR);
EXPECT_TRUE(equalTokenL == equalTokenR);
}
//-----
TEST_F(TokenTransferUnitTests, OperatorEqualsDiff)
{
// Given
TokenTransfer testTokenTransfer(getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), true);
TokenTransfer diffTestToken( TokenId(505ULL), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
TokenTransfer diffTestAccountId(getTestTokenId(), AccountId(12345ULL), getTestAmount(), getTestExpectedDecimals(), getTestIsApproval());
TokenTransfer diffTestAmount(getTestTokenId(), getTestAccountId(), 500LL, getTestExpectedDecimals(), getTestIsApproval());
TokenTransfer diffTestExpectedDecimals(getTestTokenId(), getTestAccountId(), getTestAmount(), 2U, getTestIsApproval());
TokenTransfer diffTestIsApproval(getTestTokenId(), getTestAccountId(), getTestAmount(), getTestExpectedDecimals(), false);
// Then
EXPECT_FALSE(testTokenTransfer == diffTestToken);
EXPECT_FALSE(testTokenTransfer == diffTestAccountId);
EXPECT_FALSE(testTokenTransfer == diffTestAmount);
EXPECT_FALSE(testTokenTransfer == diffTestExpectedDecimals);
EXPECT_FALSE(testTokenTransfer == diffTestIsApproval);
}