# Tests for the zsh/db/gdbm module.
# This contains literal UTF-8 characters; if editing, use
# UTF-8 mode.

%prep

 modname="zsh/db/gdbm"
 dbfile=db.gdbm
 if ! zmodload $modname 2>/dev/null; then
   ZTST_unimplemented="can't load $modname module for testing"
 fi
 rm -f db.gdbm

%test

 (zmodload -u $modname && zmodload $modname)
0:unload and reload the module without crashing

 ztie -d db/gdbm -f $dbfile dbase
 zuntie dbase
0:create the database

 ztie -r -d db/gdbm -f $dbfile dbase
 zuntie -u dbase
0:open the database read-only

 ztie -d db/gdbm -f $dbfile dbase
 dbase[testkey]=testdata
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[testkey]
 zuntie -u dbase
0:store key in database
>testdata

 ztie -d db/gdbm -f $dbfile dbase2
 unset 'dbase2[testkey]'
 zuntie dbase2
 ztie -d db/gdbm -f $dbfile dbase
 echo $dbase[testkey]
 zuntie dbase
0:remove key from database (different variables)
>

 ztie -d db/gdbm -f $dbfile dbase
 dbase[testkey]=testdata
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[testkey]
 zuntie -u dbase
 ztie -d db/gdbm -f $dbfile dbase
 unset 'dbase[testkey]'
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[testkey]
 zuntie -u dbase
0:store & remove key from database (the same variables)
>testdata
>

 ztie -d db/gdbm -f $dbfile dbase
 dbase[testkey]=testdata
 dbase[testkey2]=$dbase[testkey]
 dbase[testkey3]=$dbase[testkey]x$dbase[testkey2]
 zuntie dbase
 ztie -d db/gdbm -f $dbfile dbase
 echo $dbase[testkey]
 echo $dbase[testkey2]
 echo $dbase[testkey3]
 zuntie dbase
0:store 2 keys fetching 1st
>testdata
>testdata
>testdataxtestdata

 ztie -d db/gdbm -f $dbfile dbase
 val=$dbase[testkey2]
 unset 'dbase[testkey2]'
 echo $val
 zuntie dbase
0:unset key that was fetched
>testdata

 ztie -r -d db/gdbm -f $dbfile dbase
 local -a result=( "${(kv)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie -u dbase
0:scan read-only tied hash, directly assign local -a
>testdata
>testdataxtestdata
>testkey
>testkey3

 ztie -d db/gdbm -f $dbfile dbase
 dbase=( a a )
 print -rl -- "${(kv)dbase[@]}"
 zuntie dbase
0:Use scan directly, read-write mode
>a
>a

 ztie -d db/gdbm -f $dbfile dbase
 dbase=( a b c d )
 zuntie dbase
 ztie -d db/gdbm -f $dbfile dbase
 result=( "${(kv)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie dbase
0:replace hash / database, scan
>a
>b
>c
>d

 ztie -d db/gdbm -f $dbfile dbase
 local -a arr
 arr=( "${dbase[@]}" )
 print -rl -- "${(o)arr[@]}"
 zuntie dbase
0:scan with no (kv)
>b
>d

 ztie -d db/gdbm -f $dbfile dbase
 result=( "${(k)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie dbase
0:scan with keys only (k)
>a
>c

 ztie -d db/gdbm -f $dbfile dbase
 result=( "${(v)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie dbase
0:scan with keys only explicit (v)
>b
>d

 rm -f $dbfile
 ztie -r -d db/gdbm -f $dbfile dbase 2>/dev/null
1:read-only open non-existent database

 ztie -d db/gdbm -f $dbfile dbase
 dbase+=( a b )
 echo $dbase[a]
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[a]
 result=( "${(kv)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie -u dbase
 ztie -d db/gdbm -f $dbfile dbase
 dbase+=( c d )
 echo $dbase[a]
 echo $dbase[c]
 result=( "${(kv)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[a]
 echo $dbase[c]
 result=( "${(kv)dbase[@]}" )
 print -rl -- "${(o)result[@]}"
 zuntie -u dbase
0:Append with +=( ), also with existing data, also (kv) scan
>b
>b
>a
>b
>b
>d
>a
>b
>c
>d
>b
>d
>a
>b
>c
>d

 ztie -d db/gdbm -f $dbfile dbase
 echo ${(t)dbase}
 zuntie dbase
0:Type of tied parameter
>association-special

 typeset -ga dbase
 ztie -d db/gdbm -f $dbfile dbase
 echo ${(t)dbase}
 zuntie dbase
0:Type of tied parameter, with preceding unset
>association-special

 local -a dbase
 ztie -d db/gdbm -f $dbfile dbase
 echo ${(t)dbase}
 zuntie dbase
0:Type of tied parameter, with local parameter already existing
>association-local-special

 local -a dbase
 dbase=( fromarray )
 () {
     local -a dbase
     ztie -d db/gdbm -f $dbfile dbase
     echo ${(t)dbase}
     zuntie dbase
 }
 echo $dbase[1]
 ztie -d db/gdbm -f $dbfile dbase2
 echo "Can connect, so untie happened:" $dbase2[a]
 zuntie dbase2
0:Test of automatic untie (use of local scope) and of scoping
>association-local-special
>fromarray
>Can connect, so untie happened: b

 echo $zgdbm_tied ${#zgdbm_tied}
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $zgdbm_tied ${#zgdbm_tied}
 ztie -d db/gdbm -f ${dbfile}2 dbase2
 echo $zgdbm_tied ${#zgdbm_tied}
 zuntie -u dbase
 echo $zgdbm_tied ${#zgdbm_tied}
 zuntie dbase2
 echo $zgdbm_tied ${#zgdbm_tied}
0:zgdbm_tied parameter
>0
>dbase 1
>dbase dbase2 2
>dbase2 1
>0

 unset zgdbm_tied 2>/dev/null
1:unset of read-only zgdbm_tied parameter

 ztie -d db/gdbm -f $dbfile dbase
 dbase[漢字]=漢字
 echo $dbase[漢字]
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[漢字]
 zuntie -u dbase
0:Unicode test
>漢字
>漢字

 key="ab"$'\0'"ef"
 ztie -d db/gdbm -f $dbfile dbase
 dbase[$key]=value
 echo $dbase[$key]
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 echo $dbase[$key]
 zuntie -u dbase
 ztie -d db/gdbm -f $dbfile dbase
 dbase[$key]=$key
 zuntie dbase
 ztie -d db/gdbm -f $dbfile dbase
 [[ "$dbase[$key]" = "$key" ]] && echo correct
 zuntie dbase
0:Metafication of $'\0'
>value
>value
>correct

 ztie -d db/gdbm -f $dbfile dbase
 dbase=( 漢字 漢字 )
 echo $dbase[漢字]
 zuntie dbase
 ztie -d db/gdbm -f $dbfile dbase
 echo $dbase[漢字]
 zuntie dbase
 key="ab"$'\0'"ef"
 ztie -d db/gdbm -f $dbfile dbase
 dbase+=( $key $key )
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 [[ "$dbase[$key]" = "$key" ]] && echo correct
 zuntie -u dbase
0:Unicode & metafication test, different hash access
>漢字
>漢字
>correct

 ztie -d db/gdbm -f $dbfile dbase
 dbase=( 漢字 漢字 )
 zuntie dbase
 ztie -d db/gdbm -f $dbfile dbase
 noglob print -rl ${(kv)dbase[@]}
 zuntie dbase
0:Hash scanning and metafication
>漢字
>漢字

 ztie -d db/gdbm -f $dbfile dbase
 noglob print -rl ${(okv)dbase[@]}
 zuntie dbase
0:Sorted hash scanning and metafication
>漢字
>漢字

 ztie -d db/gdbm -f $dbfile dbase
 zgdbmpath dbase
 [[ $REPLY = */Test/db.gdbm ]] && echo correct
 zuntie dbase
 ztie -r -d db/gdbm -f $dbfile dbase
 zgdbmpath dbase
 [[ $REPLY = */Test/db.gdbm ]] && echo correct
 zuntie -u dbase
0:zgdbmpath builtin
>correct
>correct

 ztie -d db/gdbm -f $dbfile dbase
 fun() { while read line; do echo $line; done }
 eval "dbase[testkey]=value1" | fun
 echo $dbase[testkey]
0:Test store in forked Zsh
>value1

%clean

  rm -f ${dbfile}*
