Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions include/ro-crate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,20 @@ namespace rocrate {
Entity& addEntity(const std::string& id, std::vector<std::string> types);

/**
* Retrieves an entity from the RO-Crate's entity register by its id.
*
* @param id The identifier of the entity to retrieve.
* @return A reference to the entity with the specified id.
* @throw std::runtime_error if no entity with the given id is found in the RO-Crate.
*/
* Checks if an entity with the specified id exists in the RO-Crate's entity register.
*
* @param id The identifier of the entity to check.
* @return true if an entity with the given id exists, false otherwise.
*/
bool hasEntity(const std::string& id);

/**
* Retrieves an entity from the RO-Crate's entity register by its id.
*
* @param id The identifier of the entity to retrieve.
* @return A reference to the entity with the specified id.
* @throw std::runtime_error if no entity with the given id exists in the RO-Crate.
*/
Entity& getEntity(const std::string& id);

/**
Expand Down Expand Up @@ -291,6 +299,10 @@ namespace rocrate {
return entities_.at(id);
}

inline bool ROCrate::hasEntity(const std::string& id) {
return entities_.find(id) != entities_.end();
}

inline Entity& ROCrate::getEntity(const std::string& id) {
auto it = entities_.find(id);
if (it == entities_.end()) {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ TEST_CASE("Ignore duplicate key-value pairs", "[unit]") {
// Unit tests for RO-Crate class
// -------------------------------------------------------------------------------

TEST_CASE("hasEntity returns true/false correctly", "[unit]") {
rocrate::ROCrate crate;
REQUIRE(crate.hasEntity("./"));
REQUIRE_FALSE(crate.hasEntity("#nonexistent"));
}

TEST_CASE("Get entity throws if entity not found", "[unit]") {
// Create a crate
rocrate::ROCrate crate;
Expand Down
Loading