Sisyphus repository
Last update: 1 october 2023 | SRPMs: 18631 | Visits: 37837286
en ru br
ALT Linux repos
S:0.13-alt4
5.0: 0.11-alt1
4.1: 0.10-alt4.M41.1
4.0: 0.1-alt3

Group :: System/Base
RPM: alterator-sh-functions

 Main   Changelog   Spec   Patches   Sources   Download   Gear   Bugs and FR  Repocop 

alterator-sh-functions-0.13/000075500000000000000000000000001150610253600160455ustar00rootroot00000000000000alterator-sh-functions-0.13/Makefile000064400000000000000000000004041150610253600175030ustar00rootroot00000000000000TARGET=alterator-sh-functions alterator-unittest-functions
MAN_PAGES=alterator-sh-functions.3

install:
for i in $(TARGET); do install -Dpm644 $$i $(bindir)/$$i; done
for i in $(MAN_PAGES); do install -Dpm644 $$i $(mandir)/man3/$$i; done

check:
./runtests
alterator-sh-functions-0.13/alterator-sh-functions000064400000000000000000000137711150610253600224140ustar00rootroot00000000000000#!/bin/sh -efu

if [ -z "${__included_alterator_sh_functions:-}" ]; then
__included_alterator_sh_functions=1

alterator_api_version="${alterator_api_version:-0}"

. shell-error
. shell-quote
. shell-var

exec 3>&1

### internal functions
_write_begin()
{
printf '(\n' >&3
}

_write_end()
{
printf ')\n' >&3
}

_validate_symbol()
{
[ -n "${1##*[!0-9A-Za-z_]*}" ] || fatal "wrong attribute name:$1"
}

### quote
string_quote()
{
sed 's,[\"],\\&,g' -- "$@"
}

newline_unquote()
{
if [ "$1" != "_objects" ]; then
local separator=$(printf '\007')
sed -e "s,\\\\\(.\),$separator\1,g" \
-e "s,${separator}n,\n,g" \
-e "s,$separator,,g"
else
cat
fi
}

### RPC like API

_alterator_export_var_list=

alterator_export_var()
{
while [ "$#" -gt 0 ];do
[ "$#" != 1 ] || break
_alterator_export_var_list="$_alterator_export_var_list $1 $2"
shift
shift
done
}

_alterator_print_export_var_list()
{
local IFS=' '
set -- $_alterator_export_var_list
while [ $# -gt 0 ];do
write_type_item "$1" "$2"
shift
shift
done
}


_alterator_export_proc_list=" "

alterator_export_proc()
{
_alterator_export_proc_list="$_alterator_export_proc_list$1 "
}

### main message loop

_userhandler() { # trick to protect loop values
local l= readmsg= params=
local IFS="$IFS" PATH="$PATH" # Protected variables

[ "$alterator_api_version" -le 0 ] || _write_begin

# process exported types
[ "$in_action" != "type" ] || _alterator_print_export_var_list

# process exported procedures
local proc="${1:-}"
[ -n "${_alterator_export_proc_list##* $in__objects *}" ] ||
[ "$in_action" = "type" ] ||
proc="$in__objects"
"$proc" || : # ignore exit code of user actions

[ "$alterator_api_version" -le 0 ] || _write_end
}


# default handler
on_message()
{
true
}

message_loop() {
local l= readmsg= params= name= value=
[ "$alterator_api_version" -le 0 ] || exec 1>&2

while IFS= read -r l; do
if [ "$l" != "${l#_message:begin}" ]; then
[ -n "$params" ] && unset $params && params= ||:
readmsg=1
continue
fi
if [ -n "$readmsg" -a "$l" != "${l#_message:end}" ]; then
_userhandler "${1:-on_message}"
readmsg=
fi
[ -n "$readmsg" ] || continue
name="$(printf %s\\n "${l%%:*}" | sed -e 's,[^[:alnum:]_],,g')" &&
value="$(printf %s\\n "${l#*:}" | newline_unquote "$name")" ||
continue
eval "in_$name=\"$(quote_shell "$value")\"" && params="$params in_$name" && name= value= ||:
done
}

### input/output functions

write_string()
{
local out="$*"
if [ -z "${out##*[\"\\\\]*}" ]; then
out="$(printf %s "$out" |string_quote)" ||
return 1
fi
printf %s "$out"
}

write_bool()
{
shell_var_is_yes "$1" && echo "#t" || echo "#f"
}


test_bool()
{
[ "$1" = "#t" ]
}


### i18n support

write_language()
{
local charset="${po_charset:-UTF-8}"
echo "$1" |
sed -r \
-e "s,([^;]+)(;|$),\1.$charset\2,g" \
-e 's,;,:,g'
}

set_locale(){
local charset="${po_charset:-UTF-8}"
local langlist="$(echo "$in_language" |\
sed -r \
-e "s,([^;]+)(;|$),\1.$charset\2,g" \
-e 's,;,:,g')"
local firstlang="${langlist%%:*}"
export LC_ALL="$firstlang"
export LANGUAGE="$langlist"
}

run_localized(){
local charset="${po_charset:-UTF-8}"
local langlist="$(echo "$in_language" |\
sed -r \
-e "s,([^;]+)(;|$),\1.$charset\2,g" \
-e 's,;,:,g')"
local firstlang="${langlist%%:*}"
local cmd="$1"; shift
LC_ALL="$firstlang" LANGUAGE="$langlist" "$cmd" "$@"
}

#Note: gettext uses encoding from lc_ctype and translation from language
po_domain="${po_domain:-alterator-${0##*/}}"

_()
{
local domain="${2:-$po_domain}"
run_localized gettext "$domain" "$1"
}



### high level output

write_string_param()
{
_validate_symbol "$1"
printf '%s "%s"\n' "$1" "$(write_string "$2")" >&3
}

write_bool_param()
{
_validate_symbol "$1"
printf '%s %s\n' "$1" "$(write_bool "$2")" >&3
}

write_blob_param()
{
local name="$1";shift
_validate_symbol "$name"
local value="$(base64 -w0 -- "$@"|string_quote)"
printf '%s "%s"\n' "$name" "$value" >&3
}

write_enum_item()
{
if [ $# -eq 0 ];then
echo "WARNING: write_enum_item for stream is deprecated, use write_enum instead" >&2
string_quote|
while read -r name label;do
[ -n "$name" ] || continue
printf '(name "%s" label "%s")\n' \
"$name" \
"${label:-$name}"
done
else
printf '(name "%s" label "%s")\n' \
"$(write_string "$1")" \
"$(write_string "${2:-$1}")"
fi >&3
}

write_enum()
{
string_quote|
while read -r name label;do
[ -n "$name" ] || continue
printf '(name "%s" label "%s")\n' \
"$name" \
"${label:-$name}"
done >&3
}

write_table_item()
{
[ $# -gt 0 -a "$(($# % 2))" = "0" ] || fatal "wrong number of arguments"

printf '(' >&3
while [ $# -gt 0 ]; do
local name="$1";shift
local value="$1";shift
_validate_symbol "$name"
case "$value" in
'#t'|'#f')
printf ' %s %s' "$name" "$(write_string "$value")" >&3
;;
*)
printf ' %s "%s"' "$name" "$(write_string "$value")" >&3
;;
esac
done
printf ')' >&3
}

write_type_item()
{
write_string_param "$1" "$2"
}

write_error()
{
[ "$alterator_api_version" -gt 0 ] || _write_begin
write_string_param 'error' "$1"
[ "$alterator_api_version" -gt 0 ] || _write_end
}

write_nop()
{
[ "$alterator_api_version" -gt 0 ] || _write_begin
[ "$alterator_api_version" -gt 0 ] || _write_end
}

write_debug()
{
[ -z "${ALTERATOR_DEBUG:-}" ] || printf "$@" >&2
}

### helpers

file_list_add()
{
local file="$1";shift
local line="$1";shift

fgrep -xqs "$line" "$file" ||
printf '%s\n' "$line" >>"$file"
}

file_list_del()
{
local file="$1";shift
local line
quote_sed_regexp_variable line "$1"

sed "/^$line$/d" -i "$file"
}


### backward compatibility: api_version < 0
alias simple_quote=string_quote


fi #__included_alterator_sh_functions
alterator-sh-functions-0.13/alterator-sh-functions.3000064400000000000000000000144141150610253600225500ustar00rootroot00000000000000.TH alterator-sh-functions 3 "7/04/2009" "ALT Linux" "Alterator user manual"

.SH NAME
alterator-sh-functions - library to write alterator backends in shell.

.SH SYNOPSIS

.BI "message_loop [message-handler]"
.sp
.BI "on_message"
.sp
.BI "_ <string> <domain>"
.sp
.BR "test_bool <value>"
.sp
.BI "write_string_param <name> <value>"
.sp
.BI "write_bool_param <name> <value>
.sp
.BI "write_blob_param <name> [file]
.sp
.BI "write_enum_item <name> [<label>]"
.sp
.BI "write_enum"
.sp
.BI "write_table_item <name1> <value1> <name2> <value2> ..."
.sp
.BI "write_type_item <name> <type>"
.sp
.BI "write_error <message>"
.sp
.BI "write_debug <message>"
.sp
.SH DESCRIPTION

Alterator backends use a very simple communication protocol and can be
written in any language. This library contains a set of functions to
simplify development of backend on shell.

Main event loop is provided by \fImessage_loop\fR function. Default
callback is a function called \fIon_message\fR. All incoming parameters
are provided as a global variables with prefix \fI'in_'\fR. For
example, incoming parameter \fI'foo'\fR can be found as variable
\fI'$in_foo'\fR.

You should write output parameters using functions with prefix
\fI'write_'\fR. Some parameter names have special meaning. There are:
\fIaction\fR, \fI_objects\fR, \fIlanguage\fR,
\fIexpert_mode\fR.

Usually callback determine action using \fI'action'\fR parameter. Some
action types have special meaning. There are: \fItype\fR, \fIhelp\fR.

Behaviour of the library significantly depends on the
\fIalterator_api_version\fR global variable. It is unset by default for
backward compatibility. Current API version value is \fI'1'\fR, you must
explicitely set \fIalterator_api_version=1\fR to use current API.

\fBAttention:\fR When \fIalterator_api_version < 1\fR, \fIstdout\fR and \fIstdin\fR channels
are used for communications between backend and \fBalteratord\fR daemon.
Be shure not to write something else to \fIstdout\fR or read from
\fIstdin\fR. When \fIalterator_api_version = 1\fR, backend's
\fIstdout\fR is redirected to \fIstderr\fR, so you can safely write to
it.

.SS Localization
Sometimes it is useful to have answer from backend in user native language.
List of preffered languages, separated by semicolon you can find in \fIlanguage\fR parameter.
We recommend you to use library function \fI_\fR to translate messages. This is a wrapper over
\fBgettext\fR utility. Default value of dictionary with translated
messages for \fI_\fR function is a \fI'alterator-<backend-name>'\fR, you can change this
value by using \fIpo_domain\fR global variable.
.SS Type checking
It's very useful to validate parameters before use in backend.
You can define parameter types in answer to special action called \fI'type'\fR.
All parameters with defined types will be checked in \fBalteratord\fR daemon
before passing request to backend.
.SH EXAMPLE
.sp
alterator_api_version=1
.br
po_domain=module-dictionary
.sp
. alterator-sh-functions
.sp
on_message()
.br
{
.br
case "$in_action" in
.br
type)
.br
write_type_item param1 hostname
.br
;;
.br
read)
.br
write_string_param param1 value
.br
write_string_param param2 "`_ localized value`"
.br
;;
.br
write)
.br
write_debug "param=$in_param"
.br
;;
.br
list)
.br
write_enum_item "param1" "First parameter"
.br
write_enum_item "param2" "Second parameter"
.br
;;
.br
esac
.br
}
.sp
message_loop
.SH FUNCTIONS
.SS Common functions
.TP
\fImessage_loop\fR
Main event loop. Default callback is \fI'on_message'\fR, you can specify own callback name.
.TP
\fIon_message\fR
Default event callback. This function is without parameters.
All incoming parameters are coming as global variables.
\fI_ <string> [<domain>]\fR
Output translated string. Optional parameter
\fI<domain>\fR allows you to replace a default dictionary with your one.
.TP
\fIwrite_debug <string>\fR
Use this function to debug your backend. This function will print your messages only if
global variable \fBALTERATOR_DEBUG\fR is not empty.
.SS Functions for processing of incoming parameters
.TP
\fItest_bool <value>\fR
Incoming boolean variable are represented as a string with value \fI'#t'\fR or \fI'#f'\fR.
This representation can be changed in the future and we are strongly
recommend you to use \fItest_bool\fR function to test variable for \fItrue\fR value.
.SS Functions for writing output parameters.
.TP
\fIwrite_string_param <name> <value>\fR
Write parameter of string type with name \fI<name>\fR and value \fI<value>\fR.
.TP
\fIwrite_bool_param <name> <value>\fR
Write parameter of boolean type with name \fI<name>\fR and value \fI<value>\fR.
Strings \fI'1'\fR, \fI'true'\fR, \fI'yes'\fR, \fI'y'\fR, and \fI'on'\fR are treated as a \fItrue\fR value,
all other strings a treated as a \fIfalse\fR value.
.TP
\fIwrite_blob_param <name> [<file>]\fR
Write parameter with arbitrary binary data from some file or standard input.
.TP
\fIwrite_enum_item <name> [<label>]\fR
Write member of enumeration. Optional parameter \fI<label>\fR is a descriptive member name visible for users.
.TP
\fIwrite_enum\fR
Read stdin and print enumeration members. Each line is of the format:
.sp
<name><separator><label>
.sp
Second field is optional. Field separator is a space character or tab. If you want to use other field separator in input stream, then you
should set IFS variable to appropriate value.
.TP
\fIwrite_table_item <name1> <value1> <name2> <value2> ...\fR
Sometimes we need to fill tables or multicolumn lists in interface.
This function will print attributes and it's values for one line of a such table.
Please note that all attributes should be of a type "string".
.TP
\fIwrite_error\fR
This is a function to notify about problems in backend. This function should work like "throw".
You must return from callback immediately after call of this function.
.TP
\fIwrite_type_item\fR
Write a parameter definition.
All parameters are treated as a strings by default.
If you want to have an additional check for values before use, then you
should use this function to define a type of parameter. There are a lot of standard types, e.g.,
\fIipv-address\fR, \fItcp-port\fR, \fIhostname\fR etc. You can also create your own types.
.SH FILES
.TP
\fI/usr/lib/alterator/backend3\fR - directory containing alterator backends.
.SH SEE ALSO
.BR alteratord(8)
.SH AUTHOR

Stanislav Ievlev <inger@altlinux.org>
alterator-sh-functions-0.13/alterator-unittest-functions000064400000000000000000000027721150610253600236600ustar00rootroot00000000000000#!/bin/sh -efu

. shell-error

if [ -z "${__included_alterator_unittest_functions:-}" ]; then
__included_alterator_unittest_functions=1

ALTERATOR_QUERY="${ALTERATOR_QUERY:-alterator-cmdline -l}"

assertEnumref()
{
local msg="$1";shift
local arglist="$@"
local rc=0
$ALTERATOR_QUERY "$@" action list|
awk 'BEGIN {FS=":"; hasrecord=0; }
function reset() { for (i in params) delete params[i]; }
function check() { if (params["name"] == "" || params["label"] == "") exit(1); }
/^$/ { if (hasrecord) check(); reset(); next; }
{ params[$1]=$2; hasrecord=1}
END {if (hasrecord) check(); }' || rc=1
assertEquals "$msg : enumref($arglist)" "$rc" "0"
}

assertRead()
{
local msg="$1";shift
local param="$1";shift
local expected="$1";shift

local arglist="$@"

local v="$($ALTERATOR_QUERY -k "$param" "$@" action read)" || fatal "backend execution failed"

assertEquals "$msg: read($arglist) : \"$param\" : expected \"$expected\", got \"$v\"" "$v" "$expected"
}

assertList()
{
local msg="$1";shift
local param="$1";shift
local expected="$1";shift

local arglist="$@"

local v="$($ALTERATOR_QUERY -k "$param" "$@" action list)" || fatal "backend execution failed"

assertEquals "$msg: list($arglist) : \"$param\" :
expected: \"$expected\"
got: \"$v\"" "$v" "$expected"
}

showSummary() {
printf 'Completed: %s passes, %s failures, %s total\n' \
"$passed" \
"$failed" \
"$total"
}

fi #__included_alterator_unitest_functions
alterator-sh-functions-0.13/runtests000075500000000000000000000023401150610253600176610ustar00rootroot00000000000000#!/bin/sh -e

. shell-unittest
. ./alterator-sh-functions

WORKDIR=
setUp() {
WORKDIR="$(mktemp -td "alterator-sh-functions.XXXXXXXXX")"
cd "$WORKDIR"
}

tearDown() {
rm -rf -- "$WORKDIR"
}

### common testcases

test_for_symbol() {
local assert="$1";shift

#this names can break scheme
"$assert" ''
"$assert" '#name'
"$assert" 'n"ame'
"$assert" "n'ame"
"$assert" 'n(ame'
"$assert" 'n)ame'
"$assert" 'n;ame'
"$assert" ':name'
"$assert" '#("name")'
"$assert" 'n ame'
"$assert" 'n ame'
"$assert" 'n
ame'
#this names can break shell
"$assert" 'n-ame'
"$assert" 'n!ame'
"$assert" 'n|ame'
"$assert" 'n{ame'
"$assert" 'n%ame'
"$assert" 'n,ame'
"$assert" '$name'
"$assert" '`name'
"$assert" 'имя'
}

test_for_string()
{
local assert="$1";shift

"$assert" 'abcd' 'abcd'
"$assert" 'abc\\d' 'abc\d'
"$assert" 'ab\\c\\d' 'ab\c\d'
"$assert" 'abc\"d' 'abc"d'
"$assert" 'ab\"c\"d' 'ab"c"d'
"$assert" 'abc\\\"d' 'abc\"d'
}

test_for_string_extra()
{
local assert="$1";shift

local input="ab
cd"

test_for_string "$assert"
"$assert" '' ''
"$assert" "$input" "$input"
}

for i in test/*; do . $i;done

runUnitTests
alterator-sh-functions-0.13/test/000075500000000000000000000000001150610253600170245ustar00rootroot00000000000000alterator-sh-functions-0.13/test/api_export000064400000000000000000000042651150610253600211300ustar00rootroot00000000000000appendTests \
test_export_var \
test_export_proc

export_var_handler()
{
case "$in_action" in
type)
write_type_item e typee
;;
esac
}

test_export_var()
{
local _alterator_export_var_list=
local alterator_api_version=1

alterator_export_var a typea
alterator_export_var b typeb ignore
alterator_export_var c typec d typed

local input1='
_message:begin
action:type
_message:end'

local output1='(
a "typea"
b "typeb"
c "typec"
d "typed"
e "typee"
)'

local input2='
_message:begin
action:other
_message:end'

local output2='(
)'

assertEquals "type request" "$output1" "$(echo "$input1"|message_loop export_var_handler 3>&1)"
assertEquals "no type request" "$output2" "$(echo "$input2"|message_loop export_var_handler 3>&1)"
}

export_proc1()
{
write_string_param proc1 value1
}

export_proc2()
{
write_string_param proc2 value2
}

export_proc_handler()
{
write_string_param proc value
}

test_export_proc()
{
local _alterator_export_proc_list=" "
local alterator_api_version=1

alterator_export_proc export_proc1
alterator_export_proc export_proc2
alterator_export_proc export_proc3

local input1='
_message:begin
_message:end'

local output1='(
proc "value"
)'

local input2='
_message:begin
_objects:export_proc1
_message:end'

local output2='(
proc1 "value1"
)'

local input3='
_message:begin
_objects:export_proc2
_message:end'

local output3='(
proc2 "value2"
)'

local input4='
_message:begin
_objects:export_proc3
_message:end'

local output4='(
)'

local input5='
_message:begin
_objects:export_proc4
_message:end'

local output5='(
proc "value"
)'
assertEquals "no call proc" "$output1" "$(echo "$input1"|message_loop export_proc_handler 3>&1)"
assertEquals "call proc1" "$output2" "$(echo "$input2"|message_loop export_proc_handler 3>&1)"
assertEquals "call proc2" "$output3" "$(echo "$input3"|message_loop export_proc_handler 3>&1)"
assertEquals "call unexistent proc3" "$output4" "$(echo "$input4"|message_loop export_proc_handler 3>&1)"
assertEquals "call unexported proc4" "$output5" "$(echo "$input5"|message_loop export_proc_handler 3>&1)"
}
alterator-sh-functions-0.13/test/api_read1000064400000000000000000000061511150610253600205770ustar00rootroot00000000000000appendTests \
test_on_message1 \
test_on_message2 \
test_on_message3 \
test_on_message4

# various callbacks for testing
on_message()
{
echo "on_message"
}

on_message1()
{
echo "on_message1"
}


#basic on_message call test
test_on_message1() {
local input1='
_message:begin
_message:end'

local input2="$input1
$input1"
local expected2='on_message1
on_message1'

assertEquals "default on_message" "on_message" "$(echo "$input1"|message_loop)"
assertEquals "special on_message" "on_message1" "$(echo "$input1"|message_loop on_message1)"
assertEquals "two messages" "$expected2" "$(echo "$input2"|message_loop on_message1)"
}

#basic message block processing

on_message2()
{
echo "=$in_a="
}

test_on_message2() {
local input1='
_message:begin
_message:begin
_message:end'

local input2='
_message:begin
_message:end
_message:end'

local input3='
_message:begin
a:b
_message:end
'
local input4='
_message:begin
a:b
_message:begin
_message:end'

local input5='
_message:begin
_message:end
a:b
_message:end'

local input6='
_message:begin
a:b
_message:end
_message:begin
_message:end'

local expected6='=b=
=='

local input7='
_message:begin
a:b
a:c
_message:end'

assertEquals 'double message:begin' '==' "$(echo "$input1"|message_loop on_message2)"
assertEquals 'double message:end' '==' "$(echo "$input2"|message_loop on_message2)"
assertEquals 'parameter inside block' '=b=' "$(echo "$input3"|message_loop on_message2)"
assertNotEquals 'parameter before block' '=b=' "$(echo "$input4"|message_loop on_message2)"
assertNotEquals 'parameter after block' '=b=' "$(echo "$input5"|message_loop on_message2)"
assertEquals 'parameter resetting' "$expected6" "$(echo "$input6"|message_loop on_message2)"
assertEquals 'double parameter' '=c=' "$(echo "$input7"|message_loop on_message2)"
}

#special parameter _objects
on_message3()
{
echo "=$in__objects="
}

test_on_message3()
{
local input1='
_message:begin
_objects:test
_message:end'

local input2='
_message:begin
_message:end'

local input3='
_message:begin
_objects:test\ntest
_message:end'

assertEquals '_objects set' '=test=' "$(echo "$input1"|message_loop on_message3)"
assertEquals '_objects unset' '==' "$(echo "$input2"|message_loop on_message3)"
assertEquals 'no newline quote' '=test\ntest=' "$(echo "$input3"|message_loop on_message3)"
}

#various parameter values
on_message4()
{
echo "=$in_c="
}

assert_on_message4()
{
local input="$1";shift
local expected="$1";shift

local msg="
_message:begin
c:$input
_message:end"

assertEquals "parameter value:$input" "=$expected=" "$(echo "$msg"|message_loop on_message4)"
}

test_on_message4()
{
assert_on_message4 'abcd' 'abcd'
assert_on_message4 'ab:cd' 'ab:cd'
assert_on_message4 'ab\\cd' 'ab\cd'
assert_on_message4 'ab\\c\\d' 'ab\c\d'
assert_on_message4 'ab\ncd' 'ab
cd'
assert_on_message4 'ab\nc\nd' 'ab
c
d'
assert_on_message4 'ab\nc\\d' 'ab
c\d'
assert_on_message4 'ab\\ncd' 'ab\ncd'
assert_on_message4 'ab\\\ncd' 'ab\
cd'
assert_on_message4 'ab\n\\cd' 'ab
\cd'
}
alterator-sh-functions-0.13/test/api_read2000064400000000000000000000005771150610253600206060ustar00rootroot00000000000000appendTests \
test_test_bool

#boolean value converting
assert_test_bool1() {
local rc=0
test_bool "$1" || rc=1
assertEquals "test_bool:$1" "0" "$rc"
}

assert_test_bool2() {
local rc=0
test_bool "$1" || rc=1
assertEquals "test_bool:$1" "1" "$rc"
}

test_test_bool()
{
assert_test_bool1 '#t'
assert_test_bool2 '#f'
assert_test_bool2 'other'
}
alterator-sh-functions-0.13/test/api_write1000064400000000000000000000026321150610253600210160ustar00rootroot00000000000000appendTests \
test_write_bool_param1 \
test_write_bool_param2

#bool param: good values
assert_write_bool_param1()
{
local value="$1";shift
local expected="$1";shift

assertEquals "$expected" "name $expected" "$(write_bool_param 'name' "$value" 3>&1)"
}

#bool param: common
test_write_bool_param1() {
assert_write_bool_param1 'yes' '#t'
assert_write_bool_param1 'true' '#t'
assert_write_bool_param1 'on' '#t'
assert_write_bool_param1 '1' '#t'
assert_write_bool_param1 'Yes' '#t'
assert_write_bool_param1 'True' '#t'
assert_write_bool_param1 'On' '#t'
assert_write_bool_param1 'YES' '#t'
assert_write_bool_param1 'TRUE' '#t'
assert_write_bool_param1 'ON' '#t'

assert_write_bool_param1 'no' '#f'
assert_write_bool_param1 'false' '#f'
assert_write_bool_param1 'off' '#f'
assert_write_bool_param1 '0' '#f'
assert_write_bool_param1 'No' '#f'
assert_write_bool_param1 'False' '#f'
assert_write_bool_param1 'Off' '#f'

assert_write_bool_param1 'Fake' '#f'
assert_write_bool_param1 '' '#f'

assertEquals 'output channel' '' "$(write_bool_param 'name' "yes" 3>/dev/null)"
}

#bool param: wrong names
assert_write_bool_param2()
{
local name="$1";shift

assertNotEquals "bad name test: $name" "$name #t" "$(write_bool_param "$name" "yes" 2>/dev/null 3>&1)"
}

test_write_bool_param2()
{
test_for_symbol assert_write_bool_param2
}
alterator-sh-functions-0.13/test/api_write2000064400000000000000000000013741150610253600210210ustar00rootroot00000000000000appendTests \
test_write_string_param1 \
test_write_string_param2

#bool param: good values
assert_write_string_param1()
{
local expected="$1";shift
local value="$1";shift

assertEquals "$expected" "name \"$expected\"" "$(write_string_param name "$value" 3>&1)"
}

#string param: common
test_write_string_param1() {
test_for_string_extra assert_write_string_param1

assertEquals 'output channel' '' "$(write_string_param 'name' "value" 3>/dev/null)"
}

#string param: wrong names
assert_write_string_param2()
{
local name="$1";shift

assertNotEquals "bad name test: $name" "$name \"line\"" "$(write_string_param "$name" "line" 2>/dev/null 3>&1)"
}

test_write_string_param2() {
test_for_symbol assert_write_string_param2
}
alterator-sh-functions-0.13/test/api_write3000064400000000000000000000015351150610253600210210ustar00rootroot00000000000000appendTests \
test_write_nop \
test_write_error

#empty answer
test_write_nop() {
local expected='(
)'
assertEquals 'write_nop: shell_api_version = 0' "$expected" "$(write_nop 3>&1)"

export alterator_api_version=1
assertEquals 'write_nop: shell_api_version = 0' "" "$( write_nop 3>&1)"
}

#error answer
assert_write_error1()
{
local expected="$1";shift
assertEquals "$expected" "$expected" "$(write_error "$1" 3>&1)"
}

assert_write_error2()
{
local expected="$1";shift

assertEquals "$expected" "error \"$expected\"" "$(write_error "$1" 3>&1)"
}

test_write_error() {

local expected='(
error "abcd"
)'
assert_write_error1 "$expected" 'abcd'

export alterator_api_version=1

test_for_string_extra assert_write_error2

assertEquals 'output channel' '' "$(write_error "value" 3>/dev/null)"
}
alterator-sh-functions-0.13/test/api_write4000064400000000000000000000057371150610253600210320ustar00rootroot00000000000000appendTests \
test_write_enum_item1 \
test_write_enum_item2 \
test_write_enum_item3 \
test_write_enum1 \
test_write_enum2

#enumeration: basic api test
test_write_enum_item1() {

assertEquals "single parameter" '(name "" label "")' "$(write_enum_item "" 3>&1)"
assertEquals "single parameter" '(name "object" label "object")' "$(write_enum_item "object" 3>&1)"
assertEquals "single parameter" '(name "object" label "object")' "$(write_enum_item "object" "" 3>&1)"
assertEquals "two parameters" '(name "object" label "label")' "$(write_enum_item "object" "label" 3>&1)"

assertEquals 'output channel' '' "$(write_enum_item "a" "b" 3>/dev/null)"
}



#enumeration (stream edition): basic api
test_write_enum1()
{
local input1='
'

local expected1=''

local input2='
object1 label1
object2 label2'

local expected2='(name "object1" label "label1")
(name "object2" label "label2")'

local input3='
object1:label1
object2:label2'

local expected3='(name "object1" label "label1")
(name "object2" label "label2")'

assertEquals "empty stream" "$expected1" "$(echo "$input1"|write_enum 3>&1)"
assertEquals 'stream, default separator' "$expected2" "$(echo "$input2"|write_enum 3>&1)"
assertEquals 'stream, output channel' '' "$(echo "$input2"|write_enum 3>/dev/null)"

local IFS=:
assertEquals 'stream, custom separator' "$expected3" "$(echo "$input3"|write_enum 3>&1)"
}

#enumeration: string quoting
assert_write_enum_item2() {
local expected="$1";shift
local input="$1";shift

assertEquals "quoting,name='$input'" "(name \"$expected\" label \"value\")" "$(write_enum_item "$input" value 3>&1)"
assertEquals "quoting,label='$input'" "(name \"value\" label \"$expected\")" "$(write_enum_item value "$input" 3>&1)"
}

test_write_enum_item2() {
test_for_string assert_write_enum_item2
}

#enumeration (stream edition): string quoting
assert_write_enum2() {
local expected="$1";shift
local input="$1";shift

assertEquals "quoting,name='$input'" "(name \"$expected\" label \"value\")" "$(echo "$input value"|write_enum 3>&1)"
assertEquals "quoting,label='$input'" "(name \"value\" label \"$expected\")" "$(echo "value $input"|write_enum 3>&1)"
}

test_write_enum2() {
test_for_string assert_write_enum2
}

#enumeration: deprecated api test
test_write_enum_item3() {
local input1='
'

local expected1=''

local input2='
object1 label1
object2 label2'

local expected2='(name "object1" label "label1")
(name "object2" label "label2")'

local input3='
object1:label1
object2:label2'

local expected3='(name "object1" label "label1")
(name "object2" label "label2")'

assertEquals "empty stream" "$expected1" "$(echo "$input1"|write_enum_item 3>&1 2>/dev/null)"
assertEquals "stream, default separator" "$expected2" "$(echo "$input2"|write_enum_item 3>&1 2>/dev/null)"

local IFS=:
assertEquals "stream, custom separator" "$expected3" "$(echo "$input3"|write_enum_item 3>&1 2>/dev/null)"
}
alterator-sh-functions-0.13/test/api_write5000064400000000000000000000007751150610253600210300ustar00rootroot00000000000000appendTests \
test_write_debug

#debug output: basic api test
test_write_debug() {
ALTERATOR_DEBUG=1
assertEquals "no format" 'debug' "$(write_debug "debug" 2>&1)"
assertEquals "no format" 'debug' "$(write_debug "debug" 2>&1)"
assertEquals "format" 'debug debug' "$(write_debug "debug %s" "debug" 2>&1)"
assertNotEquals "sure stderr" 'debug' "$(write_debug "debug" 2>/dev/null)"
unset ALTERATOR_DEBUG
assertNotEquals "no DEBUG variable" 'debug' "$(write_debug "debug" 2>&1)"
}
alterator-sh-functions-0.13/test/api_write6000064400000000000000000000032151150610253600210210ustar00rootroot00000000000000appendTests \
test_write_table_item1 \
test_write_table_item2 \
test_write_table_item3

#table: basic api test
test_write_table_item1() {
assertEquals 'no parameter' '' "$(write_table_item 3>&1 2>/dev/null)"
assertEquals 'single parameter' '' "$(write_table_item name1 3>&1 2>/dev/null)"
assertEquals ' parameters' '( name1 "value1")' "$(write_table_item name1 value1 3>&1 2>/dev/null)"
assertEquals '3 parameters' '' "$(write_table_item name1 value1 name2 3>&1 2>/dev/null)"
assertEquals '4 parameters' '( name1 "value1" name2 "value2")' "$(write_table_item name1 value1 name2 value2 3>&1 2>/dev/null)"
assertEquals 'output channel' '' "$(write_table_item name1 value1 3>/dev/null)"
}


#table: string quoting
assert_write_table_item2() {
local expected="$1";shift
local input="$1";shift

assertEquals "quoting,firstname='$input'" "( name1 \"$expected\" name2 \"value2\")" "$(write_table_item name1 "$input" name2 value2 3>&1)"
assertEquals "quoting,secondname='$input'" "( name1 \"value1\" name2 \"$expected\")" "$(write_table_item name1 value1 name2 "$input" 3>&1)"
}

test_write_table_item2()
{
test_for_string assert_write_table_item2
}

#table: bad symbol test
assert_write_table_item3()
{
local name="$1";shift

assertNotEquals "bad name test, firstname=$name" "( $name \"value1\" name2 \"value2\")" "$(write_table_item "$name" value1 name2 value2 2>/dev/null 3>&1)"
assertNotEquals "bad name test, secondname=$name" "( name1 \"value1\" $name \"value2\")" "$(write_table_item name1 value1 "$name" value2 2>/dev/null 3>&1)"
}

test_write_table_item3() {
test_for_symbol assert_write_table_item3
}
alterator-sh-functions-0.13/test/api_write7000064400000000000000000000013331150610253600210210ustar00rootroot00000000000000appendTests \
test_write_type_item1 \
test_write_type_item2

#bool param: good values
assert_write_type_item1()
{
local expected="$1";shift
local value="$1";shift

assertEquals "$expected" "name \"$expected\"" "$(write_type_item name "$value" 3>&1)"
}

#string param: common
test_write_type_item1() {
test_for_string_extra assert_write_type_item1

assertEquals 'output channel' '' "$(write_type_item 'name' "value" 3>/dev/null)"
}

#string param: wrong names
assert_write_type_item2()
{
local name="$1";shift

assertNotEquals "bad name test: $name" "$name \"line\"" "$(write_type_item "$name" "line" 2>/dev/null 3>&1)"
}

test_write_type_item2() {
test_for_symbol assert_write_type_item2
}
alterator-sh-functions-0.13/test/api_write8000064400000000000000000000016401150610253600210230ustar00rootroot00000000000000appendTests \
test_write_blob_param1 \
test_write_blob_param2 \
#

#bool param: good values
assert_write_blob_param1()
{
local expected="$1";shift
local value="$1";shift

assertEquals "$expected" "name \"$expected\"" "$(write_blob_param name "$value" 3>&1)"
}

#string param: common
test_write_blob_param1()
{
assertEquals 'output channel' '' "$(write_blob_param name /dev/null 3>/dev/null)"
assertEquals 'empty data' 'name ""' "$(write_blob_param name /dev/null 3>&1)"

local f="/bin/true"
local v="$(base64 -w0 "$f"|string_quote)"
assertEquals 'simple data' "name \"$v\"" "$(write_blob_param name "$f" 3>&1)"
}

#string param: wrong names
assert_write_blob_param2()
{
local name="$1";shift

assertNotEquals "bad name test: $name" "$name \"\"" "$(write_blob_param "$name" /dev/null 2>/dev/null 3>&1)"
}

test_write_blob_param2() {
test_for_symbol assert_write_blob_param2
}
alterator-sh-functions-0.13/test/file_list000064400000000000000000000010031150610253600207130ustar00rootroot00000000000000appendTests \
test_file_list

test_file_list()
{
cat>f1<<EOF
l1
l2
l3
EOF

cat>f2<<EOF
l1
l2
l3
EOF

cat>f3<<EOF
l1
l3
EOF

cat>f4<<EOF
l1
l3
l5
EOF
file_list_del f1 l4
assertEquals "del non existant line" "$(cat f1)" "$(cat f2)"
file_list_del f1 l2
assertEquals "del non existant line" "$(cat f1)" "$(cat f3)"
file_list_add f1 l5
assertEquals "add new line" "$(cat f1)" "$(cat f4)"
file_list_add f1 l5
assertEquals "add new line again" "$(cat f1)" "$(cat f4)"
}
alterator-sh-functions-0.13/test/quote000064400000000000000000000036201150610253600201050ustar00rootroot00000000000000appendTests \
test_newline_unquote1 \
test_newline_unquote2 \
test_string_quote1 \
test_string_quote2

#newline decode
assert_newline_unquote()
{
local input="$1";shift
local expected="$1";shift

assertEquals "$input" "$expected" "$(echo "$input"|newline_unquote "$@")"
}

#newline decode: without argument
test_newline_unquote1() {
assert_newline_unquote 'abcd' 'abcd'
assert_newline_unquote 'ab\\cd' 'ab\cd'
assert_newline_unquote 'ab\\c\\d' 'ab\c\d'
assert_newline_unquote 'ab\ncd' 'ab
cd'
assert_newline_unquote 'ab\nc\nd' 'ab
c
d'
assert_newline_unquote 'ab\nc\\d' 'ab
c\d'
assert_newline_unquote 'ab\\ncd' 'ab\ncd'
assert_newline_unquote 'ab\\\ncd' 'ab\
cd'
assert_newline_unquote 'ab\n\\cd' 'ab
\cd'
}

#newline decode: with argument
test_newline_unquote2()
{
assert_newline_unquote 'ab\\cd' 'ab\\cd' '_objects'
assert_newline_unquote 'ab\\cd' 'ab\cd' 'other_object'
assert_newline_unquote 'ab\\cd' 'ab\cd' 'other object'
assert_newline_unquote 'ab\\cd' 'ab\cd' 'other' 'object'
assert_newline_unquote 'ab\\cd' 'ab\cd' '--help'
}

#string quote: without argument, stream processing
assert_string_quote1()
{
local expected="$1";shift
local input="$1";shift

assertEquals "$input" "$expected" "$(echo "$input"|string_quote)"
}

test_string_quote1() {
test_for_string_extra assert_string_quote1
}

#string quote: with argument, file processing
assert_string_quote2()
{
local expected="$1";shift
local msg="$@"

assertEquals "$msg" "$expected" "$(string_quote "$@")"
}

test_string_quote2() {
local f1='f1.txt'
local f2='--help'

cat>"$f1"<<EOF
abcd
abc\d
ab\c\d
abc"d
ab"c"d
EOF
cp -- "$f1" "$f2"

local expected='abcd
abc\\d
ab\\c\\d
abc\"d
ab\"c\"d'

assert_string_quote2 "$expected" "$f1"
assert_string_quote2 "$expected" "$f2"
assert_string_quote2 "$expected
$expected" "$f1" "$f2"
}
 
design & coding: Vladimir Lettiev aka crux © 2004-2005, Andrew Avramenko aka liks © 2007-2008
current maintainer: Michael Shigorin