Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 62a5bd8def8a011a3b3cb5d42016da46797d73e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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="2706493105" desc="First" />
      <item name="FOOTYPE_SECONDS_LINE" value="3512333918" desc="Second'\''s @     @Line" />
      <item name="FOOTYPE_THIRD" value="519392729" 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 <<< 'abase
abate
' && echo 'EXPECTED FAILURE' )

  [[ "$err" =~ ABATE && "$err" =~ '157' ]] || {
    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
}