From 12607d60470b34a7381d269a8a916f7ff1ff5a61 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 29 Aug 2010 15:07:18 +0000 Subject: tests: Extended XPath tests git-svn-id: http://pugixml.googlecode.com/svn/trunk@638 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test.hpp | 1 + tests/test_xpath_api.cpp | 114 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 89 insertions(+), 26 deletions(-) (limited to 'tests') diff --git a/tests/test.hpp b/tests/test.hpp index c269fb5..3308b1e 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -105,6 +105,7 @@ struct dummy_fixture {}; #define CHECK_JOIN(text, file, line) text file #line #define CHECK_JOIN2(text, file, line) CHECK_JOIN(text, file, line) #define CHECK_TEXT(condition, text) if (condition) ; else test_runner::_failure_message = CHECK_JOIN2(text, " at "__FILE__ ":", __LINE__), longjmp(test_runner::_failure_buffer, 1) +#define CHECK_FORCE_FAIL(text) test_runner::_failure_message = CHECK_JOIN2(text, " at "__FILE__ ":", __LINE__), longjmp(test_runner::_failure_buffer, 1) #if (defined(_MSC_VER) && _MSC_VER == 1200) || defined(__MWERKS__) # define STRINGIZE(value) "??" // MSVC 6.0 and CodeWarrior have troubles stringizing stuff with strings w/escaping inside diff --git a/tests/test_xpath_api.cpp b/tests/test_xpath_api.cpp index 9f09826..87cc40a 100644 --- a/tests/test_xpath_api.cpp +++ b/tests/test_xpath_api.cpp @@ -38,22 +38,6 @@ TEST_XML(xpath_api_select_single_node, "< CHECK(n5.node().attribute(STR("id")).as_int() == 1); } -#ifndef PUGIXML_NO_EXCEPTIONS -TEST(xpath_api_exception_what) -{ - try - { - xpath_query q(STR("")); - - CHECK(!"Expected exception"); - } - catch (const xpath_exception& e) - { - CHECK(e.what()[0] != 0); - } -} -#endif - TEST_XML(xpath_api_node_bool_ops, "") { generic_bool_ops_test(doc.select_single_node(STR("node"))); @@ -157,38 +141,116 @@ TEST_XML(xpath_api_evaluate, "") } #ifdef PUGIXML_NO_EXCEPTIONS -TEST(xpath_api_evaluate_node_set) +TEST_XML(xpath_api_evaluate_fail, "") { - CHECK_XPATH_NODESET(xml_node(), STR("1")); + CHECK_XPATH_BOOLEAN(doc, STR(""), false); + CHECK_XPATH_NUMBER_NAN(doc, STR("")); + CHECK_XPATH_STRING(doc, STR(""), STR("")); + CHECK_XPATH_NODESET(doc, STR("")); } -#else -TEST(xpath_api_evaluate_node_set) +#endif + +TEST(xpath_api_evaluate_node_set_fail) { +#ifdef PUGIXML_NO_EXCEPTIONS + CHECK_XPATH_NODESET(xml_node(), STR("1")); +#else try { xpath_query q(STR("1")); q.evaluate_node_set(xml_node()); - CHECK(!"Expected exception"); + CHECK_FORCE_FAIL("Expected exception"); } catch (const xpath_exception&) { } -} #endif +} TEST(xpath_api_return_type) { +#ifdef PUGIXML_NO_EXCEPTIONS + CHECK(xpath_query(STR("")).return_type() == xpath_type_none); +#endif + CHECK(xpath_query(STR("node")).return_type() == xpath_type_node_set); CHECK(xpath_query(STR("1")).return_type() == xpath_type_number); CHECK(xpath_query(STR("'s'")).return_type() == xpath_type_string); CHECK(xpath_query(STR("true()")).return_type() == xpath_type_boolean); } +TEST(xpath_api_query_bool) +{ + xpath_query q(STR("node")); + + CHECK(q); + CHECK((!q) == false); +} + +#ifdef PUGIXML_NO_EXCEPTIONS +TEST(xpath_api_query_bool_fail) +{ + xpath_query q(STR("")); + + CHECK((q ? true : false) == false); + CHECK((!q) == true); +} +#endif + +TEST(xpath_api_query_result) +{ + xpath_query q(STR("node")); + + CHECK(q.result()); + CHECK(q.result().error == 0); + CHECK(q.result().offset == 0); + CHECK(strcmp(q.result().description(), "No error") == 0); +} + +TEST(xpath_api_query_result_fail) +{ +#ifndef PUGIXML_NO_EXCEPTIONS + try + { +#endif + xpath_query q(STR("string-length(1, 2, 3)")); + +#ifndef PUGIXML_NO_EXCEPTIONS + CHECK_FORCE_FAIL("Expected exception"); + } + catch (const xpath_exception& q) + { +#endif + xpath_parse_result result = q.result(); + + CHECK(!result); + CHECK(result.error != 0 && result.error[0] != 0); + CHECK(result.description() == result.error); + CHECK(result.offset == 0); // $$$ + +#ifndef PUGIXML_NO_EXCEPTIONS + } +#endif +} + +#ifndef PUGIXML_NO_EXCEPTIONS +TEST(xpath_api_exception_what) +{ + try + { + xpath_query q(STR("")); + + CHECK_FORCE_FAIL("Expected exception"); + } + catch (const xpath_exception& e) + { + CHECK(e.what()[0] != 0); + } +} +#endif + // $$$ -// xpath_query bool conversion -// xpath_query::result / xpath_exception::result -// result offset -// xpath_query::rettype for no root +// out of memory during parsing/execution (?) #endif -- cgit v1.2.3