Skip to content

Commit cefaf49

Browse files
committed
unit tests: close paymentrequest model gaps
Expand PaymentRequest tests to cover address encoding, formatted address grouping, destination getter/reset behavior, repeated destination signal emission, and id boundary values. Align amount display expectations with current BitcoinAmount unset semantics (empty display after default construction and clear).
1 parent 1ad58a9 commit cefaf49

1 file changed

Lines changed: 90 additions & 2 deletions

File tree

test/test_paymentrequest.cpp

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44

55
#include <QtTest/QtTest>
66

7+
#include <key_io.h>
78
#include <qml/models/paymentrequest.h>
89

10+
#include <limits>
11+
912
class PaymentRequestTests : public QObject
1013
{
1114
Q_OBJECT
1215

1316
private Q_SLOTS:
1417
void defaultState_isEmpty();
18+
void address_returnsEncodedDestination();
19+
void addressFormatted_groupsAddressByFourCharacters();
1520
void setters_emitOnlyOnChange();
1621
void setDestination_emitsAddressChanged();
22+
void destination_returnsLastSetValue_andClearResetsToNoDestination();
23+
void setDestination_sameValue_stillEmitsAddressChanged();
24+
void setId_handlesBoundaryValues();
1725
void clear_resetsAllFields();
1826
};
1927

@@ -26,7 +34,49 @@ void PaymentRequestTests::defaultState_isEmpty()
2634
QCOMPARE(request.message(), QString());
2735
QCOMPARE(request.amountError(), QString());
2836
QCOMPARE(request.id(), QString());
29-
QCOMPARE(request.amount()->toDisplay(), QString("0.00000000"));
37+
QCOMPARE(request.amount()->toDisplay(), QString());
38+
}
39+
40+
void PaymentRequestTests::address_returnsEncodedDestination()
41+
{
42+
PaymentRequest request;
43+
const CTxDestination first_destination = WitnessUnknown{1, std::vector<unsigned char>{0x01, 0x02, 0x03, 0x04}};
44+
const CTxDestination second_destination = WitnessUnknown{1, std::vector<unsigned char>{0x05, 0x06, 0x07, 0x08}};
45+
46+
request.setDestination(first_destination);
47+
const QString first_address = request.address();
48+
QVERIFY(!first_address.isEmpty());
49+
QCOMPARE(first_address, QString::fromStdString(EncodeDestination(first_destination)));
50+
51+
request.setDestination(second_destination);
52+
const QString second_address = request.address();
53+
QVERIFY(!second_address.isEmpty());
54+
QCOMPARE(second_address, QString::fromStdString(EncodeDestination(second_destination)));
55+
QVERIFY(first_address != second_address);
56+
}
57+
58+
void PaymentRequestTests::addressFormatted_groupsAddressByFourCharacters()
59+
{
60+
PaymentRequest request;
61+
request.setDestination(WitnessUnknown{1, std::vector<unsigned char>{0x01, 0x02, 0x03, 0x04}});
62+
63+
const QString raw_address = request.address();
64+
QVERIFY(!raw_address.isEmpty());
65+
66+
const QString formatted_address = request.addressFormatted();
67+
QVERIFY(formatted_address.contains(QChar(' ')));
68+
69+
QString without_spaces = formatted_address;
70+
without_spaces.remove(QChar(' '));
71+
QCOMPARE(without_spaces, raw_address);
72+
73+
const QStringList groups = formatted_address.split(QChar(' '), Qt::SkipEmptyParts);
74+
QVERIFY(!groups.isEmpty());
75+
for (int i = 0; i < groups.size() - 1; ++i) {
76+
QCOMPARE(groups[i].size(), 4);
77+
}
78+
QVERIFY(groups.back().size() > 0);
79+
QVERIFY(groups.back().size() <= 4);
3080
}
3181

3282
void PaymentRequestTests::setters_emitOnlyOnChange()
@@ -71,6 +121,44 @@ void PaymentRequestTests::setDestination_emitsAddressChanged()
71121
QCOMPARE(request.addressFormatted(), QString());
72122
}
73123

124+
void PaymentRequestTests::destination_returnsLastSetValue_andClearResetsToNoDestination()
125+
{
126+
PaymentRequest request;
127+
const CTxDestination destination = WitnessUnknown{1, std::vector<unsigned char>{0x01, 0x02, 0x03, 0x04}};
128+
129+
request.setDestination(destination);
130+
QCOMPARE(request.destination(), destination);
131+
132+
request.clear();
133+
QCOMPARE(request.destination(), CTxDestination{CNoDestination{}});
134+
}
135+
136+
void PaymentRequestTests::setDestination_sameValue_stillEmitsAddressChanged()
137+
{
138+
PaymentRequest request;
139+
QSignalSpy address_spy(&request, &PaymentRequest::addressChanged);
140+
const CTxDestination destination = WitnessUnknown{1, std::vector<unsigned char>{0x0a, 0x0b, 0x0c, 0x0d}};
141+
142+
request.setDestination(destination);
143+
request.setDestination(destination);
144+
145+
QCOMPARE(address_spy.count(), 2);
146+
}
147+
148+
void PaymentRequestTests::setId_handlesBoundaryValues()
149+
{
150+
PaymentRequest request;
151+
QSignalSpy id_spy(&request, &PaymentRequest::idChanged);
152+
153+
request.setId(0u);
154+
QCOMPARE(request.id(), QString("0"));
155+
QCOMPARE(id_spy.count(), 1);
156+
157+
request.setId(std::numeric_limits<unsigned int>::max());
158+
QCOMPARE(request.id(), QString::number(std::numeric_limits<unsigned int>::max()));
159+
QCOMPARE(id_spy.count(), 2);
160+
}
161+
74162
void PaymentRequestTests::clear_resetsAllFields()
75163
{
76164
PaymentRequest request;
@@ -94,7 +182,7 @@ void PaymentRequestTests::clear_resetsAllFields()
94182
QCOMPARE(request.message(), QString());
95183
QCOMPARE(request.amountError(), QString());
96184
QCOMPARE(request.id(), QString());
97-
QCOMPARE(request.amount()->toDisplay(), QString("0.00000000"));
185+
QCOMPARE(request.amount()->toDisplay(), QString());
98186
QCOMPARE(address_spy.count(), 1);
99187
QCOMPARE(label_spy.count(), 1);
100188
QCOMPARE(message_spy.count(), 1);

0 commit comments

Comments
 (0)