#! /bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src

# Test recognition of OCaml format strings.

cat <<\EOF > f-oc-1.data
# Valid: no argument
"abc%!"
# Valid: no argument
"abc%%"
# Valid: no argument
"abc%@"
# Valid: no argument
"abc%,"
# Valid: one integer argument
"abc%d"
# Valid: one integer argument
"abc%i"
# Valid: one integer argument
"abc%u"
# Valid: one integer argument
"abc%x"
# Valid: one integer argument
"abc%X"
# Valid: one integer argument
"abc%o"
# Valid: one int32 argument
"abc%ld"
# Valid: one int32 argument
"abc%li"
# Valid: one int32 argument
"abc%lu"
# Valid: one int32 argument
"abc%lx"
# Valid: one int32 argument
"abc%lX"
# Valid: one int32 argument
"abc%lo"
# Valid: one nativeint argument
"abc%nd"
# Valid: one nativeint argument
"abc%ni"
# Valid: one nativeint argument
"abc%nu"
# Valid: one nativeint argument
"abc%nx"
# Valid: one nativeint argument
"abc%nX"
# Valid: one nativeint argument
"abc%no"
# Valid: one int64 argument
"abc%Ld"
# Valid: one int64 argument
"abc%Li"
# Valid: one int64 argument
"abc%Lu"
# Valid: one int64 argument
"abc%Lx"
# Valid: one int64 argument
"abc%LX"
# Valid: one int64 argument
"abc%Lo"
# Valid: one string argument
"abc%s"
# Valid: one string argument
"abc%S"
# Valid: one character argument
"abc%c"
# Valid: one character argument
"abc%C"
# Valid: one floating-point argument
"abc%f"
# Valid: one floating-point argument
"abc%e"
# Valid: one floating-point argument
"abc%E"
# Valid: one floating-point argument
"abc%g"
# Valid: one floating-point argument
"abc%G"
# Valid: one floating-point argument
"abc%h"
# Valid: one floating-point argument
"abc%H"
# Valid: one floating-point argument
"abc%F"
# Valid: one boolean argument
"abc%B"
# Valid: one function argument
"abc%a"
# Valid: one function-with-arg argument
"abc%t"
# Valid: one format-string argument
"abc%{%s%}"
# Valid: one format-string argument
"abc%{%S%}"
# Valid: one format-string argument and one string argument
"abc%(%s%)"
# Valid: one format-string argument and one string argument
"abc%(%S%)"
# Valid: one argument with flags
"abc%0#g"
# Valid: one argument with width
"abc%2g"
# Valid: one argument with width
"abc%*g"
# Valid: one argument with precision
"abc%.4g"
# Valid: one argument with precision
"abc%.*g"
# Valid: one argument with width and precision
"abc%14.4g"
# Valid: one argument with width and precision
"abc%14.*g"
# Valid: one argument with width and precision
"abc%*.4g"
# Valid: one argument with width and precision
"abc%*.*g"
# Invalid: unterminated
"abc%"
# Invalid: unterminated
"abc%l"
# Invalid: unterminated
"abc%n"
# Invalid: unterminated
"abc%L"
# Invalid: unknown format specifier
"abc%y"
# Invalid: unknown format specifier
"abc%A"
# Invalid: unknown format specifier
"abc%T"
# Invalid: %{ and %} don't match
"abc%{%s"
# Invalid: %{ and %} don't match
"abc%s%}"
# Invalid: %{ and %} don't match
"abc%s%}%{"
# Invalid: %( and %) don't match
"abc%(%s"
# Invalid: %( and %) don't match
"abc%s%)"
# Invalid: %( and %) don't match
"abc%s%)%("
# Invalid: %{%} and %(%) nesting
"abc%{%(%}%)"
# Invalid: %{%} and %(%) nesting
"abc%(%{%)%}"
# Invalid: flags after width
"abc%*0g"
# Valid: null precision
"abc%.f"
# Invalid: twice precision
"abc%.4.2g"
# Valid: three arguments
"abc%d%u%u"
# Valid only in msgstr: a numbered argument
"abc%1$d"
# Invalid: zero
"abc%0$d"
# Valid only in msgstr: 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"
# Valid only in msgstr: three arguments, two with same number
"abc%1$4x,%2$c,%1$u"
# Invalid: argument with conflicting types
"abc%1$4x,%2$c,%1$s"
# Valid only in msgstr: no conflicting types
"abc%2$t%1$d"
# Invalid: argument with conflicting types
"abc%1$t%2$d"
# Valid only in msgstr: no conflict
"abc%1$4x,%2$c,%1$u"
# Invalid: mixing of numbered and unnumbered arguments
"abc%d%2$x"
# Valid only in msgstr: numbered argument with constant precision
"abc%1$.9x"
# Invalid: mixing of numbered and unnumbered arguments
"abc%1$.*x"
# Valid only in msgstr: missing non-final argument
"abc%2$x%3$s"
# Valid only in msgstr: permutation
"abc%2$ddef%1$d"
# Valid only in msgstr: multiple uses of same argument
"abc%2$xdef%1$sghi%2$x"
# Valid only in msgstr: one argument with width
"abc%2$#*1$g"
# Valid only in msgstr: one argument with width and precision
"abc%3$*2$.*1$g"
# Invalid: zero
"abc%2$*0$.*1$g"
EOF

: ${XGETTEXT=xgettext}
n=0
while read comment; do
  read string
  n=`expr $n + 1`
  cat <<EOF > f-oc-1-$n.in
let a = f_ ${string}
EOF
  ${XGETTEXT} -L OCaml -o f-oc-1-$n.po f-oc-1-$n.in || Exit 1
  test -f f-oc-1-$n.po || Exit 1
  fail=
  if echo "$comment" | grep 'Valid:' > /dev/null; then
    if grep ocaml-format f-oc-1-$n.po > /dev/null; then
      :
    else
      fail=yes
    fi
  else
    if grep ocaml-format f-oc-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-oc-1-$n.in 1>&2
    echo "Got:" 1>&2
    cat f-oc-1-$n.po 1>&2
    Exit 1
  fi
  rm -f f-oc-1-$n.in f-oc-1-$n.po
done < f-oc-1.data

Exit 0
