File: function-p.tst

package info (click to toggle)
yash 2.60-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,152 kB
  • sloc: ansic: 34,578; makefile: 851; sh: 808; sed: 16
file content (161 lines) | stat: -rw-r--r-- 2,169 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# function-p.tst: test of functions for any POSIX-compliant shell

posix="true"

test_OE -e 0 'effect of function definition (without executing the function)'
func(){ echo foo; } <_no_such_file_
__IN__

test_oE 'grouping as function body'
func(){ echo foo; }
func
__IN__
foo
__OUT__

test_oE 'subshell as function body'
func()(echo foo; exit; echo not reached)
func
:
__IN__
foo
__OUT__

test_oE 'for loop as function body'
func()for i in 1; do echo $i; done
func
__IN__
1
__OUT__

test_oE 'case statement as function body'
func()case 1 in 1) echo foo; esac
func
__IN__
foo
__OUT__

test_oE 'if statement as function body'
func()if echo foo; then echo bar; fi
func
__IN__
foo
bar
__OUT__

test_oE 'while loop as function body'
func()while true; do echo foo; break; done
func
__IN__
foo
__OUT__

test_oE 'until loop as function body'
func()until false; do echo foo; break; done
func
__IN__
foo
__OUT__

test_oE 're-defining a function'
func() { echo foo; }
func() { echo bar; }
func
__IN__
bar
__OUT__

test_oE 'characters in portable name'
_abcXYZ1() { echo foo; }
_abcXYZ1
__IN__
foo
__OUT__

test_oE 'functions and variables belong to separate namespaces'
foo=variable
foo() { echo function; }
echo $foo
foo=X
foo
__IN__
variable
function
__OUT__

test_OE 'redirections apply to function body'
func() { echo foo; cat; } >/dev/null <<END
bar
END
func
__IN__

test_oE '$# in function'
func() { echo $#; }
func
func 1
func 1 2
func 1 '2  2' 3
__IN__
0
1
2
3
__OUT__

test_oE 'arguments to function'
func() { [ $# -gt 0 ] && printf '[%s]' "$@"; echo; }
func
func 1
func 1 2
func 1 '2  2' 3
set a
func 1
echo "$@"
__IN__

[1]
[1][2]
[1][2  2][3]
[1]
a
__OUT__

test_oE '$0 remains unchanged while executing function'
func() { printf '%s\n' "${0##*/}"; }
func
func 1
__IN__
sh
sh
__OUT__

(
setup 'func() (exit $1)'

test_OE -e 0 'exit status of function call (0)'
func 0
__IN__

test_OE -e 1 'exit status of function call (1)'
func 1
__IN__

test_OE -e 19 'exit status of function call (19)'
func 19
__IN__

)

test_oE 'variable assigned in function remains after return'
func() {
    foo=bar
}
foo=
func
echo $foo
__IN__
bar
__OUT__

# vim: set ft=sh ts=8 sts=4 sw=4 et: