Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'build-aux/test')
-rwxr-xr-xbuild-aux/test/test-list2typedef105
1 files changed, 105 insertions, 0 deletions
diff --git a/build-aux/test/test-list2typedef b/build-aux/test/test-list2typedef
new file mode 100755
index 0000000..e417c02
--- /dev/null
+++ b/build-aux/test/test-list2typedef
@@ -0,0 +1,105 @@
+#!/bin/bash
+# Test list2typedef
+#
+# 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 (sum of primes)
+declare -i testsum=0
+
+
+test-typedef-gen()
+{
+ ((testsum += 1))
+
+ # this list should contain characters that are not valid in constants
+ declare -r input='First
+Second'\''s @ @Line
+
+# comment
+!!!THIRD!!!
+'
+
+ declare -r expected='<?xml version="1.0"?>
+<package xmlns="http://www.lovullo.com/rater"
+ title="FooType Type">
+ <typedef name="FooType" desc="FooType">
+ <enum type="integer">
+ <item name="FOOTYPE_FIRST" value="4285081399" desc="First" />
+ <item name="FOOTYPE_SECONDS_LINE" value="582715539" desc="Second'\''s @ @Line" />
+ <item name="FOOTYPE_THIRD" value="3355510722" desc="!!!THIRD!!!" />
+ </enum>
+ </typedef>
+</package>'
+
+ # SUT invocation
+ declare -r given=$( ../list2typedef FooType < <( cat <<< "$input" ) )
+
+ # expected output
+ diff <( cat <<< "$expected" ) <( cat <<< "$given" )
+}
+
+
+test-collision-name-check()
+{
+ ((testsum += 2))
+
+ # different value, but same generated constant
+ local err=$( ../list2typedef Foo 2>&1 >/dev/null <<< '@@@One
+Foo
+!!!One
+' && echo 'EXPECTED FAILURE' )
+
+ [[ "$err" =~ FOO_ONE ]] || {
+ echo 'expecting useful error message for name collion' >&2
+ return 1
+ }
+}
+
+
+test-collision-value-check()
+{
+ ((testsum += 5))
+
+ # we can easily force a collision by reducing the number of bytes to 1 and
+ # calculating hashes from /usr/share/dict/words; this is one example
+ local err=$( ../list2typedef Foo 1 2>&1 >/dev/null <<< 'abacist
+abatis
+' && echo 'EXPECTED FAILURE' )
+
+ [[ "$err" =~ ABATIS && "$err" =~ '44' ]] || {
+ echo 'expecting useful error message for value collion' >&2
+ return 1
+ }
+}
+
+
+test-typedef-gen \
+ && test-collision-name-check \
+ && test-collision-value-check \
+ || {
+ echo 'list2typedef test failed' >&2
+ exit 1
+ }
+
+# safety check
+test "$testsum" -eq 8 || {
+ echo 'error: did not run all list2typedef tests!' >&2
+ exit 1
+}