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

[Libguestfs] [PATCH v4 2/2] mllib: tests: Add tests of the new Getopt module.



---
 .gitignore            |   1 +
 mllib/Makefile.am     |  31 ++++++++-
 mllib/getopt_tests.ml |  68 ++++++++++++++++++++
 mllib/test-getopt.sh  | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 268 insertions(+), 2 deletions(-)
 create mode 100644 mllib/getopt_tests.ml
 create mode 100755 mllib/test-getopt.sh

diff --git a/.gitignore b/.gitignore
index 000e984..c1ae484 100644
--- a/.gitignore
+++ b/.gitignore
@@ -295,6 +295,7 @@ Makefile.in
 /mllib/common_gettext.ml
 /mllib/common_utils_tests
 /mllib/dummy
+/mllib/getopt_tests
 /mllib/guestfs_config.ml
 /mllib/JSON_tests
 /mllib/libdir.ml
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 69a996d..0433b4c 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -162,6 +162,15 @@ common_utils_tests_CPPFLAGS = \
 common_utils_tests_BOBJECTS = common_utils_tests.cmo
 common_utils_tests_XOBJECTS = $(common_utils_tests_BOBJECTS:.cmo=.cmx)
 
+getopt_tests_SOURCES = dummy.c
+getopt_tests_CPPFLAGS = \
+	-I. \
+	-I$(top_builddir) \
+	-I$(shell $(OCAMLC) -where) \
+	-I$(top_srcdir)/src
+getopt_tests_BOBJECTS = getopt_tests.cmo
+getopt_tests_XOBJECTS = $(getopt_tests_BOBJECTS:.cmo=.cmx)
+
 JSON_tests_SOURCES = dummy.c
 JSON_tests_BOBJECTS = JSON_tests.cmo
 JSON_tests_XOBJECTS = $(JSON_tests_BOBJECTS:.cmo=.cmx)
@@ -171,12 +180,18 @@ if !HAVE_OCAMLOPT
 common_utils_tests_THEOBJECTS = $(common_utils_tests_BOBJECTS)
 common_utils_tests.cmo: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
 
+getopt_tests_THEOBJECTS = $(getopt_tests_BOBJECTS)
+getopt_tests.cmo: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
+
 JSON_tests_THEOBJECTS = $(JSON_tests_BOBJECTS)
 JSON_tests.cmo: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
 else
 common_utils_tests_THEOBJECTS = $(common_utils_tests_XOBJECTS)
 common_utils_tests.cmx: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
 
+getopt_tests_THEOBJECTS = $(getopt_tests_XOBJECTS)
+getopt_tests.cmx: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
+
 JSON_tests_THEOBJECTS = $(JSON_tests_XOBJECTS)
 JSON_tests.cmx: OCAMLPACKAGES += $(OCAMLPACKAGES_TESTS)
 endif
@@ -193,6 +208,16 @@ common_utils_tests_LINK = \
 	  $(OCAMLPACKAGES) $(OCAMLPACKAGES_TESTS) \
 	  $(common_utils_tests_THEOBJECTS) -o $@
 
+getopt_tests_DEPENDENCIES = \
+	$(getopt_tests_THEOBJECTS) \
+	$(MLLIB_CMA) \
+	$(top_srcdir)/ocaml-link.sh
+getopt_tests_LINK = \
+	$(top_srcdir)/ocaml-link.sh -cclib '-lutils $(LIBXML2_LIBS) -lgnu' -- \
+	  $(OCAMLFIND) $(BEST) $(OCAMLFLAGS) $(OCAMLLINKFLAGS) \
+	  $(OCAMLPACKAGES) $(OCAMLPACKAGES_TESTS) \
+	  $(getopt_tests_THEOBJECTS) -o $@
+
 JSON_tests_DEPENDENCIES = \
 	$(JSON_tests_THEOBJECTS) \
 	$(MLLIB_CMA) \
@@ -205,8 +230,10 @@ JSON_tests_LINK = \
 
 TESTS_ENVIRONMENT = $(top_builddir)/run --test
 
-TESTS =
-check_PROGRAMS =
+TESTS = \
+	test-getopt.sh
+check_PROGRAMS = \
+	getopt_tests
 
 if HAVE_OCAML_PKG_OUNIT
 check_PROGRAMS += common_utils_tests JSON_tests
diff --git a/mllib/getopt_tests.ml b/mllib/getopt_tests.ml
new file mode 100644
index 0000000..9d432e9
--- /dev/null
+++ b/mllib/getopt_tests.ml
@@ -0,0 +1,68 @@
+(* mllib
+ * Copyright (C) 2016 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(* Test the Getopt module.  The tests are controlled by the
+ * test-getopt.sh script.
+ *)
+
+open Printf
+
+open Common_utils
+open Getopt.OptionName
+
+let adds = ref []
+let add_string = push_back adds
+
+let anons = ref []
+let anon_fun = push_back anons
+
+let ints = ref []
+let add_int = push_back ints
+
+let clear_flag = ref true
+let set_flag = ref false
+let si = ref 42
+let ss = ref "not set"
+
+let argspec = [
+  [ S 'a'; L"add" ],  Getopt.String ("string", add_string), "Add string";
+  [ S 'c'; L"clear" ], Getopt.Clear clear_flag, "Clear flag";
+  [ S 'i'; L"int" ], Getopt.Int ("int", add_int), "Add int";
+  [ M"ii"; L"set-int" ], Getopt.Set_int ("int", si), "Set int";
+  [ M"is"; L"set-string"], Getopt.Set_string ("string", ss), "Set string";
+  [ S 't'; L"set" ], Getopt.Set set_flag, "Set flag";
+]
+
+let usage_msg = sprintf "%s: test the Getopt parser" prog
+
+let opthandle = create_standard_options argspec ~anon_fun usage_msg
+let () =
+  Getopt.parse opthandle;
+
+  (* Implicit settings. *)
+  printf "trace = %b\n" (trace ());
+  printf "verbose = %b\n" (verbose ());
+
+  (* Print the results. *)
+  printf "adds = [%s]\n" (String.concat ", " !adds);
+  printf "anons = [%s]\n" (String.concat ", " !anons);
+  printf "ints = [%s]\n" (String.concat ", " (List.map string_of_int !ints));
+  printf "clear_flag = %b\n" !clear_flag;
+  printf "set_flag = %b\n" !set_flag;
+  printf "set_int = %d\n" !si;
+  printf "set_string = %s\n" !ss
diff --git a/mllib/test-getopt.sh b/mllib/test-getopt.sh
new file mode 100755
index 0000000..3feafac
--- /dev/null
+++ b/mllib/test-getopt.sh
@@ -0,0 +1,170 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2016 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test the Getopt module.
+# See also: getopt_tests.ml
+
+set -e
+set -x
+
+t=./getopt_tests
+
+expect_fail ()
+{
+    if "$@"; then
+        echo "$@" ": this command was expected to exit with an error"
+        exit 1
+    fi
+}
+
+# Program works.
+$t
+
+# Flags added automatically by Common_utils.
+$t | grep '^trace = false'
+$t | grep '^verbose = false'
+
+$t -x | grep '^trace = true'
+$t --verbose | grep '^verbose = true'
+
+# --help
+$t --help | grep '^getopt_tests: test the Getopt parser'
+$t --help | grep '^Options:'
+$t --help | grep -- '-i, --int <int>'
+$t --help | grep -- '-ii, --set-int <int>'
+$t --help | grep -- '-v, --verbose'
+$t --help | grep -- '-x'
+
+# --version
+$t --version | grep '^getopt_tests 1\.'
+
+# --short-options
+$t --short-options | grep '^-a'
+$t --short-options | grep '^-c'
+$t --short-options | grep '^-i'
+$t --short-options | grep '^-q'
+$t --short-options | grep '^-ii'
+$t --short-options | grep '^-is'
+$t --short-options | grep '^-t'
+$t --short-options | grep '^-V'
+$t --short-options | grep '^-v'
+$t --short-options | grep '^-x'
+
+# --long-options
+$t --long-options | grep '^--help'
+$t --long-options | grep '^--add'
+$t --long-options | grep '^--clear'
+$t --long-options | grep '^--color'
+$t --long-options | grep '^--colors'
+$t --long-options | grep '^--colour'
+$t --long-options | grep '^--colours'
+$t --long-options | grep '^--debug-gc'
+$t --long-options | grep '^--int'
+$t --long-options | grep '^--quiet'
+$t --long-options | grep '^--set'
+$t --long-options | grep '^--set-int'
+$t --long-options | grep '^--set-string'
+$t --long-options | grep '^--ii'
+$t --long-options | grep '^--is'
+$t --long-options | grep '^--version'
+$t --long-options | grep '^--verbose'
+
+# -a/--add parameter.
+$t | grep '^adds = \[\]'
+$t -a A | grep '^adds = \[A\]'
+$t -a A -a B | grep '^adds = \[A, B\]'
+$t --add A | grep '^adds = \[A\]'
+$t --add A -a B | grep '^adds = \[A, B\]'
+expect_fail $t -a
+expect_fail $t --add
+
+# -c/--clear parameter.
+$t | grep '^clear_flag = true'
+$t -c | grep '^clear_flag = false'
+$t --clear | grep '^clear_flag = false'
+
+# -i/--int parameter.
+$t | grep '^ints = \[\]'
+$t -i 1 | grep '^ints = \[1\]'
+$t -i 1 -i 2 | grep '^ints = \[1, 2\]'
+$t -i 1 --int 2 --int 3 | grep '^ints = \[1, 2, 3\]'
+expect_fail $t --int
+
+# Non-integer parameters.
+expect_fail $t --int --int
+expect_fail $t --int ""
+expect_fail $t --int ABC
+expect_fail $t --int 0.3
+expect_fail $t --int 0E
+expect_fail $t --int 0ABC
+
+# Negative and large integer parameters.
+# All int parameters must be within signed 31 bit (even on 64 bit arch),
+# and anything else should be rejected.
+$t -i -1 | grep '^ints = \[-1\]'
+$t -i -1073741824 | grep '^ints = \[-1073741824\]'
+$t -i  1073741823 | grep '^ints = \[1073741823\]'
+expect_fail $t -i -1073741825
+expect_fail $t -i  1073741824
+expect_fail $t -i -2147483648
+expect_fail $t -i  2147483647
+expect_fail $t -i -4611686018427387904
+expect_fail $t -i  4611686018427387903
+expect_fail $t -i -9223372036854775808
+expect_fail $t -i  9223372036854775807
+
+# -t/--set parameter.
+$t | grep '^set_flag = false'
+$t -t | grep '^set_flag = true'
+$t --set | grep '^set_flag = true'
+
+# --ii/--set-int parameter.
+$t | grep '^set_int = 42'
+$t --ii 1 | grep '^set_int = 1'
+$t --set-int 2 | grep '^set_int = 2'
+expect_fail $t --ii
+expect_fail $t --set-int
+expect_fail $t --set-int -i
+expect_fail $t --set-int ""
+expect_fail $t --set-int ABC
+expect_fail $t --set-int 0.3
+expect_fail $t --set-int 1e1
+expect_fail $t --set-int 0E
+expect_fail $t --set-int 0ABC
+
+# --is/--set-string parameter.
+$t | grep '^set_string = not set'
+$t --is A | grep '^set_string = A'
+$t --set-string B | grep '^set_string = B'
+expect_fail $t --is
+expect_fail $t --set-string
+
+# Anonymous parameters.
+$t | grep '^anons = \[\]'
+$t 1 | grep '^anons = \[1\]'
+$t 1 2 3 | grep '^anons = \[1, 2, 3\]'
+
+# Grouping single letter options.
+$t -cti1 | grep '^clear_flag = false'
+$t -cti1 | grep '^set_flag = true'
+$t -cti1 | grep '^ints = \[1\]'
+$t -i1 -i2 | grep '^ints = \[1, 2\]'
+
+# Short versions of long options (used by virt-v2v).
+$t -ii 1 | grep '^set_int = 1'
+$t -is A | grep '^set_string = A'
-- 
2.7.4


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