#!/bin/sh

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

cat >our.schema.tcl <<EOF
ai {{field . {str} >0}}
fpr {{field . {bin} len=32}}
our {
    {field a {with ai}}
    {field v {bin str}}
    {field fpr {with fpr}}
    {field comment {str} optional}
}
EOF
$root/schema.tcl our.schema.tcl | xxd -r -p >our.schema.keks

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
    comment {STR привет}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "ok" "$SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "no comment" "$SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
    comment {STR ""}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "empty comment" "$SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 33]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "fpr longer" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 31]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "fpr shorter" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN ""}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "v empty" "$SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR ""}
    v {STR whatever}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "a empty" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {INT 123}
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "a bad type" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    a {STR some-ai}
    v {BIN [binary decode hex "DEADBEEF"]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "fpr no" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

$root/keks.tcl >data.keks.hex <<EOF
MAP {
    v {BIN [binary decode hex "DEADBEEF"]}
    fpr {BIN [binary decode hex [string repeat "AB" 32]]}
}
EOF
xxd -r -p <data.keks.hex >data.keks
test_expect_success "a no" "! $SCHEMA_VALIDATE our.schema.keks our <data.keks"

test_done
