-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_back_test
executable file
·205 lines (155 loc) · 5.93 KB
/
forward_back_test
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
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
set -e
shopt -s expand_aliases
# Test file for forward back
# expect_equal is predicate function for tests,
# failure causes script to exit
file_under_test="$PWD/forward_back"
expect_equal ()
{
echo -n "$1 "
if [ "$2" == "$3" ]; then
echo "[true]"
else
echo "[false]"
exit 1
fi
}
describe ()
{
echo
echo "$1"
echo
}
dir_list_length ()
{
echo "${#_goto_dir_list[@]}"
}
dir_history_length ()
{
echo "${#_goto_dir_history[@]}"
}
current_dir_history_item ()
{
echo "${_goto_dir_history[$_goto_dir_history_index]}"
}
current_dir_list_item ()
{
echo "${_goto_dir_list[$_goto_dir_list_index]}"
}
setup ()
{
echo
echo "$1"
echo
cd "$dir1"
source "$file_under_test"
}
# Directories for testing
prefix="$(dirname $(mktemp) )"
dir1="$prefix/A"
dir2="$dir1/B"
dir3="$dir2/C"
dir4="$dir3/D"
mkdir -p "$dir4"
# Begin tests
setup "When first sourced"
expect_equal "starting history length" $(dir_history_length) 1
expect_equal "starting list length " $(dir_list_length) 1
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
# GOTO
setup "When goto is used"
goto "$dir2"
expect_equal "current history length" $(dir_history_length) 2
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" $dir2
expect_equal "current list directory" "$(current_dir_list_item)" $dir2
setup "When nonexistent directory is goto'ed"
x="$(mktemp -d)"
rmdir "$x"
goto "$x" 2>/dev/null
expect_equal "current history length" $(dir_history_length) 1
expect_equal "current list length " $(dir_list_length) 1
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
setup "When using goto number"
goto "$dir2"
goto -0
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
setup "When going to a place we've already been"
goto "$dir2"
goto "$dir1"
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
setup "When going back i.e. 'cd -'"
goto "$dir2"
goto -
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
# VERTICAL COMMANDS
setup "When using up, history is unaffected"
goto "$dir2"
up
expect_equal "current history length" $(dir_history_length) 2
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" "$dir2"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
setup "When using down, history is unaffected"
goto "$dir2"
goto -0
down
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 2
expect_equal "current history directory" "$(current_dir_history_item)" "$dir1"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir2"
# HISTORY COMMANDS
setup "When going back with back"
goto "$dir2"
goto "$dir3"
back
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 3
expect_equal "current history directory" "$(current_dir_history_item)" "$dir2"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir3"
setup "When going forward"
goto "$dir2"
goto "$dir3"
back
forward
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 3
expect_equal "current history directory" "$(current_dir_history_item)" "$dir3"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir3"
setup "When going back then going to new directory"
goto "$dir2"
goto "$dir3"
back
goto "$dir4"
expect_equal "current history length" $(dir_history_length) 3
expect_equal "current list length " $(dir_list_length) 4
expect_equal "current history directory" "$(current_dir_history_item)" "$dir4"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir4"
setup "When dropping all directories"
goto "$dir2"
drop
expect_equal "current history length" $(dir_history_length) 2
expect_equal "current list length " $(dir_list_length) 1
expect_equal "current history directory" "$(current_dir_history_item)" "$dir2"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir2"
setup "When dropping a specific directory"
goto "$dir2"
drop 1
expect_equal "current history length" $(dir_history_length) 2
expect_equal "current list length " $(dir_list_length) 1
expect_equal "current history directory" "$(current_dir_history_item)" "$dir2"
expect_equal "current list directory" "$(current_dir_list_item)" "$dir1"
echo
echo "All tests completed."