[glom] Import: More code cleanup in tests



commit 812f4bd70f0a5c7a9d72fccec0eb20bef571b576
Author: Michael Hasselmann <michaelh openismus com>
Date:   Fri Sep 25 18:53:59 2009 +0200

    Import: More code cleanup in tests
    
    * glom/import_csv/csv_parser.cc (set_file_and_start_parsing): Fail on empty
    file uri.
    
    * tests/import/*: Removed the ugly mainloop singleton, among other cleanups.

 ChangeLog                     |    9 +++++
 glom/import_csv/csv_parser.cc |    5 ++-
 tests/import/test_parsing.cc  |   29 ++++++++---------
 tests/import/test_signals.cc  |    1 -
 tests/import/utils.cc         |   68 ++++++++++++++++------------------------
 tests/import/utils.h          |    4 +--
 6 files changed, 55 insertions(+), 61 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3f075b4..7d4d0b8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-09-25  Michael Hasselmann  <michaelh openismus com>
+
+	Import: More code cleanup in tests
+	
+	* glom/import_csv/csv_parser.cc (set_file_and_start_parsing): Fail on empty
+	file uri.
+	
+	* tests/import/*: Removed the ugly mainloop singleton, among other cleanups.
+
 2009-09-25  Murray Cumming  <murrayc murrayc-desktop>
 
 	Cleanup.
diff --git a/glom/import_csv/csv_parser.cc b/glom/import_csv/csv_parser.cc
index 5e4943a..d901d89 100644
--- a/glom/import_csv/csv_parser.cc
+++ b/glom/import_csv/csv_parser.cc
@@ -65,6 +65,9 @@ CsvParser::CsvParser(const std::string& encoding_charset)
 
 void CsvParser::set_file_and_start_parsing(const std::string& file_uri)
 {
+  // TODO: Also fail on file:///?
+  g_return_if_fail(!file_uri.empty());
+
   m_file = Gio::File::create_for_uri(file_uri);
   m_file->read_async(sigc::mem_fun(*this, &CsvParser::on_file_read));
   set_state(CsvParser::STATE_PARSING);
@@ -73,7 +76,7 @@ void CsvParser::set_file_and_start_parsing(const std::string& file_uri)
   m_file->query_info_async(sigc::mem_fun(*this, &CsvParser::on_file_query_info), G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
 }
 
- 
+
 CsvParser::~CsvParser()
 {
   m_idle_connection.disconnect();
diff --git a/tests/import/test_parsing.cc b/tests/import/test_parsing.cc
index 1ee0ddb..adf40c1 100644
--- a/tests/import/test_parsing.cc
+++ b/tests/import/test_parsing.cc
@@ -102,11 +102,10 @@ int main(int argc, char* argv[])
   {
     const char* raw = "\"a \"\"quoted\"\" token\",\"sans quotes\"\n";
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
-    g_assert(finished_parsing);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("^(a \"quoted\" token|sans quotes)$") &&
-                   2 == get_tokens_instance().size());
+                         check_tokens("^(a \"quoted\" token|sans quotes)$") &&
+                         2 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_dquoted_string", passed, report))
@@ -119,8 +118,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("token in first line") &&
-                   1 == get_tokens_instance().size());
+                         check_tokens("token in first line") &&
+                         1 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_skip_on_no_ending_newline", passed, report))
@@ -133,8 +132,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("^$") &&
-                   6 == get_tokens_instance().size());
+                         check_tokens("^$") &&
+                         6 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_skip_on_no_quotes_around_token", passed, report))
@@ -147,8 +146,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("^(spaces|around|separators)$") &&
-                   3 == get_tokens_instance().size());
+                         check_tokens("^(spaces|around|separators)$") &&
+                         3 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_skip_spaces_around_separators", passed, report))
@@ -162,8 +161,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("^cannottokenizethis$") &&
-                   1 == get_tokens_instance().size());
+                         check_tokens("^cannottokenizethis$") &&
+                         1 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_fail_on_non_comma_separators", passed, report))
@@ -176,8 +175,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("^(cell with\nnewline|token on next line)$") &&
-                   2 == get_tokens_instance().size());
+                         check_tokens("^(cell with\nnewline|token on next line)$") &&
+                         2 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_parse_newline_inside_quotes", passed, report))
@@ -190,8 +189,8 @@ int main(int argc, char* argv[])
     const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
 
     const bool passed = (finished_parsing &&
-                   check_tokens("token") &&
-                   1 == get_tokens_instance().size());
+                         check_tokens("token") &&
+                         1 == get_tokens_instance().size());
     get_tokens_instance().clear();
 
     if(!ImportTests::check("test_fail_on_non_matching_quotes", passed, report))
diff --git a/tests/import/test_signals.cc b/tests/import/test_signals.cc
index e0ebf3d..a70b826 100644
--- a/tests/import/test_signals.cc
+++ b/tests/import/test_signals.cc
@@ -1,6 +1,5 @@
 #include <glom/import_csv/csv_parser.h>
 #include <tests/import/utils.h>
-//#include <glibmm/regex.h>
 #include <gtkmm.h>
 #include <iostream>
 #include <stdexcept>
diff --git a/tests/import/utils.cc b/tests/import/utils.cc
index c02fb62..a343983 100644
--- a/tests/import/utils.cc
+++ b/tests/import/utils.cc
@@ -3,6 +3,9 @@
 namespace ImportTests
 {
 
+//The result just shows whether it finished before the timeout.
+static bool finished_parsing = true;
+
 bool check(const std::string& name, bool test, std::stringstream& report)
 {
   if(!test)
@@ -14,54 +17,40 @@ bool check(const std::string& name, bool test, std::stringstream& report)
 // Returns the file name of the temporary created file, which will contain the buffer's contents.
 static std::string create_file_from_buffer(const char* input, guint input_size)
 {
-  //std::cout << "debug: input: " << input << std::endl;
-
   // Use Glib's file utilities to get a unique temporary filename:
   std::string tmp_filename;
   const int tmp_file_handle = Glib::file_open_tmp(tmp_filename, "glom_testdata");
-
-  const std::string uri = Glib::filename_to_uri(tmp_filename);
-  if(0 < tmp_file_handle)
-  {
-    ssize_t result = write(tmp_file_handle, input, input_size);
-    g_assert(-1 != result); // g_return_with_val would still be wrong here, I think?
-
+  if(-1 < tmp_file_handle)
     close(tmp_file_handle);
-  }
-
-  return uri;
-}
 
-static Glib::RefPtr<Glib::MainLoop>& get_mainloop_instance()
-{
-  static Glib::RefPtr<Glib::MainLoop> mainloop(0);
-  return mainloop;
-}
+  const std::string file_uri = Glib::filename_to_uri(tmp_filename);
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(file_uri);
+  gssize result = file->append_to()->write(input, input_size);
+  g_return_val_if_fail(-1 < result, "");
 
-static bool& get_result_instance()
-{
-  static bool result = true;
-  return result;
+  return file_uri;
 }
 
-static void on_mainloop_killed_by_watchdog()
+static void on_mainloop_killed_by_watchdog(MainLoopRp mainloop)
 {
-  get_result_instance() = false;
-  get_mainloop_instance()->quit();
+  finished_parsing = false;
+  //Quit the mainloop that we ran because the parser uses an idle handler.
+  mainloop->quit();
 }
 
-static void on_parser_encoding_error()
+static void on_parser_encoding_error(MainLoopRp mainloop)
 {
-  get_result_instance() = true; //The result just shows whether it finished before the timeout.
+  finished_parsing = true;
   //Quit the mainloop that we ran because the parser uses an idle handler.
-  get_mainloop_instance()->quit();
+  mainloop->quit();
 }
 
 
-static void on_parser_finished()
+static void on_parser_finished(MainLoopRp mainloop)
 {
+  finished_parsing = true;
   //Quit the mainloop that we ran because the parser uses an idle handler.
-  get_mainloop_instance()->quit();
+  mainloop->quit();
 }
 
 bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const std::string& input)
@@ -71,36 +60,33 @@ bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signa
 
 bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const char* input, guint input_size)
 {
-  get_result_instance() = true;
+  finished_parsing = true;
 
   //Start a mainloop because the parser uses an idle handler.
   //TODO: Stop the parser from doing that.
-  get_mainloop_instance().reset();
-  get_mainloop_instance() = Glib::MainLoop::create();
-
+  MainLoopRp mainloop = Glib::MainLoop::create();
   Glom::CsvParser parser("UTF-8");
 
-  parser.signal_encoding_error().connect(&on_parser_encoding_error);
-  parser.signal_finished_parsing().connect(&on_parser_finished);
+  parser.signal_encoding_error().connect(sigc::bind(&on_parser_encoding_error, mainloop));
+  parser.signal_finished_parsing().connect(sigc::bind(&on_parser_finished, mainloop));
 
-  // Install a watchdog for the mainloop. No test should need longer than 3
+  // Install a watchdog for the mainloop. No test should need longer than 300
   // seconds. Also, we need to avoid being stuck in the mainloop.
   // Infinitely running tests are useless.
-  get_mainloop_instance()->get_context()->signal_timeout().connect_seconds_once(
-    sigc::ptr_fun(&on_mainloop_killed_by_watchdog), 300);
+  mainloop->get_context()->signal_timeout().connect_seconds_once(sigc::bind(&on_mainloop_killed_by_watchdog, mainloop), 300);
 
   connect_parser_signals(parser);
 
   const std::string file_uri = create_file_from_buffer(input, input_size);
   parser.set_file_and_start_parsing(file_uri);
 
-  get_mainloop_instance()->run();
+  mainloop->run();
 
   Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(file_uri);
   const bool removed = file->remove();
   g_assert(removed);
 
-  return get_result_instance();
+  return finished_parsing;
 }
 
 } //namespace ImportTests
diff --git a/tests/import/utils.h b/tests/import/utils.h
index 3e9f759..4a9d67f 100644
--- a/tests/import/utils.h
+++ b/tests/import/utils.h
@@ -2,10 +2,7 @@
 #define TEST_IMPORT_UTILS_H
 
 #include <glom/import_csv/csv_parser.h>
-//#include <sigc++/sigc++.h>
 #include <iostream>
-#include <unistd.h>
-#include <errno.h>
 
 namespace ImportTests
 {
@@ -13,6 +10,7 @@ namespace ImportTests
 bool check(const std::string& name, bool test, std::stringstream& report);
 
 typedef sigc::slot<void, Glom::CsvParser&> FuncConnectParserSignals;
+typedef Glib::RefPtr<Glib::MainLoop> MainLoopRp;
 
 /**
  * @result Whether the parser finished without being killed by a timeout.



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