summaryrefslogtreecommitdiff
path: root/test/unit/_get_cword.exp
blob: 9708a87001b75cccd174a87efbeba93c1a196cef (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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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}
    # Delete 'COMP_WORDBREAKS' occupying two lines
    assert_env_unmodified {
        /COMP_WORDBREAKS=/{N
        d
        }
        /_scp_path_esc=/d
    }
}


setup


set test "_get_cword should run without errors"
assert_bash_exec {_get_cword > /dev/null} $test
sync_after_int

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

set test "a b| should return b";  # | = cursor position
set cmd {COMP_WORDS=(a b); COMP_CWORD=1; COMP_LINE='a b'; COMP_POINT=3; _get_cword; echo}
assert_bash_list b $cmd $test
sync_after_int

set test "a | should return nothing";  # | = cursor position
set cmd {COMP_WORDS=(a); COMP_CWORD=1; COMP_LINE='a '; COMP_POINT=2; _get_cword}
send "$cmd\r"
expect -ex "$cmd\r\n/@" {pass "$test"}
sync_after_int

set test "a b | should return nothing";  # | = cursor position
set cmd {COMP_WORDS=(a b ''); COMP_CWORD=2; COMP_LINE='a b '; COMP_POINT=4; _get_cword}
send "$cmd\r"
expect -ex "$cmd\r\n/@" {pass "$test"}
sync_after_int

set test "a b | with WORDBREAKS -= : should return nothing";  # | = cursor position
set cmd {COMP_WORDS=(a b ''); COMP_CWORD=2; COMP_LINE='a b '; COMP_POINT=4; _get_cword :}
send "$cmd\r"
expect -ex "$cmd\r\n/@" {pass "$test"}
sync_after_int

set test "a b|c should return b";  # | = cursor position
set cmd {COMP_WORDS=(a bc); COMP_CWORD=1; COMP_LINE='a bc'; COMP_POINT=3; _get_cword; echo}
assert_bash_list b $cmd $test
sync_after_int

set test {a b\ c| should return b\ c};  # | = cursor position
set cmd {COMP_WORDS=(a 'b\ c'); COMP_CWORD=1; COMP_LINE='a b\ c'; COMP_POINT=6; _get_cword; echo}
assert_bash_list {"b\\ c"} $cmd $test
sync_after_int

set test {a b\| c should return b\ };  # | = cursor position
set cmd {COMP_WORDS=(a 'b\ c'); COMP_CWORD=1; COMP_LINE='a b\ c'; COMP_POINT=4; _get_cword; echo}
assert_bash_list {"b\\"} $cmd $test
sync_after_int

set test {a "b\| should return "b\ };  # | = cursor position
set cmd {COMP_WORDS=(a '"b\'); COMP_CWORD=1; COMP_LINE='a "b\'; COMP_POINT=5; _get_cword; echo}
assert_bash_list {"\"b\\"} $cmd $test
sync_after_int

set test {a 'b c| should return 'b c};  # | = cursor position
set cmd {COMP_WORDS=(a "'b c"); COMP_CWORD=1}
append cmd {; COMP_LINE="a 'b c"; COMP_POINT=6; _get_cword}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -ex "'b c/@" { pass "$test" }
    -ex "c/@" { fail "$test" }
}
sync_after_int

set test {a "b c| should return "b c};  # | = cursor position
set cmd {COMP_WORDS=(a "\"b c"); COMP_CWORD=1}
append cmd {; COMP_LINE="a \"b c"; COMP_POINT=6; _get_cword};
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -ex "\"b c/@" { pass "$test" }
    -ex "c/@" { fail "$test" }
}
sync_after_int

set test {a b:c| with WORDBREAKS += : should return c};  # | = cursor position
set cmd {add_comp_wordbreak_char :; COMP_WORDS=(a b : c); COMP_CWORD=3}
set expected c
append cmd {; COMP_LINE='a b:c'; COMP_POINT=5; _get_cword; echo}
assert_bash_list $expected $cmd $test
sync_after_int

set test {a b:c| with WORDBREAKS -= : should return b:c};  # | = cursor position
set cmd {COMP_WORDS=(a b : c); COMP_CWORD=3}
append cmd {; COMP_LINE='a b:c'; COMP_POINT=5; _get_cword :; echo}
assert_bash_list b:c $cmd $test
sync_after_int

set test {a b c:| with WORDBREAKS -= : should return c:};  # | = cursor position
set cmd {COMP_WORDS=(a b c :); COMP_CWORD=3}
append cmd {; COMP_LINE='a b c:'; COMP_POINT=6; _get_cword :; echo}
assert_bash_list c: $cmd $test
sync_after_int

set test {a :| with WORDBREAKS -= : should return :};  # | = cursor position
set cmd {COMP_WORDS=(a :); COMP_CWORD=1; COMP_LINE='a :'; COMP_POINT=3; _get_cword :; echo}
assert_bash_list : $cmd $test
sync_after_int

set test {a b::| with WORDBREAKS -= : should return b::};  # | = cursor position
set cmd {COMP_WORDS=(a b ::); COMP_CWORD=2}
append cmd {; COMP_LINE='a b::'; COMP_POINT=5; _get_cword :; echo}
assert_bash_list b:: $cmd $test
sync_after_int

# This test makes sure `_get_cword' doesn't use `echo' to return it's value,
# because -n might be interpreted by `echo' and thus will not be returned.
set test "a -n| should return -n";  # | = cursor position
set cmd {COMP_WORDS=(a -n); COMP_CWORD=1; COMP_LINE='a -n'; COMP_POINT=4; _get_cword; echo}
assert_bash_list -n $cmd $test
sync_after_int

set test {a b>c| should return c};  # | = cursor position
set cmd {COMP_WORDS=(a b \> c); COMP_CWORD=3; COMP_LINE='a b>c'; COMP_POINT=5; _get_cword; echo}
assert_bash_list c $cmd $test
sync_after_int

set test {a b=c| should return c};  # | = cursor position
set cmd {COMP_WORDS=(a b = c); COMP_CWORD=3}
set expected c
append cmd {; COMP_LINE='a b=c'; COMP_POINT=5; _get_cword; echo}
assert_bash_list $expected $cmd $test
sync_after_int

set test {a *| should return *};  # | = cursor position
set cmd {COMP_WORDS=(a \*); COMP_CWORD=1; COMP_LINE='a *'; COMP_POINT=4; _get_cword; echo}
assert_bash_list * $cmd $test
sync_after_int

set test {a $(b c| should return $(b c};  # | = cursor position
set cmd {COMP_WORDS=(a '$(b c'); COMP_CWORD=1; COMP_LINE='a $(b c'; COMP_POINT=7; _get_cword}
#assert_bash_list {{$(b\ c}} $cmd $test
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -ex "\$(b c/@" { pass "$test" }
    # Expected failure on bash-4
    -ex "c/@" { xfail "$test" }
}
sync_after_int

set test {a $(b c\ d| should return $(b c\ d};  # | = cursor position
set cmd {COMP_WORDS=(a '$(b c\ d'); COMP_CWORD=1; COMP_LINE='a $(b c\ d'; COMP_POINT=10; _get_cword}
#assert_bash_list {{$(b\ c\\\ d}} $cmd $test
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -ex "\$(b c\\ d/@" { pass "$test" }
    # Expected failure on bash-4
    -ex "c\\ d/@" { xfail "$test" }
}
sync_after_int

set test {a 'b&c| should return 'b&c};  # | = cursor position
set cmd {COMP_WORDS=(a "'b&c"); COMP_CWORD=1}
append cmd {; COMP_LINE="a 'b&c"; COMP_POINT=6; _get_cword}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -ex "'b&c/@" { pass "$test" }
    -ex "c/@" { fail "$test" }
}
sync_after_int

set test "index shouldn't drop below 0"
set dir $::srcdir/fixtures/_get_cword
set cmd "scp"
send "$cmd ääää§ se\t\r\n"
expect {
    -re "index: substring expression < 0" { fail "$test" }
    -re /@ { pass "$test" }
    default { unresolved "$test" }
}
sync_after_int


teardown