]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - tests/lib.sh.test
*: License under GPL v3+.
[config/dotfiles.git] / tests / lib.sh.test
1 # Tests for lib.sh.
2
3 # Copyright (C) 2009-2013  Simon Ruderich
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 . ../lib.sh
20
21 # which with proper exit codes and output to stdout.
22 which() {
23     if [ $1 = ls ]; then
24         echo /bin/ls
25     else
26         echo no doesnt-exist in /usr/bin /bin /usr/sbin /sbin
27         return 1
28     fi
29 }
30 echo stdout which
31 installed ls && echo ls installed
32 installed doesnt-exist && echo doesnt-exist installed
33
34 # which with proper exit codes and output to stderr in case of an error.
35 which() {
36     if [ $1 = ls ]; then
37         echo /bin/ls
38     else
39         echo no doesnt-exist in /usr/bin /bin /usr/sbin /sbin >&2
40         return 1
41     fi
42 }
43 echo stderr which
44 installed ls && echo ls installed
45 installed doesnt-exist && echo doesnt-exist installed
46
47 # which with no proper exit codes and output to stdout in case of an error.
48 which() {
49     if [ $1 = ls ]; then
50         echo /bin/ls
51     else
52         echo no doesnt-exist in /usr/bin /bin /usr/sbin /sbin
53     fi
54 }
55 echo stupid which
56 installed ls && echo ls installed
57 installed doesnt-exist && echo doesnt-exist installed
58
59 # which with proper exit codes and no output in case of an error.
60 which() {
61     if [ $1 = ls ]; then
62         echo /bin/ls
63     else
64         return 1
65     fi
66 }
67 echo different which
68 installed ls && echo ls installed
69 installed doesnt-exist && echo doesnt-exist installed
70
71
72 # Tests for generate().
73 echo "Simple test file for generate() using m4.
74 include(../lib.m4)dnl
75 IF(TEST,m4)
76     m4
77 FI
78 IF(TEST,n4)
79     n4
80 FI" > tmp/test.m4
81 generate m4 tmp/test -DTEST=m4
82 # Remove the line with the current date as it changes every time.
83 cat tmp/test | grep -v "It was generated from tmp/test.m4"
84
85 # Test multiple arguments to generate().
86 echo "Test with multiple arguments passed to generate().
87 first: FIRST
88 second: SECOND
89 " > tmp/test-multiple.m4
90 generate m4 tmp/test-multiple -DFIRST=first -DSECOND=second
91 # Remove the line with the current date as it changes every time.
92 cat tmp/test-multiple | grep -v "It was generated from tmp/test-multiple.m4"
93
94 # Test multiple arguments with spaces to generate().
95 echo "Test with multiple arguments with spaces passed to generate().
96 first: FIRST
97 second: SECOND
98 " > tmp/test-multiple-spaces.m4
99 generate m4 tmp/test-multiple-spaces -DFIRST="first with spaces" \
100                                      -DSECOND="second with spaces"
101 # Remove the line with the current date as it changes every time.
102 cat tmp/test-multiple-spaces \
103     | grep -v "It was generated from tmp/test-multiple-spaces.m4"
104
105 # Test generate() using awk.
106 echo "Simple test fiel for generate() using awk.
107 first second
108 " > tmp/test-awk.in
109 generate awk tmp/test-awk '{ print $1 }'
110 # Remove the line with the current date as it changes every time.
111 cat tmp/test-awk | grep -v "It was generated from tmp/test-awk.in"
112
113 # Test generate() using perl.
114 echo "Simple test fiel for generate() using perl.
115 first second
116 " > tmp/test-perl.in
117 generate perl tmp/test-perl -p -e 's/first/best/'
118 # Remove the line with the current date as it changes every time.
119 cat tmp/test-perl | grep -v "It was generated from tmp/test-perl.in"
120
121 # Test generate() using cat.
122 echo "Simple test fiel for generate() using cat (simple copy).
123
124 ...
125 " > tmp/test-cat.real
126 generate cat tmp/test-cat .real
127 # Remove the line with the current date as it changes every time.
128 cat tmp/test-cat | grep -v "It was generated from tmp/test-cat.real"
129
130 # vim: ft=sh