Skip to content

Commit

Permalink
Base: fix PropertyValueConversionVariant::toString() for ByteArray type
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Jan 8, 2025
1 parent e6b9426 commit 6ef4577
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/base/property_value_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,19 @@ double PropertyValueConversion::Variant::toDouble(bool* ok) const
std::string PropertyValueConversion::Variant::toString(bool* ok) const
{
assignBoolPtr(ok, true);
if (std::holds_alternative<int>(*this))
if (std::holds_alternative<int>(*this)) {
return std::to_string(std::get<int>(*this));
else if (std::holds_alternative<double>(*this))
return std::to_string(std::get<double>(*this));
else if (std::holds_alternative<std::vector<uint8_t>>(*this))
}
else if (std::holds_alternative<double>(*this)) {
return std::to_string(std::get<double>(*this));
else
}
else if (std::holds_alternative<std::vector<uint8_t>>(*this)) {
const auto bytes = this->toConstRefByteArray(ok);
return std::string{ bytes.begin(), bytes.end() };
}
else {
return this->toConstRefString();
}
}

const std::string& PropertyValueConversion::Variant::toConstRefString(bool* ok) const
Expand Down
34 changes: 34 additions & 0 deletions tests/test_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,40 @@ void TestBase::PropertyValueConversionVariant_doubleToInt_test_data()
QTest::newRow("INT_MIN") << double(INT_MIN) << true;
}

void TestBase::PropertyValueConversionVariant_toString_test()
{
QFETCH(PropertyValueConversion::Variant, variantValue);
QFETCH(std::string, toString);

bool ok = false;
if (std::holds_alternative<double>(variantValue)) {
const std::string str = variantValue.toString(&ok);
QCOMPARE(std::stod(str), std::stod(toString));
}
else {
QCOMPARE(variantValue.toString(&ok), toString);
}

QVERIFY(ok);
}

void TestBase::PropertyValueConversionVariant_toString_test_data()
{
using Variant = PropertyValueConversion::Variant;
QTest::addColumn<Variant>("variantValue");
QTest::addColumn<std::string>("toString");

QTest::newRow("Variant{false}") << Variant{false} << std::string{"false"};
QTest::newRow("Variant{true}") << Variant{true} << std::string{"true"};
QTest::newRow("Variant{57}") << Variant{57} << std::string{"57"};
QTest::newRow("Variant{4.57f}") << Variant{4.57f} << std::string{"4.57"};
QTest::newRow("Variant{1.25}") << Variant{1.25} << std::string{"1.25"};
QTest::newRow("Variant{'some string'}") << Variant{"some string"} << std::string{"some string"};

const uint8_t bytes[] = { 48, 65 }; // ascii: 0A
QTest::newRow("Variant{bytes}") << Variant{Span<const uint8_t>(bytes)} << std::string{"0A"};
}

void TestBase::PropertyValueConversion_test()
{
QFETCH(QString, strPropertyName);
Expand Down
3 changes: 3 additions & 0 deletions tests/test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ private slots:
void PropertyValueConversionVariant_doubleToInt_test();
void PropertyValueConversionVariant_doubleToInt_test_data();

void PropertyValueConversionVariant_toString_test();
void PropertyValueConversionVariant_toString_test_data();

void PropertyValueConversion_test();
void PropertyValueConversion_test_data();
void PropertyValueConversion_bugGitHub219_test();
Expand Down

0 comments on commit 6ef4577

Please sign in to comment.