[glom/origin/import_csv_refactored: 1/2] Import tests: made them quiet
- From: Michael Hasselmann <mikhas src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [glom/origin/import_csv_refactored: 1/2] Import tests: made them quiet
- Date: Wed, 23 Sep 2009 08:52:01 +0000 (UTC)
commit 24421bf2833967e8b532bf7cae17c1b76690911b
Author: Michael Hasselmann <michaelh openismus com>
Date: Wed Sep 23 10:34:44 2009 +0200
Import tests: made them quiet
* Makefile_tests.am, test/import/utils.[cc|h],
tests/import/test_[parsing|signals].cc: Only report on failure. Moved common
parts into an utility namespace.
ChangeLog | 8 ++++
Makefile_tests.am | 4 ++
tests/import/test_parsing.cc | 77 +++++++++++++++++-------------------------
tests/import/test_signals.cc | 71 +++++++++++++++++---------------------
tests/import/utils.cc | 21 +++++++++++
tests/import/utils.h | 16 +++++++++
6 files changed, 112 insertions(+), 85 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 44394d1..334d4e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-09-18 Michael Hasselmann <michaelh openismus com>
+
+ Import tests: made them quiet
+
+ * Makefile_tests.am, test/import/utils.[cc|h],
+ tests/import/test_[parsing|signals].cc: Only report on failure. Moved common
+ parts into an utility namespace.
+
2009-09-18 Murray Cumming <murrayc murrayc com>
CsvParser: Make this an actual parser, with a sane API.
diff --git a/Makefile_tests.am b/Makefile_tests.am
index 044443d..f6441b8 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -50,10 +50,14 @@ tests_test_load_python_library_SOURCES = tests/test_load_python_library.cc
tests_import_test_parsing_SOURCES = \
glom/import/csv_parser.cc \
glom/import/csv_parser.h \
+ tests/import/utils.cc\
+ tests/import/utils.h\
tests/import/test_parsing.cc
tests_import_test_signals_SOURCES = \
glom/import/csv_parser.cc \
glom/import/csv_parser.h \
+ tests/import/utils.cc\
+ tests/import/utils.h\
tests/import/test_signals.cc
glom_libglom_test_connectionpool_LDADD = $(tests_ldadd)
diff --git a/tests/import/test_parsing.cc b/tests/import/test_parsing.cc
index 6874023..27a2a2f 100644
--- a/tests/import/test_parsing.cc
+++ b/tests/import/test_parsing.cc
@@ -1,5 +1,7 @@
#include <glom/import_csv/csv_parser.h>
+#include <tests/import/utils.h>
//#include <glibmm/regex.h>
+#include <glibmm/regex.h>
#include <iostream>
#include <cstdlib>
@@ -41,12 +43,6 @@ bool check_tokens(Glib::RefPtr<Glib::Regex> check)
return true;
}
-void set_parser_contents(Glom::CsvParser& parser, const char* input, guint size)
-{
- // Do not read terminating null byte.
- parser.m_raw = std::vector<char>(input, input + size -1);
-}
-
void on_line_scanned(const Glib::ustring& line, guint /*line_number*/)
{
Glib::ustring field;
@@ -74,26 +70,20 @@ int main()
Glom::CsvParser parser("UTF-8");
parser.signal_line_scanned().connect(sigc::ptr_fun(&on_line_scanned));
- bool test_dquoted_string = false;
- bool test_skip_on_no_ending_newline = false;
- bool test_skip_on_no_quotes_around_token = false;
- bool test_skip_spaces_around_separators = false;
- bool test_fail_on_non_comma_separators = false;
- bool test_parse_newline_inside_quotes = false;
-
- std::stringstream results;
+ bool result = true;
+ std::stringstream report;
// test_dquoted_string
{
const char raw_line[] = "\"a \"\"quoted\"\" token\",\"sans quotes\"\n";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_dquoted_string: "
- << (test_dquoted_string = check_tokens(Glib::Regex::create("^(a \"quoted\" token|,|sans quotes)$")))
- << std::endl;
+ bool passed = check_tokens(Glib::Regex::create("^(a \"quoted\" token|,|sans quotes)$"));
+ if(!ImportTests::check("test_dquoted_string", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
@@ -102,14 +92,14 @@ int main()
// test_skip_on_no_ending_newline
{
const char raw_line[] = "\"this\",\"line\",\"will\",\"be\",\"skipped\"";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_skip_on_no_ending_newline: "
- << (test_skip_on_no_ending_newline = (get_tokens_instance().size() == 0))
- << std::endl;
+ bool passed = (get_tokens_instance().size() == 0);
+ if(!ImportTests::check("test_skip_on_no_ending_newline", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
@@ -118,14 +108,14 @@ int main()
// test_skip_on_no_quotes_around_token
{
const char raw_line[] = "this,line,contains,only,empty,tokens\n";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_skip_on_no_quotes_around_token: "
- << (test_skip_on_no_quotes_around_token = check_tokens(Glib::Regex::create("^(|,)$")))
- << std::endl;
+ bool passed = check_tokens(Glib::Regex::create("^(|,)$"));
+ if(!ImportTests::check("test_skip_on_no_quotes_around_token", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
@@ -134,14 +124,14 @@ int main()
// test_skip_spaces_around_separators
{
const char raw_line[] = "\"spaces\" , \"around\", \"separators\"\n";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_skip_spaces_around_separators: "
- << (test_skip_spaces_around_separators = (get_tokens_instance().size() == 5))
- << std::endl;
+ bool passed = (get_tokens_instance().size() == 5);
+ if(!ImportTests::check("test_skip_spaces_around_separators", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
@@ -150,14 +140,14 @@ int main()
// test_fail_on_non_comma_separators
{
const char raw_line[] = "\"cannot\"\t\"tokenize\"\t\"this\"\n";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_fail_on_non_comma_separators: "
- << (test_fail_on_non_comma_separators = check_tokens(Glib::Regex::create("^cannottokenizethis$")))
- << std::endl;
+ bool passed = check_tokens(Glib::Regex::create("^cannottokenizethis$"));
+ if(!ImportTests::check("test_fail_on_non_comma_separators", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
@@ -166,26 +156,21 @@ int main()
// test_parse_newline_inside_quotes
{
const char raw_line[] = "\"cell with\nnewline\"\n\"token on next line\"";
- set_parser_contents(parser, raw_line, sizeof(raw_line));
+ ImportTests::set_parser_contents(parser, raw_line, sizeof(raw_line));
while(parser.on_idle_parse())
{}
- results << "test_parse_newline_inside_quotes: "
- << (test_parse_newline_inside_quotes = check_tokens(Glib::Regex::create("^(cell with\nnewline|token on next line)$")))
- << std::endl;
+ bool passed = check_tokens(Glib::Regex::create("^(cell with\nnewline|token on next line)$"));
+ if(!ImportTests::check("test_parse_newline_inside_quotes", passed, report))
+ result = false;
get_tokens_instance().clear();
parser.clear();
}
- std::cout << results.rdbuf() << std::endl;
+ if(!result)
+ std::cout << report.rdbuf() << std::endl;
- return (test_dquoted_string &&
- test_skip_on_no_ending_newline &&
- test_skip_on_no_quotes_around_token &&
- test_skip_spaces_around_separators &&
- test_fail_on_non_comma_separators &&
- test_parse_newline_inside_quotes) ? EXIT_SUCCESS
- : EXIT_FAILURE;
+ return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
diff --git a/tests/import/test_signals.cc b/tests/import/test_signals.cc
index f4d4677..7126d34 100644
--- a/tests/import/test_signals.cc
+++ b/tests/import/test_signals.cc
@@ -1,4 +1,5 @@
#include <glom/import_csv/csv_parser.h>
+#include <tests/import/utils.h>
//#include <glibmm/regex.h>
#include <iostream>
#include <stdexcept>
@@ -8,13 +9,6 @@ namespace {
typedef std::vector<std::string> Encodings;
-/// This takes a @a size argument so we can test parsing of null bytes.
-void set_parser_contents(Glom::CsvParser& parser, const char* input, guint size)
-{
- // Do not read terminating null byte.
- parser.m_raw = std::vector<char>(input, input + size -1);
-}
-
guint& get_line_scanned_count_instance()
{
static guint line_scanned_count = 0;
@@ -58,25 +52,23 @@ int main()
parser.signal_line_scanned().connect(sigc::hide(sigc::hide(&on_line_scanned)));
parser.signal_encoding_error().connect(sigc::ptr_fun(&on_encoding_error));
- bool test_ignore_quoted_newlines = false;
- bool test_ignore_empty_lines = false;
- bool test_wrong_encoding = false;
- bool test_incomplete_chars = false;
-
- std::stringstream results;
+ bool result = true;
+ std::stringstream report;
// 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";
- set_parser_contents(parser, raw, sizeof(raw));
+ ImportTests::set_parser_contents(parser, raw, sizeof(raw));
while(parser.on_idle_parse())
{}
- results << "test_ignore_quoted_newlines: "
- << (test_ignore_quoted_newlines = (2 == get_line_scanned_count_instance()))
- << std::endl;
+ bool passed = (2 == get_line_scanned_count_instance() &&
+ 0 == get_encoding_error_count_instance());
+
+ if(!ImportTests::check("test_ignore_quoted_newlines", passed, report))
+ result = false;
reset_signal_counts();
parser.clear();
@@ -86,15 +78,16 @@ int main()
{
// 5 CSV lines, but only 2 contain data
const char raw[] = "token1\n\n\n\ntoken2, token3\n";
- set_parser_contents(parser, raw, sizeof(raw));
+ ImportTests::set_parser_contents(parser, raw, sizeof(raw));
while(parser.on_idle_parse())
{}
- results << "test_ignore_empty_lines: "
- << (test_ignore_empty_lines = (2 == get_line_scanned_count_instance() &&
- 0 == get_encoding_error_count_instance()))
- << std::endl;
+ bool passed = (2 == get_line_scanned_count_instance() &&
+ 0 == get_encoding_error_count_instance());
+
+ if(!ImportTests::check("test_ignore_empty_lines", passed, report))
+ result = false;
reset_signal_counts();
parser.clear();
@@ -107,7 +100,7 @@ int main()
// An invalid Unicode sequence.
const char raw[] = "\0xc0\0x00\n";
- set_parser_contents(parser, raw, sizeof(raw));
+ ImportTests::set_parser_contents(parser, raw, sizeof(raw));
for (Encodings::const_iterator iter = encodings.begin();
iter != encodings.end();
@@ -128,10 +121,12 @@ int main()
parser.set_encoding((*iter).c_str());
}
- results << "test_wrong_encoding: "
- << (test_wrong_encoding = (2 == get_encoding_error_count_instance() &&
- 0 == get_line_scanned_count_instance()))
- << std::endl;
+
+ bool passed = (2 == get_encoding_error_count_instance() &&
+ 0 == get_line_scanned_count_instance());
+
+ if(!ImportTests::check("test_wrong_encoding", passed, report))
+ result = false;
reset_signal_counts();
parser.clear();
@@ -141,26 +136,24 @@ int main()
{
// An incomplete Unicode sequence.
const char raw[] = "\0xc0\n";
- set_parser_contents(parser, raw, sizeof(raw));
+ ImportTests::set_parser_contents(parser, raw, sizeof(raw));
while(parser.on_idle_parse())
{}
- parser.clear();
+ bool passed = (1 == get_encoding_error_count_instance() &&
+ 0 == get_line_scanned_count_instance());
- results << "test_incomplete_chars: "
- << (test_incomplete_chars = (1 == get_encoding_error_count_instance() &&
- 0 == get_line_scanned_count_instance()))
- << std::endl;
+ if(!ImportTests::check("test_incomplete_chars", passed, report))
+ result = false;
reset_signal_counts();
parser.clear();
}
- std::cout << results.rdbuf() << std::endl;
- return (test_ignore_quoted_newlines &&
- test_ignore_empty_lines &&
- test_wrong_encoding &&
- test_incomplete_chars
- ) ? EXIT_SUCCESS : EXIT_FAILURE;
+ if(!result)
+ std::cout << report.rdbuf() << std::endl;
+
+ return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
+
diff --git a/tests/import/utils.cc b/tests/import/utils.cc
new file mode 100644
index 0000000..0a9f868
--- /dev/null
+++ b/tests/import/utils.cc
@@ -0,0 +1,21 @@
+#include <tests/import/utils.h>
+
+namespace ImportTests
+{
+
+bool check(const std::string& name, bool test, std::stringstream& report)
+{
+ if(!test)
+ report << name << ": FAILED" << std::endl;
+
+ return test;
+}
+
+void set_parser_contents(Glom::CsvParser& parser, const char* input, guint size)
+{
+ // Do not read terminating null byte.
+ parser.m_raw = std::vector<char>(input, input + size -1);
+}
+
+} //namespace ImportTests
+
diff --git a/tests/import/utils.h b/tests/import/utils.h
new file mode 100644
index 0000000..4088b5c
--- /dev/null
+++ b/tests/import/utils.h
@@ -0,0 +1,16 @@
+#ifndef TEST_IMPORT_UTILS_H
+#define TEST_IMPORT_UTILS_H
+
+#include <glom/import_csv.h>
+#include <iostream>
+
+namespace ImportTests
+{
+
+bool check(const std::string& name, bool test, std::stringstream& report);
+
+/// This takes a @a size argument so we can test parsing of null bytes.
+void set_parser_contents(Glom::CsvParser& parser, const char* input, guint size);
+
+} //namespace ImportTests
+#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]