[glom/import_csv_refactored] Import tests: Fixed.
- From: Murray Cumming <murrayc src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [glom/import_csv_refactored] Import tests: Fixed.
- Date: Fri, 25 Sep 2009 14:44:03 +0000 (UTC)
commit 60fa53e27e5abfd90f5adf2b90b2515927c3b051
Author: Murray Cumming <murrayc murrayc com>
Date: Fri Sep 25 16:43:55 2009 +0200
Import tests: Fixed.
* tests/import/utils.[h|cc]: run_parser_from_buffer(): Added a
version that takes a std::string, to make the common case more robust.
* tests/import/test_parsing.cc:
* tests/import/test_signals.cc: Fix the tests by not incorrectly
taking the length of the raw arrays, which caused the temp files to
contain garbage.
ChangeLog | 11 +++++++++++
tests/import/test_parsing.cc | 28 ++++++++++++++--------------
tests/import/test_signals.cc | 14 +++++++-------
tests/import/utils.cc | 24 +++++++++++++++---------
tests/import/utils.h | 11 ++++++++++-
5 files changed, 57 insertions(+), 31 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 99f2148..82bd453 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
2009-09-25 Murray Cumming <murrayc murrayc-desktop>
+ Import tests: Fixed.
+
+ * tests/import/utils.[h|cc]: run_parser_from_buffer(): Added a
+ version that takes a std::string, to make the common case more robust.
+ * tests/import/test_parsing.cc:
+ * tests/import/test_signals.cc: Fix the tests by not incorrectly
+ taking the length of the raw arrays, which caused the temp files to
+ contain garbage.
+
+2009-09-25 Murray Cumming <murrayc murrayc-desktop>
+
Import tests: Handle encoding errors.
* tests/import/utils.cc: Handle signal_encoding_error too, because this
diff --git a/tests/import/test_parsing.cc b/tests/import/test_parsing.cc
index 0a71670..1581d25 100644
--- a/tests/import/test_parsing.cc
+++ b/tests/import/test_parsing.cc
@@ -100,8 +100,8 @@ int main(int argc, char* argv[])
// test_dquoted_string
{
- const char raw[] = "\"a \"\"quoted\"\" token\",\"sans quotes\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ 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 &&
@@ -115,8 +115,8 @@ int main(int argc, char* argv[])
// test_skip_on_no_ending_newline
{
- const char raw[] = "\"token in first line\"\n\"2nd token\", \"but\", \"this\",\"line\",\"will\",\"be\",\"skipped\"";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"token in first line\"\n\"2nd token\", \"but\", \"this\",\"line\",\"will\",\"be\",\"skipped\"";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
check_tokens("token in first line") &&
@@ -129,8 +129,8 @@ int main(int argc, char* argv[])
// test_skip_on_no_quotes_around_token
{
- const char raw[] = "this,line,contains,only,empty,tokens\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "this,line,contains,only,empty,tokens\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
check_tokens("^$") &&
@@ -143,8 +143,8 @@ int main(int argc, char* argv[])
// test_skip_spaces_around_separators
{
- const char raw[] = "\"spaces\" , \"around\", \"separators\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"spaces\" , \"around\", \"separators\"\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
check_tokens("^(spaces|around|separators)$") &&
@@ -158,8 +158,8 @@ int main(int argc, char* argv[])
// test_fail_on_non_comma_separators
{
- const char raw[] = "\"cannot\"\t\"tokenize\"\t\"this\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"cannot\"\t\"tokenize\"\t\"this\"\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
check_tokens("^cannottokenizethis$") &&
@@ -172,8 +172,8 @@ int main(int argc, char* argv[])
// test_parse_newline_inside_quotes
{
- const char raw[] = "\"cell with\nnewline\"\n\"token on next line\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"cell with\nnewline\"\n\"token on next line\"\n";
+ 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)$") &&
@@ -186,8 +186,8 @@ int main(int argc, char* argv[])
// test_fail_on_non_matching_quotes
{
- const char raw[] = "\"token\"\nthis quote has no partner\",\"token\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"token\"\nthis quote has no partner\",\"token\"\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
check_tokens("token") &&
diff --git a/tests/import/test_signals.cc b/tests/import/test_signals.cc
index d382776..10f763e 100644
--- a/tests/import/test_signals.cc
+++ b/tests/import/test_signals.cc
@@ -64,8 +64,8 @@ int main(int argc, char* argv[])
// test_ignore_quoted_newlines
{
// 2 CSV lines, first one contains newlines inside quotes
- const char raw[] = "\"some\n quoted\r\n newlines\n\", \"token2\"\n\"token3\"\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "\"some\n quoted\r\n newlines\n\", \"token2\"\n\"token3\"\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
2 == get_line_scanned_count_instance() &&
@@ -80,8 +80,8 @@ int main(int argc, char* argv[])
// test_ignore_empty_lines
{
// 5 CSV lines, but only 2 contain data
- const char raw[] = "token1\n\n\n\ntoken2, token3\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const char* raw = "token1\n\n\n\ntoken2, token3\n";
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw);
const bool passed = (finished_parsing &&
2 == get_line_scanned_count_instance() &&
@@ -100,8 +100,8 @@ int main(int argc, char* argv[])
type_encodings encodings(encoding_arr, encoding_arr + G_N_ELEMENTS(encoding_arr));
// An invalid Unicode sequence.
- const char raw[] = "\0xc0\0x00\n";
- ImportTests::set_parser_contents(parser, raw, sizeof(raw));
+ const char* raw = "\0xc0\0x00\n";
+ ImportTests::set_parser_contents(parser, raw);
for (type_encodings::const_iterator iter = encodings.begin();
iter != encodings.end();
@@ -145,7 +145,7 @@ int main(int argc, char* argv[])
{
// An incomplete Unicode sequence.
const char raw[] = "\0xc0\n";
- const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, sizeof(raw));
+ const bool finished_parsing = ImportTests::run_parser_from_buffer(&connect_signals, raw, G_N_ELEMENTS(raw));
const bool passed = (finished_parsing &&
1 == get_encoding_error_count_instance() &&
diff --git a/tests/import/utils.cc b/tests/import/utils.cc
index 17718c3..5bad66e 100644
--- a/tests/import/utils.cc
+++ b/tests/import/utils.cc
@@ -12,21 +12,24 @@ 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 size)
+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, size);
+ 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?
close(tmp_file_handle);
}
- return Glib::filename_to_uri(tmp_filename);
+ return uri;
}
static Glib::RefPtr<Glib::MainLoop>& get_mainloop_instance()
@@ -43,15 +46,13 @@ static bool& get_result_instance()
static void on_mainloop_killed_by_watchdog()
{
- get_result_instance() = false; // Comment out if you want to run tests and get useful results even with a non-working finished_parsing signal.
+ get_result_instance() = false;
get_mainloop_instance()->quit();
}
static void on_parser_encoding_error()
{
- std::cerr << "debug: utils.cc: on_parser_encoding_error()" << std::endl;
-
- get_result_instance() = false;
+ get_result_instance() = true; //The result just shows whether it finished before the timeout.
//Quit the mainloop that we ran because the parser uses an idle handler.
get_mainloop_instance()->quit();
}
@@ -63,7 +64,12 @@ static void on_parser_finished()
get_mainloop_instance()->quit();
}
-bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const char* input, guint size)
+bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const std::string& input)
+{
+ return run_parser_from_buffer(connect_parser_signals, input.data(), input.size());
+}
+
+bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const char* input, guint input_size)
{
get_result_instance() = true;
@@ -85,7 +91,7 @@ bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signa
connect_parser_signals(parser);
- const std::string file_uri = create_file_from_buffer(input, size);
+ const std::string file_uri = create_file_from_buffer(input, input_size);
parser.set_file_and_start_parsing(file_uri);
get_mainloop_instance()->run();
diff --git a/tests/import/utils.h b/tests/import/utils.h
index 40bf321..c2e4063 100644
--- a/tests/import/utils.h
+++ b/tests/import/utils.h
@@ -13,7 +13,16 @@ namespace ImportTests
bool check(const std::string& name, bool test, std::stringstream& report);
typedef sigc::slot<void, Glom::CsvParser&> FuncConnectParserSignals;
-bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const char* input, guint size);
+
+/**
+ * @result Whether the parser finished without being killed by a timeout.
+ */
+bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const char* input, guint input_size);
+
+/**
+ * @result Whether the parser finished without being killed by a timeout.
+ */
+bool run_parser_from_buffer(const FuncConnectParserSignals& connect_parser_signals, const std::string& input);
} //namespace ImportTests
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]