OILS / spec / func-parsing.test.sh View on Github | oilshell.org

105 lines, 27 significant
1
2#### Incomplete Function
3## code: foo()
4## status: 2
5## BUG mksh status: 0
6
7#### Incomplete Function 2
8## code: foo() {
9## status: 2
10## OK mksh status: 1
11
12#### Bad function
13## code: foo(ls)
14## status: 2
15## OK mksh status: 1
16
17#### Unbraced function body.
18# dash allows this, but bash does not. The POSIX grammar might not allow
19# this? Because a function body needs a compound command.
20# function_body : compound_command
21# | compound_command redirect_list /* Apply rule 9 */
22## code: one_line() ls; one_line;
23## status: 0
24## OK bash/osh status: 2
25
26#### Function with spaces, to see if ( and ) are separate tokens.
27# NOTE: Newline after ( is not OK.
28fun ( ) { echo in-func; }; fun
29## stdout: in-func
30
31#### subshell function
32# bash allows this.
33i=0
34j=0
35inc() { i=$((i+5)); }
36inc_subshell() ( j=$((j+5)); )
37inc
38inc_subshell
39echo $i $j
40## stdout: 5 0
41
42#### Hard case, function with } token in it
43rbrace() { echo }; }; rbrace
44## stdout: }
45
46#### . in function name
47# bash accepts; dash doesn't
48func-name.ext ( ) { echo func-name.ext; }
49func-name.ext
50## stdout: func-name.ext
51## OK dash status: 2
52## OK dash stdout-json: ""
53
54#### = in function name
55# WOW, bash is so lenient. foo=bar is a command, I suppose. I think I'm doing
56# to disallow this one.
57func-name=ext ( ) { echo func-name=ext; }
58func-name=ext
59## stdout: func-name=ext
60## OK dash status: 2
61## OK dash stdout-json: ""
62## OK mksh status: 1
63## OK mksh stdout-json: ""
64
65#### Function name with $
66$foo-bar() { ls ; }
67## status: 2
68## OK bash/mksh status: 1
69
70#### Function name with command sub
71foo-$(echo hi)() { ls ; }
72## status: 2
73## OK bash/mksh status: 1
74
75#### Function name with !
76# bash allows this; dash doesn't.
77foo!bar() { ls ; }
78## status: 0
79## OK dash status: 2
80
81#### Function name with -
82# bash allows this; dash doesn't.
83foo-bar() { ls ; }
84## status: 0
85## OK dash status: 2
86
87#### Break after ) is OK.
88# newline is always a token in "normal" state.
89echo hi; fun ( )
90{ echo in-func; }
91fun
92## STDOUT:
93hi
94in-func
95## END
96
97#### Nested definition
98# A function definition is a command, so it can be nested
99fun() {
100 nested_func() { echo nested; }
101 nested_func
102}
103fun
104## stdout: nested
105