summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-12-10 09:47:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-12-10 09:47:29 +0200
commitf8edfd22cb45b554a573d2722900196758e9e958 (patch)
treeb2c800c8793f08be287a67ed72517f9cc2831fda /tests
parenteddefea6ea39e64e9eb5adf74a279a230a63cf5b (diff)
Scanner-based parsing with support for element erasing
Also implement argv_file_scanner which provides support for reading command line arguments from the argv array as well as files specified with command line options. New examples: file. New tests: ctor, erase, file.
Diffstat (limited to 'tests')
-rw-r--r--tests/ctor/driver.cxx69
-rw-r--r--tests/ctor/makefile73
-rw-r--r--tests/ctor/test.cli9
-rw-r--r--tests/erase/driver.cxx35
-rw-r--r--tests/erase/makefile73
-rw-r--r--tests/erase/test.cli10
-rw-r--r--tests/file/base.ops2
-rw-r--r--tests/file/driver.cxx29
-rw-r--r--tests/file/empty.ops3
-rw-r--r--tests/file/makefile96
-rw-r--r--tests/file/test-000.ops6
-rw-r--r--tests/file/test-000.std18
-rw-r--r--tests/file/test-001.ops3
-rw-r--r--tests/file/test-001.std6
-rw-r--r--tests/file/test-002.ops7
-rw-r--r--tests/file/test-002.std17
-rw-r--r--tests/file/test-003.std7
-rw-r--r--tests/file/test.cli8
-rw-r--r--tests/makefile2
19 files changed, 472 insertions, 1 deletions
diff --git a/tests/ctor/driver.cxx b/tests/ctor/driver.cxx
new file mode 100644
index 0000000..c923ec1
--- /dev/null
+++ b/tests/ctor/driver.cxx
@@ -0,0 +1,69 @@
+// file : tests/ctor/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include "test.hxx"
+
+int
+main (int argc, char* argv[])
+{
+ // Test that we can call all the c-tors unambiguously.
+ //
+ {
+ options o1 (argc, argv);
+ options o2 (argc, argv,
+ cli::unknown_mode::fail);
+ options o3 (argc, argv,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ options o4 (argc, argv, true,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ }
+
+ {
+ options o1 (1, argc, argv);
+ options o2 (1, argc, argv,
+ cli::unknown_mode::fail);
+ options o3 (1, argc, argv,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ options o4 (1, argc, argv, true,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ }
+
+ {
+ int end;
+ options o1 (argc, argv, end);
+ options o2 (argc, argv, end,
+ cli::unknown_mode::fail);
+ options o3 (argc, argv, end,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ options o4 (argc, argv, end, true,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ }
+
+ {
+ int end;
+ options o1 (1, argc, argv, end);
+ options o2 (1, argc, argv, end,
+ cli::unknown_mode::fail);
+ options o3 (1, argc, argv, end,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ options o4 (1, argc, argv, end, true,
+ cli::unknown_mode::fail,
+ cli::unknown_mode::stop);
+ }
+
+ {
+ cli::argv_scanner s (argc, argv);
+ options o1 (s);
+ options o2 (s, cli::unknown_mode::fail);
+ options o3 (s, cli::unknown_mode::fail, cli::unknown_mode::stop);
+ }
+}
diff --git a/tests/ctor/makefile b/tests/ctor/makefile
new file mode 100644
index 0000000..93d832a
--- /dev/null
+++ b/tests/ctor/makefile
@@ -0,0 +1,73 @@
+# file : tests/ctor/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+cli_tun := test.cli
+
+#
+#
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(cxx_obj)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+
+genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(out_root)/cli/cli
+$(gen): cli := $(out_root)/cli/cli
+$(gen): cli_options :=
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+$(test): driver := $(driver)
+$(test): $(driver)
+ $(call message,test $$1,$$1,$(driver))
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean))
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(scf_root)/cli/cli-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/cli/makefile)
+
diff --git a/tests/ctor/test.cli b/tests/ctor/test.cli
new file mode 100644
index 0000000..f85ad98
--- /dev/null
+++ b/tests/ctor/test.cli
@@ -0,0 +1,9 @@
+// file : tests/ctor/test.cli
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+class options
+{
+ bool --help;
+};
diff --git a/tests/erase/driver.cxx b/tests/erase/driver.cxx
new file mode 100644
index 0000000..ddb6b8d
--- /dev/null
+++ b/tests/erase/driver.cxx
@@ -0,0 +1,35 @@
+// file : tests/erase/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+// Test argument erasing.
+//
+
+#include <string>
+#include <cassert>
+
+#include "test.hxx"
+
+using namespace std;
+
+int
+main (int argc, char* argv[])
+{
+ options o (argc, argv, true,
+ cli::unknown_mode::skip,
+ cli::unknown_mode::skip);
+
+ assert (o.a ());
+ assert (o.b () == 123);
+
+ // We should have 'foo bar --arg -- -b 234'.
+ //
+ assert (argc == 7);
+ assert (argv[1] == string ("foo"));
+ assert (argv[2] == string ("bar"));
+ assert (argv[3] == string ("--arg"));
+ assert (argv[4] == string ("--"));
+ assert (argv[5] == string ("-b"));
+ assert (argv[6] == string ("234"));
+}
diff --git a/tests/erase/makefile b/tests/erase/makefile
new file mode 100644
index 0000000..5a8f49a
--- /dev/null
+++ b/tests/erase/makefile
@@ -0,0 +1,73 @@
+# file : tests/erase/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+cli_tun := test.cli
+
+#
+#
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(cxx_obj)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+
+genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(out_root)/cli/cli
+$(gen): cli := $(out_root)/cli/cli
+$(gen): cli_options :=
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+$(test): driver := $(driver)
+$(test): $(driver)
+ $(call message,test $$1,$$1 foo -a bar -b 123 --arg -- -b 234,$(driver))
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean))
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(scf_root)/cli/cli-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/cli/makefile)
+
diff --git a/tests/erase/test.cli b/tests/erase/test.cli
new file mode 100644
index 0000000..57820c0
--- /dev/null
+++ b/tests/erase/test.cli
@@ -0,0 +1,10 @@
+// file : tests/erase/test.cli
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+class options
+{
+ bool -a;
+ int -b;
+};
diff --git a/tests/file/base.ops b/tests/file/base.ops
new file mode 100644
index 0000000..45d7696
--- /dev/null
+++ b/tests/file/base.ops
@@ -0,0 +1,2 @@
+-a 21
+-b 21
diff --git a/tests/file/driver.cxx b/tests/file/driver.cxx
new file mode 100644
index 0000000..e0aec6e
--- /dev/null
+++ b/tests/file/driver.cxx
@@ -0,0 +1,29 @@
+// file : tests/file/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+// Test argv_file_scanner.
+//
+
+#include <iostream>
+
+#include "test.hxx"
+
+using namespace std;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ cli::argv_file_scanner s (argc, argv, "--file");
+
+ while (s.more ())
+ cout << s.next () << endl;
+ }
+ catch (const cli::exception& e)
+ {
+ cout << e << endl;
+ }
+}
diff --git a/tests/file/empty.ops b/tests/file/empty.ops
new file mode 100644
index 0000000..ed080fb
--- /dev/null
+++ b/tests/file/empty.ops
@@ -0,0 +1,3 @@
+# Empty options file.
+#
+
diff --git a/tests/file/makefile b/tests/file/makefile
new file mode 100644
index 0000000..56e95c6
--- /dev/null
+++ b/tests/file/makefile
@@ -0,0 +1,96 @@
+# file : tests/file/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+cli_tun := test.cli
+
+tests := 000 001 002 003
+
+#
+#
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(cxx_obj)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+
+genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(out_root)/cli/cli
+$(gen): cli := $(out_root)/cli/cli
+$(gen): cli_options := --generate-file-scanner
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+test_targets := $(addprefix $(out_base)/.test-,$(tests))
+
+$(test): $(test_targets)
+$(test_targets): driver := $(driver)
+
+.PHONY: $(out_base)/.test-%
+
+$(out_base)/.test-000: $(driver) $(src_base)/test-000.ops
+ $(call message,test $(out_base)/000,$(driver) -a 1 \
+--file $(src_base)/empty.ops -b 1 --file $(src_base)/base.ops \
+--file $(src_base)/test-000.ops b | diff -u $(src_base)/test-000.std -)
+
+$(out_base)/.test-001: $(driver) $(src_base)/test-001.ops
+ $(call message,test $(out_base)/001,$(driver) -a 1 -- --file \
+$(src_base)/test-001.ops b | diff -u $(src_base)/test-001.std -)
+
+$(out_base)/.test-002: $(driver) $(src_base)/test-002.ops
+ $(call message,test $(out_base)/002,$(driver) -a 1 --file \
+$(src_base)/test-002.ops --file $(src_base)/empty.ops b | \
+diff -u $(src_base)/test-002.std -)
+
+$(out_base)/.test-003: $(driver)
+ $(call message,test $(out_base)/003,$(driver) -a 1 --file \
+$(src_base)/base.ops --file test-003.ops b | diff -u $(src_base)/test-003.std -)
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean))
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(scf_root)/cli/cli-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/cli/makefile)
+
diff --git a/tests/file/test-000.ops b/tests/file/test-000.ops
new file mode 100644
index 0000000..c0462e2
--- /dev/null
+++ b/tests/file/test-000.ops
@@ -0,0 +1,6 @@
+-a 11
+-b 11
+ -a 12
+
+ -b 12
+a
diff --git a/tests/file/test-000.std b/tests/file/test-000.std
new file mode 100644
index 0000000..392b83d
--- /dev/null
+++ b/tests/file/test-000.std
@@ -0,0 +1,18 @@
+-a
+1
+-b
+1
+-a
+21
+-b
+21
+-a
+11
+-b
+11
+-a
+12
+-b
+12
+a
+b
diff --git a/tests/file/test-001.ops b/tests/file/test-001.ops
new file mode 100644
index 0000000..ed080fb
--- /dev/null
+++ b/tests/file/test-001.ops
@@ -0,0 +1,3 @@
+# Empty options file.
+#
+
diff --git a/tests/file/test-001.std b/tests/file/test-001.std
new file mode 100644
index 0000000..36fabf3
--- /dev/null
+++ b/tests/file/test-001.std
@@ -0,0 +1,6 @@
+-a
+1
+--
+--file
+/home/boris/work/cli/cli/tests/file/test-001.ops
+b
diff --git a/tests/file/test-002.ops b/tests/file/test-002.ops
new file mode 100644
index 0000000..5c728a0
--- /dev/null
+++ b/tests/file/test-002.ops
@@ -0,0 +1,7 @@
+-a 11
+-b 11
+--
+--file base.ops
+-a 12
+-b 12
+a
diff --git a/tests/file/test-002.std b/tests/file/test-002.std
new file mode 100644
index 0000000..0336afe
--- /dev/null
+++ b/tests/file/test-002.std
@@ -0,0 +1,17 @@
+-a
+1
+-a
+11
+-b
+11
+--
+--file
+base.ops
+-a
+12
+-b
+12
+a
+--file
+/home/boris/work/cli/cli/tests/file/empty.ops
+b
diff --git a/tests/file/test-003.std b/tests/file/test-003.std
new file mode 100644
index 0000000..6e9a6ae
--- /dev/null
+++ b/tests/file/test-003.std
@@ -0,0 +1,7 @@
+-a
+1
+-a
+21
+-b
+21
+unable to open file 'test-003.ops' or read failure
diff --git a/tests/file/test.cli b/tests/file/test.cli
new file mode 100644
index 0000000..4bd40d0
--- /dev/null
+++ b/tests/file/test.cli
@@ -0,0 +1,8 @@
+// file : tests/file/test.cli
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+class options
+{
+};
diff --git a/tests/makefile b/tests/makefile
index 422b93b..5eef36e 100644
--- a/tests/makefile
+++ b/tests/makefile
@@ -5,7 +5,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
-tests := lexer parser
+tests := ctor erase file lexer parser
default := $(out_base)/
test := $(out_base)/.test