From: Bruno Haible Date: Mon, 28 Jul 2025 08:16:38 +0000 (+0200) Subject: OCaml support: Add a unit test. X-Git-Tag: v1.0~118 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=8a1197f724a307583ede5e78f2df4a674866397e;p=thirdparty%2Fgettext.git OCaml support: Add a unit test. * gettext-tools/tests/lang-ocaml: New file. * gettext-tools/tests/Makefile.am (TESTS): Add it. * HACKING: Document dependencies for lang-ocaml. --- diff --git a/HACKING b/HACKING index 3a39542a1..40bbe4602 100644 --- a/HACKING +++ b/HACKING @@ -316,6 +316,19 @@ are skipped. To this effect, you need to install also: - On Red Hat distributions: --. - Other: https://repology.org/project/fpc/versions + * OCaml + + Homepage: https://ocaml.org/ + + Pre-built package name: + - On Debian and Debian-based systems: ocaml, + - On Red Hat distributions: ocaml. + - Other: https://repology.org/project/ocaml/versions + * The OCaml package manager + + Homepage: https://opam.ocaml.org/ + + Pre-built package name: + - On Debian and Debian-based systems: opam, + - On Red Hat distributions: --. + - Other: https://repology.org/project/opam/versions + * GNU smalltalk + Homepage: http://smalltalk.gnu.org/ + Pre-built package name: diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 3ff4ef642..9f54c706a 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -250,6 +250,7 @@ TESTS = gettext-1 gettext-2 \ lang-pascal \ lang-modula2 \ lang-d \ + lang-ocaml \ lang-smalltalk \ lang-vala \ lang-tcl \ diff --git a/gettext-tools/tests/lang-ocaml b/gettext-tools/tests/lang-ocaml new file mode 100755 index 000000000..8593c9476 --- /dev/null +++ b/gettext-tools/tests/lang-ocaml @@ -0,0 +1,158 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test of gettext facilities in the OCaml language. +# Assumes an fr_FR locale is installed. +# Assumes the following packages are installed: +# - OCaml, +# - 'opam', the OCaml package manager. +# The following get installed through 'opam': +# - 'dune', part of the OCaml build system, +# - the opam package 'gettext-stub' +# +# +# + +# Test for presence of opam. +(opam --version) >/dev/null 2>/dev/null \ + || { echo "Skipping test: opam not found"; Exit 77; } + +# Set environment variables for using opam, including PATH. +eval $(opam env) + +# Ensure dune is installed. +opam install dune \ + || { echo "Skipping test: failed to install dune"; Exit 77; } + +# Test for presence of dune. +(dune --version) >/dev/null 2>/dev/null \ + || { echo "Skipping test: dune not found"; Exit 77; } + +cat <<\EOF > prog.ml +module ConfiguredGettext = + (* See https://gildor478.github.io/ocaml-gettext/gettext/Gettext/Program/index.html *) + Gettext.Program + (* See https://gildor478.github.io/ocaml-gettext/gettext/GettextTypes/module-type-INIT_TYPE/index.html *) + (struct + let textdomain = "prog" + let codeset = None + let dir = Some "." + let dependencies = [] + end) + (* See https://github.com/gildor478/ocaml-gettext/blob/master/doc/reference-manual.md *) + (GettextStub.Native) + +open ConfiguredGettext;; +open Printf;; + +let () = + let arg1 = Sys.argv.(1) in + let n = int_of_string arg1 in + print_endline (s_ "'Your command, please?', asked the waiter."); + print_endline (sprintf (fn_ "%u piece of cake" "%u pieces of cake" n) n) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} -o prog.tmp --omit-header --no-location prog.ml \ + || Exit 1 +LC_ALL=C tr -d '\r' < prog.tmp > prog.pot || Exit 1 + +cat <<\EOF > prog.ok +msgid "'Your command, please?', asked the waiter." +msgstr "" + +#, ocaml-format +msgid "%u piece of cake" +msgid_plural "%u pieces of cake" +msgstr[0] "" +msgstr[1] "" +EOF + +: ${DIFF=diff} +${DIFF} prog.ok prog.pot || Exit 1 + +cat <<\EOF > fr.po +msgid "" +msgstr "" +"Content-Type: text/plain; charset=ISO-8859-1\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +msgid "'Your command, please?', asked the waiter." +msgstr "«Votre commande, s'il vous plait», dit le garçon." + +# Les gateaux allemands sont les meilleurs du monde. +#, ocaml-format +msgid "%u piece of cake" +msgid_plural "%u pieces of cake" +msgstr[0] "un morceau de gateau" +msgstr[1] "%u morceaux de gateau" +EOF + +: ${MSGMERGE=msgmerge} +${MSGMERGE} -q -o fr.po.tmp fr.po prog.pot || Exit 1 +LC_ALL=C tr -d '\r' < fr.po.tmp > fr.po.new || Exit 1 + +: ${DIFF=diff} +${DIFF} fr.po fr.po.new || Exit 1 + +test -d fr || mkdir fr +test -d fr/LC_MESSAGES || mkdir fr/LC_MESSAGES + +: ${MSGFMT=msgfmt} +${MSGFMT} -c -o fr/LC_MESSAGES/prog.mo fr.po + +opam install gettext-stub \ + || { echo "Skipping test: failed to install gettext-stub"; Exit 77; } + +cat > dune <<\EOF +(executable + (name prog) + (libraries gettext.base gettext-stub unix)) +EOF + +cat > dune-project <<\EOF +(lang dune 3.19) +(name prog) +EOF + +dune build --root=. prog.exe \ + || { echo "Build failure" 1>&2; Exit 1; } + +: ${DIFF=diff} +cat <<\EOF > prog.ok +«Votre commande, s'il vous plait», dit le garçon. +2 morceaux de gateau +EOF +cat <<\EOF > prog.oku +«Votre commande, s'il vous plait», dit le garçon. +2 morceaux de gateau +EOF + +: ${LOCALE_FR=fr_FR} +: ${LOCALE_FR_UTF8=fr_FR.UTF-8} +if test $LOCALE_FR != none; then + LC_ALL=$LOCALE_FR LANGUAGE= _build/default/prog.exe 2 > prog.out + case $? in + 0) ${DIFF} prog.ok prog.out || Exit 1;; + 77) LOCALE_FR=none;; + *) Exit 1;; + esac +fi +if test $LOCALE_FR_UTF8 != none; then + LC_ALL=$LOCALE_FR_UTF8 LANGUAGE= _build/default/prog.exe 2 > prog.out + case $? in + 0) ${DIFF} prog.oku prog.out || Exit 1;; + 77) LOCALE_FR_UTF8=none;; + *) Exit 1;; + esac +fi +if test $LOCALE_FR = none && test $LOCALE_FR_UTF8 = none; then + if test -f /usr/bin/localedef; then + echo "Skipping test: no french locale is installed" + else + echo "Skipping test: no french locale is supported" + fi + Exit 77 +fi + +Exit 0