aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/function.in
blob: 8202fb139a6aa4f6e9e0bffe50a1305b66fbc57e (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
# vim: set filetype=fish:
#
# Test the `function` builtin

# utility function
function show_ary -a name --no-scope-shadowing
    set -l count (count $$name)
    echo "\$$name: ($count)"
    if test $count -gt 0
        for i in (seq $count)
            echo "$i: '$$name[1][$i]'"
        end
    end
end

# Test the -V flag
set -g foo 'global foo'
set -l foo 'local foo'
set bar one 'two    2' \t '' 3
set baz
function frob -V foo -V bar -V baz
    show_ary foo
    show_ary bar
    show_ary baz
end
echo "Testing -V"
frob
echo "Testing -V with changed variables"
set foo 'bad foo'
set bar 'bad bar'
set baz 'bad baz'
frob

# Test that -a does not mix up the function name with arguments
# See #2068
function name1 -a arg1 arg2 ; end
function -a arg1 arg2 name2 ; end
function name3 --argument-names arg1 arg2 ; end
function --argument-names arg1 arg2 name4 ; end
for i in (seq 4)
    if functions -q name$i
        echo "Function name$i found"
    else
        echo "Function name$i not found, but should have been"
    end
end

# Test that we can't define a function that shadows a builtin by accident.
function pwd; end
or echo 'yes, it failed as expected' >&2

# Test that we can define a function that shadows a builtin if we use the
# right flag.
function pwd --shadow-builtin; end
and echo '"function pwd --shadow-builtin" worked'

# Using --shadow-builtin for a non-builtin function name also fails.
function not_builtin --shadow-builtin; end
or echo 'yes, it failed as expected' >&2