#!/bin/bash
# json.test
#
# Copyright (C) 2018-2024 Free Software Foundation, Inc.
#
# This program is free software, licensed under the terms of the GNU
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Reini Urban
[ -z "$DATADIR" ] && DATADIR="../../test/test-data"
top_builddir=".."
EXEEXT=""
TESTS_ENVIRONMENT=""
# artefact for "shellcheck"
[ -z "$top_builddir" ] && echo $top_builddir
i=0
DATA="sample_2000 example_2000 example_2004 example_2007 example_2010"
DATA="$DATA example_2013 example_2018 example_r14 example_r13"
DATA="$DATA r11/entities-3d r2.10/entities r2.6/entities r10/entities"
# eed numentities
TODO="r11/ACEB10 r1.4/entities"
SED="/usr/bin/sed"
JQ="jq"
GEOJSONLINT=""
[ -z "$SED" ] && echo "$SED"
[ -z "$JQ" ] && echo "$JQ"
[ -z "$GEOJSONLINT" ] && echo "$GEOJSONLINT"
for d in $TODO $DATA; do
b="$(basename "$d")"
rm "./$b.json" 2>/dev/null
done
if [ -n "$VALGRIND" ] && [ -n "$LOG_COMPILER" ]; then
TESTPROG="$LOG_COMPILER $LOG_FLAGS"
elif [ -n "" ]; then
TESTPROG=""
elif [ -n "$TESTS_ENVIRONMENT" ]; then
TESTPROG="$TESTS_ENVIRONMENT ${top_builddir}/libtool --mode=execute"
else
TESTPROG="${top_builddir}/libtool --mode=execute"
fi
check_roundtrip() {
if [ -f "$dwg" ] && [ -x dwgwrite ]
then
json="$(basename "$dwg" .dwg).json"
tgt="$(basename "$dwg")"
log1="$(basename "$dwg" .dwg).log.orig"
log2="$(basename "$dwg" .dwg).log"
todo=
case $dwg in
*r11/A*) todo=1 ;;
*r1.*) todo=1 ;;
esac
echo "./dwgread${EXEEXT} -y -v2 -o $json $dwg 2>$log1"
if ! $TESTPROG "./dwgread${EXEEXT}" -y -v2 -o "$json" "$dwg" 2>"$log1" >/dev/null; then
echo "json $dwg failed"
tail "$log1"
if [ -z $todo ]; then i=$((i+1)); fi
fi
echo "./dwgwrite${EXEEXT} -y -v0 -o $tgt $json"
if ! $TESTPROG "./dwgwrite${EXEEXT}" -y -v0 -o "$tgt" "$json" 2>/dev/null >/dev/null; then
echo "dwgwrite $json failed"
if [ -z $todo ]; then i=$((i+1)); fi
fi
echo "./dwgread${EXEEXT} -v2 $tgt 2>$log2"
if ! $TESTPROG "./dwgread${EXEEXT}" -v2 "$tgt" 2>"$log2" >/dev/null; then
echo "dwgread resulting $tgt failed"
tail "$log2"
if [ -z $todo ]; then i=$((i+1)); fi
fi
expect1="$(grep -E -c '^Add entity' "$log1")"
got1="$(grep -E -c '^Add entity' "$log2")"
expect2="$(grep -E -c '^Add object' "$log1")"
got2="$(grep -E -c '^Add object' "$log2")"
rm "./$json" "./$tgt" 2>/dev/null
if [ "$got1" -eq "$expect1" ] && [ "$got2" -ge "$expect2" ]
then
echo "$json roundtrip ok"
rm "./$log1" "./$log2" 2>/dev/null
else
echo "$json" "roundtrip fail: $expect1 vs $got1 entities, $expect2 vs $got2 objects"
if [ -z $todo ]; then
i=$((i+1))
else
rm "./$log1" "./$log2" 2>/dev/null
fi
echo "expect entities: $expect1" >"$log"
# shellcheck disable=SC2129
echo "got entities: $got1" >>"$log"
echo "expect objects: $expect2" >>"$log"
echo "got objects: $got2" >>"$log"
fi
fi
}
for dwg in $DATA $TODO; do
json="$(basename "$dwg" .dwg).json"
log="$json.log"
dwg="${DATADIR}/$dwg.dwg"
echo "dwgread${EXEEXT}" -Ojson -o"$json" "$dwg"
# With --enable-debug fixup nan
if $TESTPROG "./dwgread${EXEEXT}" -Ojson -o"$json" "$dwg" 2>/dev/null && \
true "$json" && \
$JQ . "$json" >"$json.log" 2>&1
then
rm "./$json.log" "./$json"
else
cat "$json.log"
i=$((i+1))
fi
geojson="$(basename "$dwg" .dwg).geojson"
echo "./dwgread${EXEEXT}" -o"$geojson" "$dwg"
if $TESTPROG "./dwgread${EXEEXT}" -Ogeojson -o"$geojson" "$dwg" 2>/dev/null && \
true "$geojson" && \
$JQ . "$geojson" >>"$json.log" 2>&1
then
if [ -n "$GEOJSONLINT" ]; then
$GEOJSONLINT "$geojson" ||
i=$((i+1))
fi
rm "./$json.log" "./$geojson"
else
cat "$json.log"
i=$((i+1))
fi
check_roundtrip
done
# teigha supports r9-r2018. r2.10: Unsupported early .dwg file version
can_teigha=1
case $(uname) in
Darwin) TeighaFileConverter=/Applications/ODAFileConverter.app/Contents/MacOS/ODAFileConverter ;;
Linux) TeighaFileConverter=/usr/bin/ODAFileConverter ;;
Windows) TeighaFileConverter=ODAFileConverter ;;
*) can_teigha=0 ;;
esac
if [ -n "$can_teigha" ] && [ ! -x "$TeighaFileConverter" ]; then
case $(uname) in
Darwin) TeighaFileConverter=/Applications/TeighaFileConverter.app/Contents/MacOS/TeighaFileConverter ;;
Linux) TeighaFileConverter=/usr/bin/TeighaFileConverter ;;
Windows) TeighaFileConverter=TeighaFileConverter ;;
esac
fi
check_oda() {
if [ -x "$TeighaFileConverter" ]
then
b="$(basename "$dwg")"
dir="$(dirname "$dwg")"
r="$(basename "$dir")"
can_teigha=1
case $r in
20*) b="${b}_${r}" ;;
r9) r=9; b="${b}_r${r}" ;;
r11) r=12; b="${b}_r${r}" ;;
r1[0234]) r=${r:1}; b="${b}_r${r}" ;;
r[12]\.*) r=${r:1}
can_teigha=
;;
.) case $b in
*_20*) r="${b:(-4)}" ;;
*_r9) r=9 ;;
*_r11) r=12 ;;
*_r1[0234]) r="${b:(-2)}" ;;
esac
;;
esac
if [ -n "$can_teigha" ] && [ ! -f "$b.dwg" ]; then
if [ ! -f "$b.json" ]; then
echo "dwgread${EXEEXT}" -o"$b.json" "${DATADIR}/$dwg.dwg"
$TESTPROG "./dwgread${EXEEXT}" -o"$b.json" "${DATADIR}/$dwg.dwg" 2>/dev/null
fi
echo "./dwgwrite${EXEEXT} -y -v0 -o $b.dwg $b.json"
$TESTPROG "./dwgwrite${EXEEXT}" -y -v0 -o "$b.dwg" "$b.json" 2>/dev/null
fi
if [ -n "$can_teigha" ] && [ -f "$b.dwg" ]; then
echo TeighaFileConverter . .. "ACAD$r" DXF 0 1 "$b.dwg" from "$dwg"
$TeighaFileConverter . .. "ACAD$r" DXF 0 1 "$b.dwg" >/dev/null 2>/dev/null
if [ -e "../$b.dxf.err" ]; then
mv "../$b.dxf.err" "$b.json.err"
cat "$b.json.err"
i=$((i+1))
fi
rm -f "../$b.dxf"
else
rm -f "./$b.json.err" 2>/dev/null
fi
fi
}
if [ -n "$can_teigha" ] && [ -x "$TeighaFileConverter" ]
then
echo ""
echo TeighaFileConverter roundtrip checks
# example_r14: CRC does not match
for dwg in sample_2000 example_2000 2000/Arc 2000/Text 2000/circle 2000/PolyLine2D 2000/RAY\
2000/Spline r11/entities-3d r11/entities-2d
do
check_oda
done
else
echo "(Teigha|ODA)FileConverter not installed"
fi
if [ -n "$JQ" ]
then
# check pipe (nocomma)
dwg="$DATADIR/example_2000.dwg"
json="example_2000.json"
echo "dwgread -O geojson $DATADIR/example_2000.dwg | jq ."
if $TESTPROG "./dwgread${EXEEXT}" -O geojson "$DATADIR/example_2000.dwg" | $JQ . >"$json.log" 2>&1
then
echo pipe to geojson ok
rm "./$json.log"
else
cat "$json.log"
i=$((i+1))
fi
echo "dwgread -O json $DATADIR/example_2000.dwg | jq ."
if $TESTPROG "./dwgread${EXEEXT}" -O json "$DATADIR/example_2000.dwg" | $JQ . >"$json.log" 2>&1
then
echo pipe to json ok
rm "./$json.log"
else
cat "$json.log"
i=$((i+1))
fi
else
echo no jq, cannot check pipe
fi
if test "0" = "$i" ; then
exit 0
else
echo "$(basename "$0"): $i failures"
ls -l ./*json.log
exit 1
fi