[glom] Added tests for some utility methods.



commit 26281658127da260e15ea22dc872f19f294494ac
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Dec 16 22:17:37 2013 +0100

    Added tests for some utility methods.

 Makefile_tests.am         |   12 ++++++
 tests/.gitignore          |    3 +
 tests/test_conversions.cc |   95 +++++++++++++++++++++++++++++++++++++++++++++
 tests/test_xml_utils.cc   |   79 +++++++++++++++++++++++++++++++++++++
 4 files changed, 189 insertions(+), 0 deletions(-)
---
diff --git a/Makefile_tests.am b/Makefile_tests.am
index fc3f767..93de3dc 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -52,6 +52,8 @@ check_PROGRAMS =                                              \
        tests/test_selfhosting_new_then_change_columns \
        tests/test_selfhosting_non_numeric_primary_keys \
        tests/test_selfhosting_sqlinjection \
+       tests/test_conversions \
+       tests/test_xml_utils \
        tests/import/test_parsing \
        tests/import/test_signals \
        tests/translations_po/test_document_export_po \
@@ -94,6 +96,8 @@ TESTS =       tests/test_document_load        \
        tests/test_selfhosting_new_then_change_columns \
        tests/test_selfhosting_non_numeric_primary_keys \
        tests/test_selfhosting_sqlinjection \
+       tests/test_conversions \
+       tests/test_xml_utils \
        tests/import/test_parsing \
        tests/import/test_signals \
        tests/translations_po/test_document_export_po \
@@ -182,6 +186,14 @@ tests_test_signal_reemit_SOURCES = tests/test_signal_reemit.cc
 tests_test_signal_reemit_LDADD = $(LIBGLOM_LIBS)
 tests_test_signal_reemit_CPPFLAGS = $(tests_cppflags)
 
+tests_test_conversions_SOURCES = tests/test_conversions.cc
+tests_test_conversions_LDADD = $(tests_ldadd)
+tests_test_conversions_CPPFLAGS = $(tests_cppflags)
+
+tests_test_xml_utils_SOURCES = tests/test_xml_utils.cc
+tests_test_xml_utils_LDADD = $(tests_ldadd)
+tests_test_xml_utils_CPPFLAGS = $(tests_cppflags)
+
 tests_python_test_load_python_library_SOURCES = tests/python/test_load_python_library.cc
 tests_python_test_load_python_library_LDADD = $(LIBGLOM_LIBS) $(DL_LIB)
 tests_python_test_load_python_library_CPPFLAGS = $(tests_cppflags)
diff --git a/tests/.gitignore b/tests/.gitignore
index 781f98c..e48b2b0 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -9,6 +9,7 @@
 /import/test_signals
 /translations_po/test_document_export_po
 /translations_po/test_document_import_po
+/test_conversions
 /test_document_autosave
 /test_document_load
 /test_document_load_and_change
@@ -40,6 +41,8 @@
 /test_selfhosting_sqlinjection
 /test_selfhosting_new_from_example_strangepath
 /test_selfhosting_new_then_change_columns
+/test_xml_utils
+
 
 coverage/
 
diff --git a/tests/test_conversions.cc b/tests/test_conversions.cc
new file mode 100644
index 0000000..4a42b17
--- /dev/null
+++ b/tests/test_conversions.cc
@@ -0,0 +1,95 @@
+/* Glom
+ *
+ * Copyright (C) 2013 Openismus GmbH
+ *
+ * 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 2 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, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ */
+
+#include <glom/libglom/init.h>
+#include <glom/libglom/data_structure/glomconversions.h>
+#include <iostream>
+#include <cstdlib>
+
+static bool test_string_value(const Glib::ustring& str)
+{
+  const Glom::Field::glom_field_type field_type = Glom::Field::TYPE_TEXT;
+
+  bool success = false;
+  const Gnome::Gda::Value value =
+    Glom::Conversions::parse_value(field_type, str, success, true);
+  if(!success)
+  {
+    std::cerr << G_STRFUNC << ": parse_value() failed." << std::endl;
+    return false;
+  }
+
+  const Glib::ustring retrieved = value.get_string();
+
+  if(str != retrieved)
+  {
+    std::cerr << G_STRFUNC << ": Got string=" << retrieved << ", instead of string=" << str << std::endl;
+    return false;
+  }
+
+  return true;
+}
+
+
+static bool test_numeric_value(double num)
+{
+  const Gnome::Gda::Value value =
+    Glom::Conversions::parse_value(num);
+
+  const double retrieved =
+    Glom::Conversions::get_double_for_gda_value_numeric(value);
+
+  if(num != retrieved)
+  {
+    std::cerr << G_STRFUNC << ": Got number=" << retrieved << ", instead of number=" << num << std::endl;
+    return false;
+  }
+
+  return true;
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  const Glib::ustring str = " Some value or other with a quote \" and leading space."; //Just to be awkward.
+  if(!test_string_value(str))
+    return EXIT_FAILURE;
+
+  /* TODO:
+  const Glib::Date date(11, Glib::Date::MAY, 1973);
+  if(!test_value(Glom::Field::TYPE_DATE, Gnome::Gda::Value(date)))
+    return EXIT_FAILURE;
+
+  Gnome::Gda::Time time = {10, 20, 30, 0, 0};
+  if(!test_value(Glom::Field::TYPE_TIME, Gnome::Gda::Value(time)))
+    return EXIT_FAILURE;
+  */
+
+  if(!test_numeric_value((double)3.50f))
+    return EXIT_FAILURE;
+
+
+  //TODO: Image.
+
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}
diff --git a/tests/test_xml_utils.cc b/tests/test_xml_utils.cc
new file mode 100644
index 0000000..c90f64b
--- /dev/null
+++ b/tests/test_xml_utils.cc
@@ -0,0 +1,79 @@
+/* Glom
+ *
+ * Copyright (C) 2013 Openismus GmbH
+ *
+ * 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 2 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, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ */
+
+#include <glom/libglom/init.h>
+#include <glom/libglom/xml_utils.h>
+#include <glom/libglom/data_structure/glomconversions.h>
+#include <iostream>
+#include <cstdlib>
+
+static
+xmlpp::Element* create_element()
+{
+  xmlNodePtr cnode = xmlNewNode(0, (const xmlChar*)"somenodename");
+  return new xmlpp::Element(cnode);
+}
+
+static bool test_value(Glom::Field::glom_field_type field_type, const Gnome::Gda::Value& value)
+{
+  xmlpp::Element* element = create_element();
+  Glom::XmlUtils::set_node_text_child_as_value(element, value, field_type);
+
+  const Gnome::Gda::Value value_retrieved =
+   Glom::XmlUtils::get_node_text_child_as_value(element, field_type);
+
+  delete element;
+
+  if(value != value_retrieved)
+  {
+    std::cerr << G_STRFUNC << ": Got value=" << value_retrieved.to_string() << ", instead of value=" << 
value.to_string() << std::endl;
+    std::cerr << "  value_retrieved type=" << g_type_name(value_retrieved.get_value_type()) << ", value 
type=" << g_type_name(value.get_value_type()) << std::endl;
+    return false;
+  }
+
+  return true;
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  const Glib::ustring str = " Some value or other with a quote \" and leading space."; //Just to be awkward.
+  if(!test_value(Glom::Field::TYPE_TEXT, Gnome::Gda::Value(str)))
+    return EXIT_FAILURE;
+
+  const Glib::Date date(11, Glib::Date::MAY, 1973);
+  if(!test_value(Glom::Field::TYPE_DATE, Gnome::Gda::Value(date)))
+    return EXIT_FAILURE;
+
+  Gnome::Gda::Time time = {10, 20, 30, 0, 0};
+  if(!test_value(Glom::Field::TYPE_TIME, Gnome::Gda::Value(time)))
+    return EXIT_FAILURE;
+
+  if(!test_value(Glom::Field::TYPE_NUMERIC, Glom::Conversions::parse_value((double)3.50f)))
+    return EXIT_FAILURE;
+
+
+  //TODO: Image.
+
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]