summaryrefslogtreecommitdiff
path: root/test/unit/_get_comp_words_by_ref.exp
blob: a0e1886b1c282136443638c5ffb6e9363629b9da (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
proc setup {} {
    assert_bash_exec {unset COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS}
    save_env
}


proc teardown {} {
    assert_bash_exec { \
        unset COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS cur prev words cword \
        cur2 prev2 words2 cword2 \
    }
    # Delete 'COMP_WORDBREAKS' occupying two lines
    assert_env_unmodified {
        /COMP_WORDBREAKS=/{N
            d
        }
    }
}


setup


# See also ./lib/completions/alias.exp.  Here `_get_cword' is actually tested
# by moving the cursor left into the current word.

set test {unknown argument should raise error}
set cmd {_get_comp_words_by_ref dummy}
assert_bash_list {"bash_completion: _get_comp_words_by_ref: `dummy': unknown argument"} $cmd $test
sync_after_int

set test "a b| to all vars";  # | = cursor position
set cmd {COMP_WORDS=(a b); COMP_CWORD=1; COMP_LINE='a b'; COMP_POINT=3}
assert_bash_exec $cmd
set cmd { \
    _get_comp_words_by_ref words cword prev cur; echo "${words[@]} $cword $cur $prev" \
}
assert_bash_list {"a b 1 b a"} $cmd $test
sync_after_int

set test "a b| to alternate vars";  # | = cursor position
set cmd {COMP_WORDS=(a b); COMP_CWORD=1; COMP_LINE='a b'; COMP_POINT=3;}
assert_bash_exec $cmd
set cmd {_get_comp_words_by_ref -c cur2 -p prev2 -w words2 -i cword2}
assert_bash_exec $cmd
set cmd {echo "$cur2 $prev2 ${words2[@]} $cword2"}
assert_bash_list {"b a a b 1"} $cmd $test
sync_after_int

set test "a b| to alternate vars";  # | = cursor position
set cmd {COMP_WORDS=(a b); COMP_CWORD=1; COMP_LINE='a b'; COMP_POINT=3;}
assert_bash_exec $cmd
set cmd {_get_comp_words_by_ref -c cur2 -p prev2 -w words2 -i cword2}
assert_bash_exec $cmd
set cmd {echo "$cur2 $prev2 ${words2[@]} $cword2"}
assert_bash_list {"b a a b 1"} $cmd $test
sync_after_int

set test {a b : c| with WORDBREAKS -= :};  # | = cursor position
set cmd {COMP_WORDS=(a b : c); COMP_CWORD=3; COMP_LINE='a b : c'; COMP_POINT=7}
assert_bash_exec $cmd $test
set cmd {_get_comp_words_by_ref -n : words; echo "${words[@]}"}
assert_bash_list {"a b : c"} $cmd $test
sync_after_int

set test {a b: c| with WORDBREAKS -= :};  # | = cursor position
set cmd {COMP_WORDS=(a b : c); COMP_CWORD=3}
append cmd {; COMP_LINE='a b: c'; COMP_POINT=6}
assert_bash_exec $cmd $test
set cmd {_get_comp_words_by_ref -n : words; echo "${words[@]}"}
assert_bash_list {"a b: c"} $cmd $test
sync_after_int

set test {a b :c| with WORDBREAKS -= :};  # | = cursor position
set cmd {COMP_WORDS=(a b : c); COMP_CWORD=3}
append cmd {; COMP_LINE='a b :c'; COMP_POINT=6}
assert_bash_exec $cmd $test
set cmd {_get_comp_words_by_ref -n : words; echo "${words[@]}"}
assert_bash_list {"a b :c"} $cmd $test
sync_after_int

set test {a b\ :c| with WORDBREAKS -= :};  # | = cursor position
set cmd {COMP_WORDS=(a "b\\ " : c); COMP_CWORD=3}
append cmd {; COMP_LINE='a b\ :c'; COMP_POINT=7}
assert_bash_exec $cmd $test
set cmd {_get_comp_words_by_ref -n : words; echo "${words[@]}"}
assert_bash_list {a "b\\ :c"} $cmd $test
sync_after_int


teardown