diff --git a/include/ro-crate.hpp b/include/ro-crate.hpp index 9ba4094..4b648b9 100644 --- a/include/ro-crate.hpp +++ b/include/ro-crate.hpp @@ -185,12 +185,20 @@ namespace rocrate { Entity& addEntity(const std::string& id, std::vector 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); /** @@ -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()) { diff --git a/tests/unit.cpp b/tests/unit.cpp index d9fde6e..da496d3 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -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;