1#!/bin/sh
2# json.test
3#
4# Copyright (C) 2018-2020 Free Software Foundation, Inc.
5#
6# This program is free software, licensed under the terms of the GNU
7# General Public License as published by the Free Software Foundation,
8# either version 3 of the License, or (at your option) any later version.
9# You should have received a copy of the GNU General Public License
10# along with this program.  If not, see <http://www.gnu.org/licenses/>.
11#
12# Author: Reini Urban
13
14[ -z "$DATADIR" ] && DATADIR="../../test/test-data"
15top_builddir=".."
16EXEEXT=""
17# artefact for "shellcheck"
18[ -z "$top_builddir" ] && echo $top_builddir
19
20i=0
21DATA="sample_2000 example_2000 example_2004 example_2007 example_2010"
22DATA="$DATA example_2013 example_2018 example_r14"
23#TODO="r11/ACEL10"
24SED=/usr/bin/sed
25JQ=jq
26GEOJSONLINT=geojsonhint
27[ -z "$SED" ] && echo "$SED"
28[ -z "$JQ" ] && echo "$JQ"
29[ -z "$GEOJSONLINT" ] && echo "$GEOJSONLINT"
30
31for d in $DATA; do
32    b="$(basename "$d")"
33    rm "./$b.json" 2>/dev/null
34done
35
36check_roundtrip() {
37    if [ -f "$dwg" ] && [ -x dwgwrite ]
38    then
39        json="$(basename "$dwg" .dwg).json"
40        tgt="$(basename "$dwg")"
41        log1="$(basename "$dwg" .dwg).log.orig"
42        log2="$(basename "$dwg" .dwg).log"
43        ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -y -v2 -o "$json" "$dwg" 2>"$log1" >/dev/null
44        ${top_builddir}/libtool --mode=execute ./dwgwrite${EXEEXT} -y -v0 -o "$tgt" "$json" 2>/dev/null >/dev/null
45        ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -v2 "$tgt" 2>"$log2" >/dev/null
46        expect1="$(grep -E -c '^Add entity' "$log1")"
47        got1="$(grep -E -c '^Add entity' "$log2")"
48        expect2="$(grep -E -c '^Add object' "$log1")"
49        got2="$(grep -E -c '^Add object' "$log2")"
50        rm "./$json" "./$tgt" 2>/dev/null
51        if [ "$got1" -eq "$expect1" ] && [ "$got2" -ge "$expect2" ]
52        then
53            echo "$json roundtrip ok"
54            rm "./$log1" "./$log2" 2>/dev/null
55        else
56            echo "$json" "roundtrip fail: $expect1 vs $got1 entities, $expect2 vs $got2 objects"
57            i=$((i+1))
58            echo "expect entities: $expect1" >"$log"
59            # shellcheck disable=SC2129
60            echo "got    entities: $got1"    >>"$log"
61            echo "expect objects:  $expect2" >>"$log"
62            echo "got    objects:  $got2"    >>"$log"
63        fi
64    fi
65}
66
67for dwg in $DATA; do
68    json="$(basename "$dwg" .dwg).json"
69    log="$json.log"
70    dwg="${DATADIR}/$dwg.dwg"
71    echo dwgread${EXEEXT} -Ojson -o"$json" "$dwg"
72    # With --enable-debug fixup nan
73    if ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -Ojson -o"$json" "$dwg" 2>/dev/null && \
74       true "$json" && \
75       $JQ . "$json" >"$json.log" 2>&1
76    then
77        rm "./$json.log" "./$json"
78    else
79        cat "$json.log"
80        i=$((i+1))
81    fi
82
83    geojson="$(basename "$dwg" .dwg).geojson"
84    echo ./dwgread${EXEEXT} -o"$geojson" "$dwg"
85    if ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -Ogeojson -o"$geojson" "$dwg" 2>/dev/null && \
86       true "$geojson" && \
87       $JQ . "$geojson" >>"$json.log" 2>&1
88    then
89        if [ -n "$GEOJSONLINT" ]; then
90            $GEOJSONLINT "$geojson" ||
91                i=$((i+1))
92        fi
93        rm "./$json.log" "./$geojson"
94    else
95        cat "$json.log"
96        i=$((i+1))
97    fi
98
99    check_roundtrip
100done
101
102if [ -n "$JQ" ]
103then
104    # check pipe (nocomma)
105    dwg="$DATADIR/example_2000.dwg"
106    json="example_2000.json"
107    echo "dwgread -O geojson $DATADIR/example_2000.dwg | jq ."
108    if ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -O geojson "$DATADIR/example_2000.dwg" | $JQ . >"$json.log" 2>&1
109    then
110        echo pipe to geojson ok
111        rm "./$json.log"
112    else
113        cat "$json.log"
114        i=$((i+1))
115    fi
116
117    echo "dwgread -O json $DATADIR/example_2000.dwg | jq ."
118    if ${top_builddir}/libtool --mode=execute ./dwgread${EXEEXT} -O json "$DATADIR/example_2000.dwg" | $JQ . >"$json.log" 2>&1
119    then
120        echo pipe to json ok
121        rm "./$json.log"
122    else
123        cat "$json.log"
124        i=$((i+1))
125    fi
126else
127    echo no jq, cannot check pipe
128fi
129
130if test "0" = "$i" ; then
131    exit 0
132else
133    echo "$(basename "$0"): $i failures"
134    ls -l ./*json.log
135    exit 1
136fi
137