]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
OCaml support: Add a unit test.
authorBruno Haible <bruno@clisp.org>
Mon, 28 Jul 2025 08:16:38 +0000 (10:16 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 28 Jul 2025 08:16:38 +0000 (10:16 +0200)
* gettext-tools/tests/lang-ocaml: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.
* HACKING: Document dependencies for lang-ocaml.

HACKING
gettext-tools/tests/Makefile.am
gettext-tools/tests/lang-ocaml [new file with mode: 0755]

diff --git a/HACKING b/HACKING
index 3a39542a1796da8c6496a4ec496cc5d8e8f0863f..40bbe46027048941f8fd38ad9d9c178df5b50ee2 100644 (file)
--- 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:
index 3ff4ef642a76010f4e869fbb86127f9525a7d332..9f54c706ab0d69fb81956ed6c635a2045784391f 100644 (file)
@@ -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 (executable)
index 0000000..8593c94
--- /dev/null
@@ -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'
+#     <https://opam.ocaml.org/packages/gettext-stub/>
+#     <https://github.com/gildor478/ocaml-gettext/>
+#     <https://gildor478.github.io/ocaml-gettext/>
+
+# 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