1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*
* Copyright (C) 2016-2018 Olzhas Rakhimov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xml_stream.h"
#include <catch.hpp>
namespace scram::xml::test {
/// This fixture provides document stream for tests.
class XmlStreamTest {
public:
XmlStreamTest() : xml_stream_(stderr) {}
~XmlStreamTest() noexcept {}
protected:
Stream xml_stream_; ///< The stream to add elements into per test case.
};
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement ctor", "[xml_stream]") {
CHECK_THROWS_AS(xml_stream_.root(""), StreamError);
CHECK_NOTHROW(xml_stream_.root("element"));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::Stream ctor", "[xml_stream]") {
CHECK_NOTHROW(xml_stream_.root("root"));
CHECK_THROWS_AS(xml_stream_.root("root"), StreamError);
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement SetAttribute",
"[xml_stream]") {
StreamElement el = xml_stream_.root("element");
CHECK_THROWS_AS(el.SetAttribute("", "value"), StreamError);
CHECK_NOTHROW(el.SetAttribute("attr1", "value"));
CHECK_NOTHROW(el.SetAttribute("attr2", ""));
CHECK_NOTHROW(el.SetAttribute("attr3", 7));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement AddText", "[xml_stream]") {
StreamElement el = xml_stream_.root("element");
CHECK_NOTHROW(el.AddText("text"));
CHECK_NOTHROW(el.AddText(7));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement AddChild", "[xml_stream]") {
StreamElement el = xml_stream_.root("element");
CHECK_THROWS_AS(el.AddChild(""), StreamError);
CHECK_NOTHROW(el.AddChild("child"));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement StateAfterSetAttribute",
"[xml_stream]") {
StreamElement root = xml_stream_.root("root");
{
StreamElement el = root.AddChild("element");
CHECK_NOTHROW(el.SetAttribute("attr", "value"));
CHECK_NOTHROW(el.AddText("text"));
}
{
StreamElement el = root.AddChild("element");
CHECK_NOTHROW(el.SetAttribute("attr", "value"));
CHECK_NOTHROW(el.AddChild("child"));
}
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement StateAfterAddText",
"[xml_stream]") {
StreamElement el = xml_stream_.root("element");
CHECK_NOTHROW(el.AddText("text")); // Locks on text.
CHECK_THROWS_AS(el.SetAttribute("attr", "value"), StreamError);
CHECK_THROWS_AS(el.AddChild("another_child"), StreamError);
CHECK_NOTHROW(el.AddText(" and continuation..."));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement StateAfterAddChild",
"[xml_stream]") {
StreamElement el = xml_stream_.root("element");
CHECK_NOTHROW(el.AddChild("child")); // Locks on elements.
CHECK_THROWS_AS(el.SetAttribute("attr", "value"), StreamError);
CHECK_THROWS_AS(el.AddText("text"), StreamError);
CHECK_NOTHROW(el.AddChild("another_child"));
}
TEST_CASE_METHOD(XmlStreamTest, "xml::StreamElement InactiveParent",
"[xml_stream]") {
StreamElement el = xml_stream_.root("element");
{
StreamElement child = el.AddChild("child"); // Make the parent inactive.
CHECK_THROWS_AS(el.SetAttribute("attr", "value"), StreamError);
CHECK_THROWS_AS(el.AddText("text"), StreamError);
CHECK_THROWS_AS(el.AddChild("another_child"), StreamError);
// Child must be active without problems.
CHECK_NOTHROW(child.SetAttribute("sub_attr", "value"));
CHECK_NOTHROW(child.AddChild("sub_child"));
} // Lock is off at the scope exit.
CHECK_NOTHROW(el.AddChild("another_child"));
}
} // namespace scram::xml::test
|