Home | History | Annotate | Line # | Download | only in tests
      1 #! /bin/sh
      2 
      3 # Test checking of PHP format strings.
      4 
      5 tmpfiles=""
      6 trap 'rm -fr $tmpfiles' 1 2 3 15
      7 
      8 tmpfiles="$tmpfiles f-ph-2.data"
      9 cat <<\EOF > f-ph-2.data
     10 # Valid: %% doesn't count
     11 msgid  "abc%%def"
     12 msgstr "xyz"
     13 # Invalid: invalid msgstr
     14 msgid  "abc%%def"
     15 msgstr "xyz%"
     16 # Valid: same arguments
     17 msgid  "abc%s%gdef"
     18 msgstr "xyz%s%g"
     19 # Valid: same arguments, with different widths
     20 msgid  "abc%2sdef"
     21 msgstr "xyz%3s"
     22 # Valid: same arguments but in numbered syntax
     23 msgid  "abc%s%gdef"
     24 msgstr "xyz%1$s%2$g"
     25 # Valid: permutation
     26 msgid  "abc%s%g%cdef"
     27 msgstr "xyz%3$c%2$g%1$s"
     28 # Invalid: too few arguments
     29 msgid  "abc%2$udef%1$s"
     30 msgstr "xyz%1$s"
     31 # Invalid: too few arguments
     32 msgid  "abc%sdef%u"
     33 msgstr "xyz%s"
     34 # Invalid: too many arguments
     35 msgid  "abc%udef"
     36 msgstr "xyz%uvw%c"
     37 # Valid: same numbered arguments, with different widths
     38 msgid  "abc%2$5s%1$4s"
     39 msgstr "xyz%2$4s%1$5s"
     40 # Invalid: missing argument
     41 msgid  "abc%2$sdef%1$u"
     42 msgstr "xyz%1$u"
     43 # Invalid: missing argument
     44 msgid  "abc%1$sdef%2$u"
     45 msgstr "xyz%2$u"
     46 # Invalid: added argument
     47 msgid  "abc%1$udef"
     48 msgstr "xyz%1$uvw%2$c"
     49 # Valid: type compatibility
     50 msgid  "abc%b"
     51 msgstr "xyz%d"
     52 # Valid: type compatibility
     53 msgid  "abc%u"
     54 msgstr "xyz%d"
     55 # Valid: type compatibility
     56 msgid  "abc%o"
     57 msgstr "xyz%d"
     58 # Valid: type compatibility
     59 msgid  "abc%x"
     60 msgstr "xyz%d"
     61 # Valid: type compatibility
     62 msgid  "abc%X"
     63 msgstr "xyz%d"
     64 # Valid: type compatibility
     65 msgid  "abc%e"
     66 msgstr "xyz%f"
     67 # Invalid: type incompatibility
     68 msgid  "abc%s"
     69 msgstr "xyz%d"
     70 # Invalid: type incompatibility
     71 msgid  "abc%s"
     72 msgstr "xyz%e"
     73 # Invalid: type incompatibility
     74 msgid  "abc%s"
     75 msgstr "xyz%c"
     76 # Invalid: type incompatibility
     77 msgid  "abc%d"
     78 msgstr "xyz%e"
     79 # Invalid: type incompatibility
     80 msgid  "abc%d"
     81 msgstr "xyz%c"
     82 # Invalid: type incompatibility
     83 msgid  "abc%e"
     84 msgstr "xyz%c"
     85 EOF
     86 
     87 : ${MSGFMT=msgfmt}
     88 n=0
     89 while read comment; do
     90   read msgid_line
     91   read msgstr_line
     92   n=`expr $n + 1`
     93   tmpfiles="$tmpfiles f-ph-2-$n.po f-ph-2-$n.mo"
     94   cat <<EOF > f-ph-2-$n.po
     95 #, php-format
     96 ${msgid_line}
     97 ${msgstr_line}
     98 EOF
     99   fail=
    100   if echo "$comment" | grep 'Valid:' > /dev/null; then
    101     if ${MSGFMT} --check-format -o f-ph-2-$n.mo f-ph-2-$n.po; then
    102       :
    103     else
    104       fail=yes
    105     fi
    106   else
    107     ${MSGFMT} --check-format -o f-ph-2-$n.mo f-ph-2-$n.po 2> /dev/null
    108     if test $? = 1; then
    109       :
    110     else
    111       fail=yes
    112     fi
    113   fi
    114   if test -n "$fail"; then
    115     echo "Format string checking error:" 1>&2
    116     cat f-ph-2-$n.po 1>&2
    117     exit 1
    118   fi
    119   rm -f f-ph-2-$n.po f-ph-2-$n.mo
    120 done < f-ph-2.data
    121 
    122 rm -fr $tmpfiles
    123 
    124 exit 0
    125