Skip to content

Commit 94ec138

Browse files
committed
Add: Allow to set and reset the text on an XML command object
Not every XML element will have a value/text. Therefore it should be possible to set it optionally.
1 parent 07caf94 commit 94ec138

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

gvm/xml.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ def set_attributes(self, attrs: dict[str, str]) -> "XmlCommandElement":
123123

124124
return self
125125

126+
def set_text(self, text: Optional[str]) -> "XmlCommandElement":
127+
"""Set the text of the element.
128+
129+
Args:
130+
text: Text to be set on the element. None to remove the text.
131+
"""
132+
self._element.text = text
133+
return self
134+
126135
def append_xml_str(self, xml_text: str) -> None:
127136
"""Append a xml element in string format."""
128137
node = parse_xml(xml_text)

tests/xml/test_xml_command.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ def test_should_allow_to_set_attributes(self):
3939

4040
self.assertEqual(cmd.to_string(), '<foo bar="1" baz="2"/>')
4141

42+
def test_should_allow_to_set_text(self):
43+
cmd = XmlCommand("foo")
44+
cmd.set_text("bar")
45+
self.assertEqual(cmd.to_string(), "<foo>bar</foo>")
46+
47+
def test_should_allow_to_reset_text(self):
48+
cmd = XmlCommand("foo")
49+
sub_cmd = cmd.add_element("bar", "baz")
50+
sub_cmd.set_text("qux")
51+
52+
self.assertEqual(cmd.to_string(), "<foo><bar>qux</bar></foo>")
53+
4254
def test_should_convert_to_string(self):
4355
cmd = XmlCommand("foo")
4456

0 commit comments

Comments
 (0)