From: Eshel Yaron Date: Sat, 27 Aug 2022 15:46:55 +0000 (+0300) Subject: TEST: Add ERT based tests X-Git-Tag: v0.2.0~85 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4ee42917036e70c7e8b516a0b2a6f0b2f80d380c;p=dict.git TEST: Add ERT based tests --- diff --git a/.build.yml b/.build.yml index 7e2aed0..394cd48 100644 --- a/.build.yml +++ b/.build.yml @@ -26,6 +26,7 @@ packages: - libyaml-dev - zip - git + - emacs secrets: - 750079bb-9735-473b-bebf-db897c9f0c6b - 72d5c3dc-f83f-4cc2-96e3-b2b08f6ee8a0 @@ -38,4 +39,5 @@ tasks: sudo localectl set-locale LANG=en_US.UTF-8 localectl set-locale LANG=en_US.UTF-8 make - make info + make sweep.info + make check diff --git a/Makefile b/Makefile index e507296..fc31a9c 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ CMAKE_OPTIONS += -DSWIPL_PACKAGES_JAVA=OFF CMAKE_OPTIONS += -DSWIPL_PACKAGES_X=OFF CMAKE_OPTIONS += -DSWIPL_INSTALL_IN_LIB=ON -.PHONY: clean all swipl +.PHONY: clean all swipl check all: $(TARGET) @@ -58,3 +58,6 @@ lib/libswipl.$(SOEXT): $(BASENAME).info:: README.org emacs -Q --batch --eval '(require (quote ox-texinfo))' --eval "(with-current-buffer (find-file \"README.org\") (org-export-to-file (quote texinfo) \"$@\" nil nil nil nil nil (quote org-texinfo-compile)))" + +check: $(TARGET) + emacs -batch --eval '(add-to-list (quote load-path) (expand-file-name "."))' -l ert -l sweep -l sweep-tests.el -f ert-run-tests-batch-and-exit diff --git a/sweep-tests.el b/sweep-tests.el new file mode 100644 index 0000000..c2dc790 --- /dev/null +++ b/sweep-tests.el @@ -0,0 +1,18 @@ +(ert-deftest lists:permutation/2 () + "Tests calling the Prolog predicate permutation/2 from Elisp." + (should (equal (sweep-open-query "user" "lists" "permutation" (list 1 2 3)) t)) + (should (equal (sweep-next-solution) (list t 1 2 3))) + (should (equal (sweep-next-solution) (list t 1 3 2))) + (should (equal (sweep-next-solution) (list t 2 1 3))) + (should (equal (sweep-next-solution) (list t 2 3 1))) + (should (equal (sweep-next-solution) (list t 3 1 2))) + (should (equal (sweep-next-solution) (list t 3 2 1))) + (should (equal (sweep-next-solution) nil)) + (should (equal (sweep-cut-query) t))) + +(ert-deftest system:=/2 () + "Tests calling the Prolog predicate permutation/2 from Elisp." + (should (equal (sweep-open-query "user" "system" "=" (list 1 2 3)) t)) + (should (equal (sweep-next-solution) (list '! 1 2 3))) + (should (equal (sweep-next-solution) nil)) + (should (equal (sweep-cut-query) t))) diff --git a/sweep.c b/sweep.c index 1d6a6af..ff148c7 100644 --- a/sweep.c +++ b/sweep.c @@ -154,9 +154,16 @@ term_to_value_compound(emacs_env *env, term_t t) { const char * chars = NULL; size_t len = 0; emacs_value * vals = NULL; + emacs_value res = NULL; size_t n = 0; - (void)PL_get_compound_name_arity(t, &name, &arity); + + if (!PL_get_compound_name_arity(t, &name, &arity)) { + ethrow(env, "Not a compound"); + goto cleanup; + } + chars = PL_atom_nchars(name, &len); + vals = (emacs_value*)malloc(sizeof(emacs_value)*arity + 1); if (vals == NULL) { ethrow(env, "malloc failed"); @@ -167,12 +174,19 @@ term_to_value_compound(emacs_env *env, term_t t) { vals[0] = env->make_string(env, chars, len); for(n=1; n<=arity; n++) { - - (void)PL_get_arg(n, t, arg); + if (!PL_get_arg(n, t, arg)) { + ethrow(env, "get_arg falied"); + goto cleanup; + } vals[n] = term_to_value(env, arg); } - return econs(env, env->intern(env, "compound"), env->funcall(env, env->intern(env, "list"), arity + 1, vals)); + res = econs(env, env->intern(env, "compound"), env->funcall(env, env->intern(env, "list"), arity + 1, vals)); + + cleanup: + if (vals != NULL) free(vals); + + return res; } emacs_value