2013-05-08 02:30:08 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2014-07-13 19:00:37 -05:00
|
|
|
set -e
|
|
|
|
|
2013-12-13 01:07:26 +00:00
|
|
|
if which valgrind > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
VALGRIND='valgrind --error-exitcode=1 --leak-check=full --suppressions=tests/onig.supp'
|
2014-07-27 17:41:40 -05:00
|
|
|
Q=-q
|
2013-12-13 01:07:26 +00:00
|
|
|
else
|
|
|
|
VALGRIND=
|
2014-07-27 17:41:40 -05:00
|
|
|
Q=
|
2013-12-13 01:07:26 +00:00
|
|
|
fi
|
|
|
|
|
2014-07-27 17:41:40 -05:00
|
|
|
cat $@ | $VALGRIND $Q ./jq --run-tests
|
2014-07-13 19:00:37 -05:00
|
|
|
|
|
|
|
d=
|
|
|
|
trap '[ -n "$d" ] && rm -rf "$d"' EXIT
|
|
|
|
d=`mktemp -d -t || true`
|
|
|
|
if [ -z "$d" ]; then
|
|
|
|
echo "Your OS does not support mktemp(1) -d" 1>&2
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2014-07-27 17:33:22 -05:00
|
|
|
## Test constant folding
|
|
|
|
|
|
|
|
# String constant folding (addition only)
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '"foo"' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Numeric constant folding (not all ops yet)
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '1+1' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '1-1' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '2*3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '9/3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '9==3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '9!=3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '9<=3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
n=`$VALGRIND $Q ./jq -n --debug-dump-disasm '9>=3' | wc -l`
|
|
|
|
if [ $n -ne 5 ]; then
|
|
|
|
echo "Constant expression folding for strings didn't work"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-08-10 16:52:03 -05:00
|
|
|
v=`scripts/version`
|
|
|
|
case "$v" in
|
2014-08-20 20:48:48 -05:00
|
|
|
*-*) v=`echo $v|sed -e 's/-.*$/-master/'`;;
|
2014-08-10 16:52:03 -05:00
|
|
|
*) true;;
|
|
|
|
esac
|
|
|
|
|
2014-10-10 22:19:38 -05:00
|
|
|
## Test JSON sequence support
|
|
|
|
|
|
|
|
cat > $d/expected <<EOF
|
|
|
|
ignoring parse error: Potentially truncated top-level numeric value at line 1, column 2
|
|
|
|
ignoring parse error: Truncated value at line 2, column 5
|
|
|
|
ignoring parse error: Truncated value at line 2, column 25
|
|
|
|
ignoring parse error: Truncated value at line 2, column 41
|
|
|
|
EOF
|
|
|
|
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"\036false\n'|$VALGRIND $Q ./jq -ces --seq '. == [2,3,[4,5],true,"ab",{},false]' > /dev/null 2> $d/out
|
|
|
|
cmp $d/out $d/expected
|
|
|
|
|
|
|
|
cat > $d/expected <<EOF
|
|
|
|
ignoring parse error: Potentially truncated top-level numeric value at line 1, column 2
|
|
|
|
ignoring parse error: Truncated value at line 2, column 5
|
|
|
|
ignoring parse error: Truncated value at line 2, column 25
|
|
|
|
ignoring parse error: Invalid literal at line 3, column 1
|
|
|
|
EOF
|
|
|
|
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"false\n\036null'|$VALGRIND $Q ./jq -ces --seq '. == [2,3,[4,5],true,"ab",{},null]' > /dev/null 2> $d/out
|
|
|
|
cmp $d/out $d/expected
|
|
|
|
|
|
|
|
# Note that here jq sees no inputs at all but it still succeeds because
|
|
|
|
# --seq ignores parse errors
|
|
|
|
cat > $d/expected <<EOF
|
Add Streaming parser (--stream)
Streaming means that outputs are produced as soon as possible. With the
`foreach` syntax one can write programs which reduce portions of the
streaming parse of a large input (reduce into proper JSON values, for
example), and discard the rest, processing incrementally.
This:
$ jq -c --stream .
should produce the same output as this:
$ jq -c '. as $dot | path(..) as $p | $dot | getpath($p) | [$p,.]'
The output of `jq --stream .` should be a sequence of`[[<path>],<leaf>]`
and `[[<path>]]` values. The latter indicate that the array/object at
that path ended.
Scalars and empty arrays and objects are leaf values for this purpose.
For example, a truncated input produces a path as soon as possible, then
later the error:
$ printf '[0,\n'|./jq -c --stream .
[[0],0]
parse error: Unfinished JSON term at EOF at line 3, column 0
$
2014-12-22 23:06:27 -06:00
|
|
|
ignoring parse error: Unfinished string at EOF at line 1, column 4
|
2014-10-10 22:19:38 -05:00
|
|
|
EOF
|
|
|
|
printf '"foo'|./jq -ce --seq . > $d/out 2>&1
|
|
|
|
cmp $d/out $d/expected
|
|
|
|
|
|
|
|
# Numeric values truncated by EOF are ignored
|
|
|
|
cat > $d/expected <<EOF
|
Add Streaming parser (--stream)
Streaming means that outputs are produced as soon as possible. With the
`foreach` syntax one can write programs which reduce portions of the
streaming parse of a large input (reduce into proper JSON values, for
example), and discard the rest, processing incrementally.
This:
$ jq -c --stream .
should produce the same output as this:
$ jq -c '. as $dot | path(..) as $p | $dot | getpath($p) | [$p,.]'
The output of `jq --stream .` should be a sequence of`[[<path>],<leaf>]`
and `[[<path>]]` values. The latter indicate that the array/object at
that path ended.
Scalars and empty arrays and objects are leaf values for this purpose.
For example, a truncated input produces a path as soon as possible, then
later the error:
$ printf '[0,\n'|./jq -c --stream .
[[0],0]
parse error: Unfinished JSON term at EOF at line 3, column 0
$
2014-12-22 23:06:27 -06:00
|
|
|
ignoring parse error: Potentially truncated top-level numeric value at EOF at line 1, column 1
|
2014-10-10 22:19:38 -05:00
|
|
|
EOF
|
|
|
|
printf '1'|./jq -ce --seq . > $d/out 2>&1
|
|
|
|
cmp $d/out $d/expected
|
|
|
|
|
|
|
|
cat > $d/expected <<EOF
|
|
|
|
EOF
|
|
|
|
printf '1\n'|./jq -ces --seq '. == [1]' >/dev/null 2> $d/out
|
|
|
|
cmp $d/out $d/expected
|
|
|
|
|
Add Streaming parser (--stream)
Streaming means that outputs are produced as soon as possible. With the
`foreach` syntax one can write programs which reduce portions of the
streaming parse of a large input (reduce into proper JSON values, for
example), and discard the rest, processing incrementally.
This:
$ jq -c --stream .
should produce the same output as this:
$ jq -c '. as $dot | path(..) as $p | $dot | getpath($p) | [$p,.]'
The output of `jq --stream .` should be a sequence of`[[<path>],<leaf>]`
and `[[<path>]]` values. The latter indicate that the array/object at
that path ended.
Scalars and empty arrays and objects are leaf values for this purpose.
For example, a truncated input produces a path as soon as possible, then
later the error:
$ printf '[0,\n'|./jq -c --stream .
[[0],0]
parse error: Unfinished JSON term at EOF at line 3, column 0
$
2014-12-22 23:06:27 -06:00
|
|
|
## Test streaming parser
|
|
|
|
|
|
|
|
$VALGRIND $Q ./jq -c '. as $d|path(..) as $p|$d|getpath($p)|scalars_or_empty|[$p,.]' < "$PWD/tests/torture/input0.json" > $d/out0
|
|
|
|
$VALGRIND $Q ./jq --stream -c '.|select(length==2)' < "$PWD/tests/torture/input0.json" > $d/out1
|
|
|
|
diff $d/out0 $d/out1
|
|
|
|
|
2014-07-27 17:33:22 -05:00
|
|
|
## Test library/module system
|
|
|
|
|
2014-08-20 20:48:48 -05:00
|
|
|
mods=$PWD/tests/modules
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if [ "`HOME="$mods" $VALGRIND $Q ./jq -nr fg`" != foobar ]; then
|
2014-07-13 19:00:37 -05:00
|
|
|
echo "Bug #479 appears to be back" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if [ `HOME="$mods" $VALGRIND $Q ./jq --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l` -gt 3 ]; then
|
2014-07-13 19:00:37 -05:00
|
|
|
echo "Binding too many defs into program" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if ! $VALGRIND $Q ./jq -ner -L "$mods" 'import a as foo; import b as bar; import a; def fooa: foo::a; [fooa, bar::a, bar::b, foo::a, a] | . == ["a","b","c","a","a"]' > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Module system appears to be broken" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if ! $VALGRIND $Q ./jq -ner -L "$mods" 'import c as foo; [foo::a, foo::c] | . == [0,"acmehbah"]' > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Module system appears to be broken" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if [ "`$VALGRIND $Q ./jq -cner -L "$mods" '\"c\" | modulemeta'`" != '{"whatever":null,"name":"c","deps":[{"as":"foo","name":"a"},{"search":"./","as":"d","name":"d"},{"search":"./","name":"d"},{"search":"./../lib/jq","as":"e","name":"e"},{"search":"./../lib/jq","as":"f","name":"f"}]}' ]; then
|
2014-08-10 16:52:03 -05:00
|
|
|
echo "modulemeta builtin appears to be broken" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if $VALGRIND ./jq -ner -L "$mods" 'import syntaxerror; .' > $d/out 2>&1; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Module system appears to be broken" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-07-27 17:41:40 -05:00
|
|
|
if [ -n "$VALGRIND" ] && ! grep 'ERROR SUMMARY: 0 errors from 0 contexts' $d/out > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Module system has memory errors when modules have syntax errors" 1>&2
|
|
|
|
cat $d/out
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-08-11 17:25:09 -05:00
|
|
|
if ! grep '^jq: error: syntax error,' $d/out > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Module system not detecting syntax errors in modules correctly" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if $VALGRIND ./jq -ner -L "$mods" '%::wat' > $d/out 2>&1 ||
|
2014-08-11 17:25:09 -05:00
|
|
|
! grep '^jq: error: syntax error,' $d/out > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Syntax errors not detected?" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-07-27 17:41:40 -05:00
|
|
|
if [ -n "$VALGRIND" ] && ! grep 'ERROR SUMMARY: 0 errors from 0 contexts' $d/out > /dev/null; then
|
2014-07-09 00:55:20 -04:00
|
|
|
echo "Memory errors when programs have syntax errors" 1>&2
|
|
|
|
cat $d/out
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-11-24 17:58:34 -06:00
|
|
|
|
2014-12-26 19:31:17 -06:00
|
|
|
if ! $VALGRIND ./jq -ner -L "$mods" -f "$mods/test_bind_order.jq" > $d/out 2>&1; then
|
2014-11-24 17:58:34 -06:00
|
|
|
echo "Import bind order is broken" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$VALGRIND" ] && ! grep 'ERROR SUMMARY: 0 errors from 0 contexts' $d/out > /dev/null; then
|
|
|
|
echo "Memory errors when programs have syntax errors" 1>&2
|
|
|
|
cat $d/out
|
|
|
|
exit 1
|
|
|
|
fi
|