Implement conversion to double

This commit is contained in:
TheAssassin
2018-11-09 01:32:18 +01:00
parent 73832323c8
commit a647205226
3 changed files with 21 additions and 0 deletions

View File

@@ -87,6 +87,12 @@ long DesktopFileEntry::asLong() const {
return lexical_cast<long>(value());
}
double DesktopFileEntry::asDouble() const {
d->assertValueNotEmpty();
return lexical_cast<double>(value());
}
std::vector<std::string> DesktopFileEntry::parseStringList() const {
const auto& value = this->value();

View File

@@ -58,6 +58,10 @@ public:
// throws boost::bad_lexical_cast in case of type errors
long asLong() const;
// convert value to double
// throws boost::bad_lexical_cast in case of type errors
double asDouble() const;
// split CSV list value into vector
// the separator used to split the string is a semicolon as per desktop file spec
std::vector<std::string> parseStringList() const;

View File

@@ -116,6 +116,17 @@ TEST_F(DesktopFileEntryFixture, testConversionToLong) {
ASSERT_THROW(emptyEntry.asLong(), std::invalid_argument);
}
TEST_F(DesktopFileEntryFixture, testConversionToDouble) {
DesktopFileEntry doubleEntry(key, "1.234567");
EXPECT_NEAR(doubleEntry.asDouble(), 1.234567, 0.00000001);
DesktopFileEntry brokenValueEntry(key, "abcd");
ASSERT_THROW(brokenValueEntry.asDouble(), bad_lexical_cast);
DesktopFileEntry emptyEntry(key, "");
ASSERT_THROW(emptyEntry.asDouble(), std::invalid_argument);
}
TEST_F(DesktopFileEntryFixture, testConversionToString) {
DesktopFileEntry entry(key, value);
auto result = static_cast<std::string>(entry);