xlnt/tests/helpers/helper.hpp

153 lines
4.4 KiB
C++
Raw Normal View History

2014-06-05 06:42:17 +08:00
#pragma once
#include <sstream>
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/xml_document.hpp>
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/serialization/xml_serializer.hpp>
2014-06-05 06:42:17 +08:00
2014-06-06 04:19:31 +08:00
#include "path_helper.hpp"
2014-06-05 06:42:17 +08:00
class Helper
{
public:
2014-07-20 02:43:48 +08:00
enum class difference_type
2014-06-05 06:42:17 +08:00
{
2014-07-20 02:43:48 +08:00
names_differ,
missing_attribute,
attribute_values_differ,
missing_text,
text_values_differ,
missing_child,
child_order_differs,
equivalent,
};
struct comparison_result
{
difference_type difference;
2015-11-11 08:46:57 +08:00
std::string value_left;
std::string value_right;
2014-07-20 02:43:48 +08:00
operator bool() const { return difference == difference_type::equivalent; }
};
2015-10-31 06:54:04 +08:00
static comparison_result compare_xml(const xlnt::xml_document &expected, const xlnt::xml_document &observed)
{
return compare_xml(expected.get_root(), observed.get_root());
}
2015-11-11 08:46:57 +08:00
static comparison_result compare_xml(const std::string &expected, const xlnt::xml_document &observed)
2015-10-14 12:03:48 +08:00
{
2015-11-11 08:46:57 +08:00
std::string expected_contents = expected;
2015-10-31 06:54:04 +08:00
if(PathHelper::FileExists(expected))
{
2015-11-11 08:46:57 +08:00
std::ifstream f(expected);
2015-10-31 06:54:04 +08:00
std::ostringstream s;
f >> s.rdbuf();
2015-11-11 08:46:57 +08:00
expected_contents = s.str();
2015-10-31 06:54:04 +08:00
}
2015-10-15 06:05:13 +08:00
2015-11-03 03:22:13 +08:00
xlnt::xml_document expected_xml;
2015-11-11 08:46:57 +08:00
expected_xml.from_string(expected_contents);
2015-11-03 03:22:13 +08:00
2016-05-12 07:24:53 +08:00
auto observed_string = observed.to_string();
2015-10-31 06:54:04 +08:00
return compare_xml(expected_xml.get_root(), observed.get_root());
}
2015-11-11 08:46:57 +08:00
static comparison_result compare_xml(const std::string &left_contents, const std::string &right_contents)
{
2015-11-03 03:22:13 +08:00
xlnt::xml_document left_xml;
2015-11-11 08:46:57 +08:00
left_xml.from_string(left_contents);
2015-11-03 03:22:13 +08:00
xlnt::xml_document right_xml;
2015-11-11 08:46:57 +08:00
right_xml.from_string(right_contents);
2015-11-03 03:22:13 +08:00
return compare_xml(left_xml.get_root(), right_xml.get_root());
2015-10-14 12:03:48 +08:00
}
static comparison_result compare_xml(const xlnt::xml_node &left, const xlnt::xml_node &right)
2014-07-20 02:43:48 +08:00
{
2015-11-11 08:46:57 +08:00
std::string left_temp = left.get_name();
std::string right_temp = right.get_name();
2014-07-20 02:43:48 +08:00
if(left_temp != right_temp)
2014-07-17 07:53:45 +08:00
{
2014-07-20 02:43:48 +08:00
return {difference_type::names_differ, left_temp, right_temp};
2014-07-17 07:53:45 +08:00
}
2014-07-20 02:43:48 +08:00
for(auto &left_attribute : left.get_attributes())
2014-07-17 07:53:45 +08:00
{
left_temp = left_attribute.second;
2014-07-20 02:43:48 +08:00
if(!right.has_attribute(left_attribute.first))
2014-07-20 02:43:48 +08:00
{
return {difference_type::missing_attribute, left_temp, "((empty))"};
}
left_temp = left_attribute.second;
right_temp = right.get_attribute(left_attribute.first);
2014-07-20 02:43:48 +08:00
if(left_temp != right_temp)
{
return {difference_type::attribute_values_differ, left_temp, right_temp};
}
2014-07-17 07:53:45 +08:00
}
2014-07-20 02:43:48 +08:00
if(left.has_text())
2014-07-20 02:43:48 +08:00
{
left_temp = left.get_text();
2014-07-20 02:43:48 +08:00
if(!right.has_text())
2014-07-20 02:43:48 +08:00
{
return {difference_type::missing_text, left_temp, "((empty))"};
}
right_temp = right.get_text();
2014-07-20 02:43:48 +08:00
if(left_temp != right_temp)
{
return {difference_type::text_values_differ, left_temp, right_temp};
}
}
else if(right.has_text())
2014-07-20 02:43:48 +08:00
{
right_temp = right.get_text();
2014-07-20 02:43:48 +08:00
return {difference_type::text_values_differ, "((empty))", right_temp};
}
2015-10-31 06:54:04 +08:00
auto right_children = right.get_children();
auto right_child_iter = right_children.begin();
for(auto left_child : left.get_children())
2014-07-20 02:43:48 +08:00
{
left_temp = left_child.get_name();
2014-07-20 02:43:48 +08:00
2015-10-31 06:54:04 +08:00
if(right_child_iter == right_children.end())
2014-07-20 02:43:48 +08:00
{
return {difference_type::child_order_differs, left_temp, "((end))"};
}
auto right_child = *right_child_iter;
right_child_iter++;
auto child_comparison_result = compare_xml(left_child, right_child);
if(!child_comparison_result)
{
return child_comparison_result;
}
}
2015-10-31 06:54:04 +08:00
if(right_child_iter != right_children.end())
2014-07-20 02:43:48 +08:00
{
right_temp = right_child_iter->get_name();
2014-07-20 02:43:48 +08:00
return {difference_type::child_order_differs, "((end))", right_temp};
}
return {difference_type::equivalent, "", ""};
}
2014-06-05 06:42:17 +08:00
};