Skip to content

Commit

Permalink
More tests for IptcDataSets
Browse files Browse the repository at this point in the history
  • Loading branch information
piponazo committed Jan 27, 2022
1 parent 45a5d6d commit 26bb12e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
4 changes: 0 additions & 4 deletions include/exiv2/datasets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,6 @@ namespace Exiv2 {
//! Internal virtual copy constructor.
IptcKey* clone_() const override;

// DATA
#ifndef SWIG
static constexpr auto familyName_ = "Iptc";
#endif
uint16_t tag_; //!< Tag value
uint16_t record_; //!< Record value
std::string key_; //!< Key
Expand Down
31 changes: 14 additions & 17 deletions src/datasets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@

#include <iostream>
#include <iomanip>
#include <regex>
#include <sstream>

// *****************************************************************************
// class member definitions

namespace Exiv2 {
constexpr const char *familyName_{"Iptc"};
constexpr RecordInfo recordInfo_[] = {
{IptcDataSets::invalidRecord, "(invalid)", N_("(invalid)")},
{IptcDataSets::envelope, "Envelope", N_("IIM envelope record")},
Expand Down Expand Up @@ -616,29 +618,24 @@ namespace Exiv2 {

void IptcKey::decomposeKey()
{
/// \todo Use regex to check the expected format. Then process the 3 expected chunks
// Check that the key has the expected format with RE
static const std::regex re(R"((\w+)(\.\w+){2})");
std::smatch sm;
if (!std::regex_match(key_, sm, re)) {
throw Error(kerInvalidKey, key_);
}

// Get the family name, record name and dataSet name parts of the key
std::string::size_type pos1 = key_.find('.');
if (pos1 == std::string::npos)
throw Error(kerInvalidKey, key_);
auto posDot1 = key_.find('.');
auto posDot2 = key_.find('.', posDot1+1);

std::string familyName = key_.substr(0, pos1);
const std::string familyName = key_.substr(0, posDot1);
if (0 != strcmp(familyName.c_str(), familyName_)) {
throw Error(kerInvalidKey, key_);
}

std::string::size_type pos0 = pos1 + 1;
pos1 = key_.find('.', pos0);
if (pos1 == std::string::npos)
throw Error(kerInvalidKey, key_);

std::string recordName = key_.substr(pos0, pos1 - pos0);
if (recordName.empty())
throw Error(kerInvalidKey, key_);

std::string dataSetName = key_.substr(pos1 + 1);
if (dataSetName.empty())
throw Error(kerInvalidKey, key_);
std::string recordName = key_.substr(posDot1+1, posDot2 - posDot1 - 1);
std::string dataSetName = key_.substr(posDot2+1);

// Use the parts of the key to find dataSet and recordId
uint16_t recId = recordId(recordName);
Expand Down
45 changes: 45 additions & 0 deletions unitTests/test_datasets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,51 @@ TEST(IptcDataSets, dataSet_throwWithNonExistingRecordId)

// ----------------------

TEST(IptcDataSets, dataSetType_returnsExpectedTypeWhenRequestingValidDataset)
{
ASSERT_EQ(unsignedShort, IptcDataSets::dataSetType(IptcDataSets::ModelVersion, IptcDataSets::envelope));
ASSERT_EQ(Exiv2::string, IptcDataSets::dataSetType(IptcDataSets::Destination, IptcDataSets::envelope));

ASSERT_EQ(unsignedShort, IptcDataSets::dataSetType(IptcDataSets::RecordVersion, IptcDataSets::application2));
ASSERT_EQ(Exiv2::string, IptcDataSets::dataSetType(IptcDataSets::ObjectType, IptcDataSets::application2));
}

/// \todo probably better to throw exception here?
TEST(IptcDataSets, dataSetType_returnsStringTypeWhenRecordIdDoesNotExist)
{
ASSERT_EQ(Exiv2::string, IptcDataSets::dataSetType(1, 5));
}

// ----------------------

TEST(IptcDataSets, recordName_returnsExpectedNameWhenRequestingValidRecordId)
{
ASSERT_EQ("Envelope", IptcDataSets::recordName(IptcDataSets::envelope));
ASSERT_EQ("Application2", IptcDataSets::recordName(IptcDataSets::application2));
}

TEST(IptcDataSets, recordName_returnsHexStringWhenRecordIdDoesNotExist)
{
ASSERT_EQ("0x0000", IptcDataSets::recordName(0));
ASSERT_EQ("0x0003", IptcDataSets::recordName(3));
}

// ----------------------

TEST(IptcDataSets, recordDesc_returnsExpectedDescriptionWhenRequestingValidRecordId)
{
ASSERT_STREQ("IIM envelope record", IptcDataSets::recordDesc(IptcDataSets::envelope));
ASSERT_STREQ("IIM application record 2", IptcDataSets::recordDesc(IptcDataSets::application2));
}

TEST(IptcDataSets, recordDesc_)
{
ASSERT_STREQ("Unknown dataset", IptcDataSets::recordDesc(0));
ASSERT_STREQ("Unknown dataset", IptcDataSets::recordDesc(3));
}

// ----------------------

TEST(IptcDataSets, dataSetLists_printDatasetsIntoOstream)
{
std::ostringstream stream;
Expand Down

0 comments on commit 26bb12e

Please sign in to comment.