#!/bin/sh

test_description="Check \"wheres\" specs/ example"
. $SHARNESS_TEST_SRCDIR/sharness.sh
root=$(realpath ../..)
SCHEMA_VALIDATE=${SCHEMA_VALIDATE:-schema-validate}
export TCLLIBPATH=$root

cat >wheres.schema.tcl <<EOF
latitude {{field . {int} >-91 <91}}
longitude {{field . {int} >-181 <181}}
where {
    {field . {list} len=2}
    {field 0 {with latitude}}
    {field 1 {with longitude}}
}
wheres {{field . {list} {of where} >0}}
EOF
$root/schema.tcl wheres.schema.tcl | xxd -r -p >wheres.schema.keks

$root/keks.tcl >data.keks.hex <<EOF
LIST {}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "wheres empty" "! $SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {{LIST {}}}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "where empty" "! $SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {{LIST {{INT 45} {INT 180}}}}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "ok" "$SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {{LIST {{INT -45} {INT 180}}}}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "lat negative" "$SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {{LIST {{INT 45} {INT -180}}}}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "lon negative" "$SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {
    {LIST {{INT 45} {INT 180}}}
    {LIST {{INT -91} {INT 180}}}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "oob lat" "! $SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {
    {LIST {{INT 45} {INT 180}}}
    {LIST {{INT 90} {INT -181}}}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "oob lon" "! $SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
LIST {
    {LIST {{INT 45} {INT 180}}}
    {LIST {{STR 0} {INT 123}}}
    {LIST {{INT 12}}}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "lon bad len" "! $SCHEMA_VALIDATE wheres.schema.keks wheres <data.keks"

test_done
