Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#6097: xmlutil::Document unit tests
  • Loading branch information
codereader committed Sep 11, 2022
1 parent ffb7602 commit c1348a6
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 8 deletions.
102 changes: 94 additions & 8 deletions test/XmlUtil.cpp
Expand Up @@ -5,6 +5,7 @@
#include "RadiantTest.h"
#include "algorithm/FileUtils.h"
#include "string/trim.h"
#include "testutil/TemporaryFile.h"
#include "xmlutil/Document.h"

namespace test
Expand Down Expand Up @@ -45,11 +46,6 @@ TEST_F(XmlTest, CreateDocumentFromStream)
EXPECT_EQ(document.getTopLevelNode().getName(), "testDocument");
}

TEST_F(XmlTest, CreateDocumentCopy)
{
// Copy-construct an XML document, prove that the copies are not linked
}

TEST_F(XmlTest, AddTopLevelNodeToDocument)
{
// Create an empty document and add a top level node to it
Expand All @@ -66,12 +62,56 @@ TEST_F(XmlTest, AddTopLevelNodeToDocument)

TEST_F(XmlTest, ImportDocument)
{
// Insert a document/node tree into an existing document
// Create an empty document, add an <importNode /> tag without children
auto importDocument = xml::Document::create();
importDocument.addTopLevelNode("importNode");

// Load an existing file
xml::Document document(_context.getTestResourcePath() + TEST_XML_FILE);

// Import the <importNode /> into this document
auto targetNode = document.findXPath("//colourscheme[@name='Black & Green']").at(0);
auto previousChildCount = document.findXPath("//colourscheme[@name='Black & Green']/*").size();

document.importDocument(importDocument, targetNode);

auto newChildCount = document.findXPath("//colourscheme[@name='Black & Green']/*").size();
EXPECT_EQ(newChildCount, previousChildCount + 1);

// Importing a node to a target node without existing children should work too
targetNode = document.findXPath("//colourscheme[@name='Black & Green']/colour[@name='active_view_name']").at(0);

// The target node shouldn't have any children yet
EXPECT_EQ(document.findXPath("//colourscheme[@name='Black & Green']/colour[@name='active_view_name']/*").size(), 0);

auto importDocument2 = xml::Document::create();
importDocument2.addTopLevelNode("anotherNode");

document.importDocument(importDocument2, targetNode);

// We should have one child node now
EXPECT_EQ(document.findXPath("//colourscheme[@name='Black & Green']/colour[@name='active_view_name']/*").size(), 1);
}

TEST_F(XmlTest, CopyNodesIntoDocument)
{
// Copy existing nodes into this document
auto targetDocument = xml::Document::create();
targetDocument.addTopLevelNode("importNode");

// Load an existing file
xml::Document document(_context.getTestResourcePath() + TEST_XML_FILE);

// Copy all colourScheme nodes
auto importNodes = document.findXPath("//colourscheme");
EXPECT_EQ(importNodes.size(), 2) << "Expect 2 colour schemes to be present";

EXPECT_EQ(targetDocument.findXPath("//colourscheme").size(), 0) << "Target document shouldn't have any colourscheme nodes yet";

targetDocument.copyNodes(importNodes);

// The 2 colour schemes should have been added below the top level node
EXPECT_EQ(targetDocument.findXPath("/importNode/colourscheme").size(), 2) << "Target document should have 2 colourscheme nodes now";
}

TEST_F(XmlTest, DocumentValidityCheck)
Expand All @@ -92,11 +132,57 @@ TEST_F(XmlTest, DocumentValidityCheck)
TEST_F(XmlTest, FindXPathInDocument)
{
// Test the findXPath() method
xml::Document document(_context.getTestResourcePath() + TEST_XML_FILE);

// Find the root element
EXPECT_EQ(document.findXPath("/testDocument").size(), 1);

// Find the root node everywhere
EXPECT_EQ(document.findXPath("//testDocument").size(), 1);

// Find the 2 colourScheme nodes
EXPECT_EQ(document.findXPath("/testDocument/colourscheme").size(), 2);

// Find the 2 colourScheme nodes with the double-slash search
EXPECT_EQ(document.findXPath("//colourscheme").size(), 2);

// Find the 2 colourScheme nodes using a wildcard expression
EXPECT_EQ(document.findXPath("/testDocument/*").size(), 2);

// XPath expressions are case sensitive
EXPECT_EQ(document.findXPath("/testDocument/colourSCHEME").size(), 0); // find nothing

// Find all colour nodes using various methods
EXPECT_EQ(document.findXPath("/testDocument/colourscheme/colour").size(), 62);
EXPECT_EQ(document.findXPath("/testDocument/colourscheme/*").size(), 62);
EXPECT_EQ(document.findXPath("/testDocument//colour").size(), 62);
EXPECT_EQ(document.findXPath("//colour").size(), 62);

// Find specific colour scheme(s)
EXPECT_EQ(document.findXPath("//colourscheme[@name='Black & Green']").size(), 1);
EXPECT_EQ(document.findXPath("//colourscheme[@version='1.0']").size(), 2);
EXPECT_EQ(document.findXPath("/testDocument/colourscheme[@name='Black & Green']").size(), 1);
EXPECT_EQ(document.findXPath("/testDocument/*[@name='Black & Green']").size(), 1);

// Ampersand doesn't need to be escaped, otherwise there's no result
EXPECT_EQ(document.findXPath("//colourscheme[@name='Black &amp; Green']").size(), 0);
}

TEST_F(XmlTest, SaveDocumentToFile)
{
// Test the saveToFile() method
auto filename = _context.getTestResourcePath() + TEST_XML_FILE;
auto targetFilePath = _context.getTestResourcePath() + TEST_XML_FILE + ".tmp";
TemporaryFile targetFile(targetFilePath);

xml::Document document(filename);
auto fileContents = algorithm::loadFileToString(filename);

// saveToFile() should produce the same file again, except for trailing line breaks
document.saveToFile(targetFilePath);
auto newFileContents = algorithm::loadFileToString(targetFilePath);

EXPECT_EQ(string::trim_copy(newFileContents), fileContents);
}

TEST_F(XmlTest, SaveDocumentToString)
Expand All @@ -108,8 +194,8 @@ TEST_F(XmlTest, SaveDocumentToString)

auto fileContents = algorithm::loadFileToString(filename);

// saveToString() should produce the same file again
EXPECT_EQ(document.saveToString(), fileContents);
// saveToString() should produce the same file again, except for trailing line breaks
EXPECT_EQ(string::trim_copy(document.saveToString()), fileContents);
}

}
6 changes: 6 additions & 0 deletions test/resources/xml/broken_file.xml
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<testDocument>
<colourscheme name="DarkRadiant Default" version="1.0">
<colour name="active_view_name" value="0.5 0 0.75"/>
</colourscheme>
</mismatching>
69 changes: 69 additions & 0 deletions test/resources/xml/testfile.xml
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<testDocument>
<colourscheme name="Black &amp; Green" version="1.0" readonly="1">
<colour name="active_view_name" value="0.7 0.7 0"/>
<colour name="axis_x" value="1 0 0"/>
<colour name="axis_y" value="0 1 0"/>
<colour name="axis_z" value="0 0 1"/>
<colour name="brush_size_info" value="0.65 0 0"/>
<colour name="brush_vertices" value="0 1 0"/>
<colour name="camera_background" value="0 0 0"/>
<colour name="camera_icon" value="0 0 1"/>
<colour name="clipper" value="0 0 1"/>
<colour name="default_brush" value="1 1 1"/>
<colour name="default_entity" value="1 0.5 0"/>
<colour name="drag_selection" value="1 0 0"/>
<colour name="grid_background" value="0 0 0"/>
<colour name="grid_block" value="0 0 1"/>
<colour name="grid_major" value="0.3 0.5 0.5"/>
<colour name="grid_minor" value="0.2 0.2 0.2"/>
<colour name="grid_text" value="1 1 1"/>
<colour name="light_startend_deselected" value="0 1 1"/>
<colour name="light_startend_selected" value="0 0 1"/>
<colour name="light_vertex_deselected" value="0 1 0"/>
<colour name="light_vertex_normal" value="1 0 0"/>
<colour name="light_vertex_selected" value="0 0 1"/>
<colour name="light_volumes" value="0 1 0"/>
<colour name="patch_vertex_corner" value="1 0 1"/>
<colour name="patch_vertex_inside" value="0 1 0"/>
<colour name="selected_brush" value="1 0 0"/>
<colour name="selected_brush_camera" value="1 0 0"/>
<colour name="selected_group_items" value="0 0.4 0.8"/>
<colour name="texture_background" value="0.25 0.25 0.25"/>
<colour name="workzone" value="1 0 0"/>
<colour name="xyview_crosshairs" value="0.2 0.9 0.2"/>
</colourscheme>
<colourscheme name="DarkRadiant Default" version="1.0">
<colour name="active_view_name" value="0.5 0 0.75"/>
<colour name="axis_x" value="1 0 0"/>
<colour name="axis_y" value="0 1 0"/>
<colour name="axis_z" value="0 0 1"/>
<colour name="brush_size_info" value="0.65 0 0"/>
<colour name="brush_vertices" value="0 1 0"/>
<colour name="camera_background" value="0.25 0.25 0.25"/>
<colour name="camera_icon" value="0 0 1"/>
<colour name="clipper" value="0 0 1"/>
<colour name="default_brush" value="0 0 0"/>
<colour name="default_entity" value="1 0.5 0"/>
<colour name="drag_selection" value="0.65 0 0"/>
<colour name="grid_background" value="1 1 1"/>
<colour name="grid_block" value="0 0 1"/>
<colour name="grid_major" value="0.5 0.5 0.5"/>
<colour name="grid_minor" value="0.75 0.75 0.75"/>
<colour name="grid_text" value="0 0 0"/>
<colour name="light_startend_deselected" value="0 1 1"/>
<colour name="light_startend_selected" value="0 0 1"/>
<colour name="light_vertex_deselected" value="0 1 0"/>
<colour name="light_vertex_normal" value="1 0 0"/>
<colour name="light_vertex_selected" value="0 0 1"/>
<colour name="light_volumes" value="0 1 0"/>
<colour name="patch_vertex_corner" value="1 0 1"/>
<colour name="patch_vertex_inside" value="0 1 0"/>
<colour name="selected_brush" value="1 0 0"/>
<colour name="selected_brush_camera" value="1 0 0"/>
<colour name="selected_group_items" value="0 0.4 0.8"/>
<colour name="texture_background" value="0.25 0.25 0.25"/>
<colour name="workzone" value="1 0 0"/>
<colour name="xyview_crosshairs" value="0.2 0.9 0.2"/>
</colourscheme>
</testDocument>

0 comments on commit c1348a6

Please sign in to comment.