From 1cd736905b4f98f419b2903d040e40103f100621 Mon Sep 17 00:00:00 2001
From: "arseny.kapoulkine@gmail.com"
 <arseny.kapoulkine@gmail.com@99668b35-9821-0410-8761-19e4c4f06640>
Date: Fri, 23 Mar 2012 05:38:08 +0000
Subject: tests: Added text/binary save_file tests

git-svn-id: http://pugixml.googlecode.com/svn/trunk@884 99668b35-9821-0410-8761-19e4c4f06640
---
 tests/test_document.cpp | 73 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 56 insertions(+), 17 deletions(-)

(limited to 'tests')

diff --git a/tests/test_document.cpp b/tests/test_document.cpp
index c8345f0..c03159d 100644
--- a/tests/test_document.cpp
+++ b/tests/test_document.cpp
@@ -26,6 +26,36 @@
 #	include <unistd.h> // for unlink
 #endif
 
+static bool load_file_in_memory(const char* path, char*& data, size_t& size)
+{
+	FILE* file = fopen(path, "rb");
+	if (!file) return false;
+
+	fseek(file, 0, SEEK_END);
+	size = static_cast<size_t>(ftell(file));
+	fseek(file, 0, SEEK_SET);
+
+	data = new char[size];
+
+	CHECK(fread(data, 1, size, file) == size);
+	fclose(file);
+
+	return true;
+}
+
+static bool test_file_contents(const char* path, const char* data, size_t size)
+{
+    char* fdata;
+    size_t fsize;
+    if (!load_file_in_memory(path, fdata, fsize)) return false;
+
+    bool result = (size == fsize && memcmp(data, fdata, size) == 0);
+
+    delete[] fdata;
+
+    return result;
+}
+
 TEST(document_create_empty)
 {
 	pugi::xml_document doc;
@@ -444,6 +474,32 @@ TEST_XML(document_save_file_error, "<node/>")
 	CHECK(!doc.save_file("tests/data/unknown/output.xml"));
 }
 
+TEST_XML(document_save_file_text, "<node/>")
+{
+	temp_file f;
+
+	CHECK(doc.save_file(f.path, STR(""), pugi::format_no_declaration | pugi::format_save_file_text));
+    CHECK(test_file_contents(f.path, "<node />\n", 9) || test_file_contents(f.path, "<node />\r\n", 10));
+
+	CHECK(doc.save_file(f.path, STR(""), pugi::format_no_declaration));
+    CHECK(test_file_contents(f.path, "<node />\n", 9));
+}
+
+TEST_XML(document_save_file_wide_text, "<node/>")
+{
+	temp_file f;
+
+	// widen the path
+	wchar_t wpath[32];
+	std::copy(f.path, f.path + strlen(f.path) + 1, wpath + 0);
+
+	CHECK(doc.save_file(wpath, STR(""), pugi::format_no_declaration | pugi::format_save_file_text));
+    CHECK(test_file_contents(f.path, "<node />\n", 9) || test_file_contents(f.path, "<node />\r\n", 10));
+
+	CHECK(doc.save_file(wpath, STR(""), pugi::format_no_declaration));
+    CHECK(test_file_contents(f.path, "<node />\n", 9));
+}
+
 TEST(document_load_buffer)
 {
 	const pugi::char_t text[] = STR("<?xml?><node/>");
@@ -701,23 +757,6 @@ TEST(document_load_file_convert_native_endianness)
 	}
 }
 
-static bool load_file_in_memory(const char* path, char*& data, size_t& size)
-{
-	FILE* file = fopen(path, "rb");
-	if (!file) return false;
-
-	fseek(file, 0, SEEK_END);
-	size = static_cast<size_t>(ftell(file));
-	fseek(file, 0, SEEK_SET);
-
-	data = new char[size];
-
-	CHECK(fread(data, 1, size, file) == size);
-	fclose(file);
-
-	return true;
-}
-
 struct file_data_t
 {
     const char* path;
-- 
cgit v1.2.3