tame/build-aux/test/test-csvm2csv

208 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# Test csvm2csv
#
# Copyright (C) 2018 R-T Specialty, LLC.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
cd "$( dirname "$0" )"
# just to ensure that we run all the tests
declare -i testsum=0
# Run test case with input and expected values
run-test()
{
local -r input="${1?Missing input}"
local -r expected="${2?Missing expected}"
((testsum++))
# SUT invocation
declare -r given=$( ../csvm2csv < <( cat <<< "$input" ) )
# expected output
diff <( cat <<< "$expected" ) <( cat <<< "$given" )
}
test-comment()
{
local -r input='# comment before header should be removed
header, line
# this is also a comment
1, 2
# which should be ignored
3, 4'
local -r expected='header,line
1,2
3,4'
run-test "$input" "$expected"
}
test-range()
{
declare -r input='header, line
1--3, 2
3--5, 4--6'
declare -r expected='header,line
1,2
2,2
3,2
3,4
3,5
3,6
4,4
4,5
4,6
5,4
5,5
5,6'
run-test "$input" "$expected"
}
test-delim()
{
declare -r input='header, line
1;4, 2
4;3, 6;9'
declare -r expected='header,line
1,2
4,2
4,6
4,9
3,6
3,9'
run-test "$input" "$expected"
}
test-var()
{
declare -r input='header, line
:foo=1
:bar_baz-quux=2
$foo,1
$bar_baz-quux,$foo'
declare -r expected='header,line
1,1
2,1'
run-test "$input" "$expected"
}
test-range-delim()
{
declare -r input='header, line
1--3;5--6, 2'
declare -r expected='header,line
1,2
2,2
3,2
5,2
6,2'
run-test "$input" "$expected"
}
test-var-in-range-delim()
{
declare -r input='header, line
:foo=1
:bar=3
$foo--$bar, $foo;$bar'
declare -r expected='header,line
1,1
1,3
2,1
2,3
3,1
3,3'
run-test "$input" "$expected"
}
test-var-with-range-delim()
{
declare -r input='header, line
:foo=1--2;4
:bar=5
$foo;$bar, 1'
declare -r expected='header,line
1,1
2,1
4,1
5,1'
run-test "$input" "$expected"
}
test-var-with-var()
{
declare -r input='header, line
:foo=2
:bar=4
:range=$foo--$bar
:baz=$range;$foo
$baz, 5'
declare -r expected='header,line
2,5
3,5
4,5
2,5'
run-test "$input" "$expected"
}
test-comment \
&& test-range \
&& test-delim \
&& test-var \
&& test-range-delim \
&& test-var-in-range-delim \
&& test-var-with-range-delim \
&& test-var-with-var \
|| {
echo 'csvm2csv test failed' >&2
exit 1
}
# safety check
test "$testsum" -eq 8 || {
echo 'error: did not run all csvm2csv tests!' >&2
exit 1
}