gettext/gettext-tools/tests/format-javascript-1
Bruno Haible 5a22e428a6 javascript-format: Reject null precision.
Proof that it's invalid:
------------------------------- foo.js -------------------------------
const Format = imports.format;
String.prototype.format = Format.format;
print("%.f".format(3.1415916535));
----------------------------------------------------------------------
$ gjs foo.js

* gettext-tools/src/format-invalid.h (INVALID_PRECISION_MISSING): New macro.
* gettext-tools/src/format-java-printf.c (INVALID_PRECISION_MISSING): Remove
macro.
* gettext-tools/src/format-javascript.c: Fix comment regarding the precision.
(format_parse): Report an error if the precision is null (empty).
* gettext-tools/tests/format-javascript-1: Add a test case with null precision.
2025-06-22 01:20:32 +02:00

100 lines
2.1 KiB
Bash
Executable File

#! /bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src
# Test recognition of JavaScript format strings.
cat <<\EOF > f-js-1.data
# Valid: no argument
"abc%%"
# Valid: one character argument
"abc%c"
# Valid: one string argument
"abc%s"
# Valid: one integer argument
"abc%b"
# Valid: one integer argument
"abc%d"
# Valid: one integer argument
"abc%o"
# Valid: one integer argument
"abc%x"
# Valid: one integer argument
"abc%X"
# Valid: one floating-point argument
"abc%f"
# Valid: one object argument
"abc%j"
# Valid: one argument with flags
"abc%Id"
# Valid: one argument with width
"abc%2d"
# Valid: one argument with precision
"abc%.4f"
# Valid: one argument with width and precision
"abc%14.4f"
# Invalid: unterminated
"abc%"
# Invalid: unknown format specifier
"abc%y"
# Invalid: flags after width
"abc%1Ig"
# Invalid: null precision
"abc%.f"
# Invalid: twice precision
"abc%.4.2f"
# Valid: three arguments
"abc%d%j%j"
# Valid: a numbered argument
"abc%1$d"
# Invalid: zero
"abc%0$d"
# Valid: two-digit numbered arguments
"abc%11$def%10$dgh%9$dij%8$dkl%7$dmn%6$dop%5$dqr%4$dst%3$duv%2$dwx%1$dyz"
# Invalid: unterminated number
"abc%1"
# Invalid: flags before number
"abc%+1$d"
# Invalid: mixing of numbered and unnumbered arguments
"abc%d%2$x"
# Valid: multiple uses of same argument
"abc%2$xdef%1$sghi%2$x"
EOF
: ${XGETTEXT=xgettext}
n=0
while read comment; do
read string
n=`expr $n + 1`
cat <<EOF > f-js-1-$n.in
gettext(${string});
EOF
# Hide xgettext's "The translator cannot reorder the arguments." warnings.
${XGETTEXT} -L JavaScript -o f-js-1-$n.po f-js-1-$n.in 2> f-js-1.err \
|| { cat f-js-1.err 1>&2; Exit 1; }
test -f f-js-1-$n.po || Exit 1
fail=
if echo "$comment" | grep 'Valid:' > /dev/null; then
if grep javascript-format f-js-1-$n.po > /dev/null; then
:
else
fail=yes
fi
else
if grep javascript-format f-js-1-$n.po > /dev/null; then
fail=yes
else
:
fi
fi
if test -n "$fail"; then
echo "Format string recognition error:" 1>&2
cat f-js-1-$n.in 1>&2
echo "Got:" 1>&2
cat f-js-1-$n.po 1>&2
Exit 1
fi
rm -f f-js-1-$n.in f-js-1-$n.po
done < f-js-1.data
Exit 0