summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-03 00:07:58 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-03 00:07:58 +0200
commit0cbc7dd8dd00c92570f35f901b820a1ea96acdf0 (patch)
tree7591b3cae8191a4bf4ec4092edb252c874792445 /test
parent5bc49de682eec79f3dc8096c6812bee70fe2d496 (diff)
Add space separated argument parser with quoting
Diffstat (limited to 'test')
-rw-r--r--test/.gitignore1
-rw-r--r--test/Makefile.am6
-rw-r--r--test/test-args.cc104
3 files changed, 110 insertions, 1 deletions
diff --git a/test/.gitignore b/test/.gitignore
index 8ab7990..d859557 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -3,3 +3,4 @@
/test-header-parser
/test-multipart-formdata-parser
/test-query-parser
+/test-args
diff --git a/test/Makefile.am b/test/Makefile.am
index 3017b6f..5711aa4 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -2,7 +2,8 @@ MAINTAINERCLEANFILES = Makefile.in
AM_CPPFLAGS = @DEFINES@ -I$(top_builddir)/src
-TESTS = test-query-parser test-header-parser test-multipart-formdata-parser
+TESTS = test-query-parser test-header-parser test-multipart-formdata-parser \
+ test-args
check_PROGRAMS = $(TESTS)
@@ -14,3 +15,6 @@ test_header_parser_LDADD = $(top_srcdir)/src/libcgi.la
test_multipart_formdata_parser_SOURCES = test-multipart-formdata-parser.cc
test_multipart_formdata_parser_LDADD = $(top_srcdir)/src/libcgi.la
+
+test_args_SOURCES = test-args.cc
+test_args_LDADD = $(top_srcdir)/src/libcgi.la
diff --git a/test/test-args.cc b/test/test-args.cc
new file mode 100644
index 0000000..a9d4328
--- /dev/null
+++ b/test/test-args.cc
@@ -0,0 +1,104 @@
+#include "common.hh"
+
+#include <cstdarg>
+#include <iostream>
+
+#include "args.hh"
+
+using namespace stuff;
+
+namespace {
+
+bool compare(const std::string& in, const std::vector<std::string>& out,
+ va_list args) {
+ for (const auto& arg : out) {
+ const char* str = va_arg(args, const char*);
+ if (!str) {
+ std::cerr << "expected less arguments: " << in
+ << ", first extra: " << arg << std::endl;
+ return false;
+ }
+ if (arg.compare(str) != 0) {
+ std::cerr << "expected " << str << " in " << in
+ << ", got: " << arg << std::endl;
+ return false;
+ }
+ }
+ const char* str = va_arg(args, const char*);
+ if (str) {
+ std::cerr << "expected more arguments: " << in
+ << ", missing: " << str << std::endl;
+ return false;
+ }
+ return true;
+}
+
+bool test(const char* in, ...) {
+ std::vector<std::string> out;
+ if (!Args::parse(in, &out, false)) {
+ std::cerr << "expected success: " << in << std::endl;
+ return false;
+ }
+ va_list args;
+ va_start(args, in);
+ bool ret = compare(in, out, args);
+ va_end(args);
+ return ret;
+}
+
+bool test_nice(const char* in, ...) {
+ std::vector<std::string> out;
+ if (!Args::parse(in, &out, true)) {
+ std::cerr << "expected success: " << in << std::endl;
+ return false;
+ }
+ va_list args;
+ va_start(args, in);
+ bool ret = compare(in, out, args);
+ va_end(args);
+ return ret;
+}
+
+bool test_fail(const std::string& in) {
+ std::vector<std::string> out;
+ if (Args::parse(in, &out, false)) {
+ std::cerr << "expected fail: " << in << std::endl;
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
+int main() {
+ unsigned int ok = 0, tot = 0;
+
+ tot++; if (test("", NULL)) ok++;
+ tot++; if (test("foo", "foo", NULL)) ok++;
+ tot++; if (test("hello world", "hello", "world", NULL)) ok++;
+ tot++; if (test("\"hello world\"", "hello world", NULL)) ok++;
+ tot++; if (test("'hello world'", "hello world", NULL)) ok++;
+ tot++; if (test("a' b'", "a b", NULL)) ok++;
+ tot++; if (test("a'b'ba", "abba", NULL)) ok++;
+ tot++; if (test("a'b'b '' a", "abb", "", "a", NULL)) ok++;
+ tot++; if (test("'\"'", "\"", NULL)) ok++;
+ tot++; if (test("'\\\"'", "\"", NULL)) ok++;
+ tot++; if (test("'\\\\'", "\\", NULL)) ok++;
+ tot++; if (test("'\\''", "'", NULL)) ok++;
+ tot++; if (test("'a\\'b'", "a'b", NULL)) ok++;
+ tot++; if (test("a'\\''b", "a'b", NULL)) ok++;
+
+ tot++; if (test_nice("a'b", "a'b", NULL)) ok++;
+ tot++; if (test_nice("a'b foo", "a'b", "foo", NULL)) ok++;
+ tot++; if (test_nice("'\\", "'\\", NULL)) ok++;
+ tot++; if (test_nice("'\\'", "'\\'", NULL)) ok++;
+
+ tot++; if (test_fail("'")) ok++;
+ tot++; if (test_fail("'''")) ok++;
+ tot++; if (test_fail("\"")) ok++;
+ tot++; if (test_fail("'\\")) ok++;
+ tot++; if (test_fail("'\\'")) ok++;
+
+ std::cout << "OK " << ok << "/" << tot << std::endl;
+ return ok == tot ? EXIT_SUCCESS : EXIT_FAILURE;
+}