Skip to content

Commit

Permalink
feat(element.hpp): overload[] for element
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 1, 2024
1 parent eab71e1 commit 7519e59
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int main()
std::string xml("<root attr=\"value\">Hello, world!<root/>");
auto elem = myxml::Element::Parse(xml);
std::cout << root->FirstText() << std::endl; // "Hello, world!";
std::cout << root["attr"] << std::endl; // "value"
std::cout << (*root)["attr"] << std::endl; // "value"
}
```

Expand Down
1 change: 1 addition & 0 deletions include/myxml/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace myxml
void SetName(std::string_view);
void SetAttribute(std::string key, std::string value);
void ExtendAttributes(std::map<std::string, std::string>);
std::string &operator[](const std::string &);

/* Implement Exportable */
virtual std::string ExportRaw() const override;
Expand Down
5 changes: 5 additions & 0 deletions src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ namespace myxml
this->attributes.insert(attris.begin(), attris.end());
}

std::string &Element::operator[](const std::string &key)
{
return this->attributes[key];
}

std::string Element::ExportRaw() const
{

Expand Down
9 changes: 9 additions & 0 deletions tests/element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ TEST_CASE("Element Functionality", "[element]")
REQUIRE(root->Elem("child")->NextElem()->GetName() == "sibiling");
REQUIRE(root->Elem("sibiling")->PrevElem()->GetName() == "child");
}

SECTION("Overload []")
{
root->SetAttribute("hello", "world");
REQUIRE((*root)["hello"] == "world");
REQUIRE(root->GetAttribute("hello") == "world");
(*root)["hello"] = "bar";
REQUIRE((*root)["hello"] == "bar");
}
}

0 comments on commit 7519e59

Please sign in to comment.