|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck disable=SC2034 |
| 3 | + |
| 4 | +set -eo pipefail |
| 5 | +CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 6 | + |
| 7 | +# shellcheck source=../src/helpers.sh |
| 8 | +source "$CURRENT_DIR/helpers.sh" |
| 9 | + |
| 10 | +#=== test:tggles ===# |
| 11 | + |
| 12 | +prepare_batch_options() { |
| 13 | + fake_batch_options \ |
| 14 | + t_id_format="pane/path/{popup_name}" \ |
| 15 | + t_on_init="display 'on-init' ; run '#{@on_init}'" \ |
| 16 | + t_before_open="display 'before-open' ; run '#{@before_open}'" \ |
| 17 | + t_after_close="display 'after-close' ; run '#{@after_close}'" \ |
| 18 | + t_toggle_mode="switch" \ |
| 19 | + t_socket_name="popup_server1" \ |
| 20 | + t_socket_path="socket/path/popup_server2" \ |
| 21 | + t_opened_name="" \ |
| 22 | + t_caller_id_format="caller/id/format" \ |
| 23 | + t_caller_path="caller/session/pane" \ |
| 24 | + t_caller_pane_path="caller/pane/path" \ |
| 25 | + t_default_id_format="session/path/{popup_name}" \ |
| 26 | + t_default_shell="/usr/bin/fish" \ |
| 27 | + t_session_path="working/session/path" \ |
| 28 | + t_pane_path="working/pane/path" |
| 29 | +} |
| 30 | + |
| 31 | +declare delimiter=">>>END" exit_codes f_call_id f_output |
| 32 | +tmux() { |
| 33 | + # Bump call ID |
| 34 | + local call_id |
| 35 | + call_id=$(cat "$f_call_id") |
| 36 | + echo "$((call_id + 1))" >"$f_call_id" |
| 37 | + |
| 38 | + # The first call is always `batch_get_options`. |
| 39 | + # Discard its output since not particular useful. |
| 40 | + if [[ $call_id == 0 ]]; then |
| 41 | + prepare_batch_options |
| 42 | + return |
| 43 | + fi |
| 44 | + |
| 45 | + # Appends arguments to output |
| 46 | + { |
| 47 | + echo ">>>TMUX:BEGIN($call_id)" |
| 48 | + printf "%s\n" "$@" |
| 49 | + echo "<<<TMUX:END($call_id)" |
| 50 | + echo |
| 51 | + } >>"$f_output" |
| 52 | + |
| 53 | + # Fake tmux exit code |
| 54 | + # shellcheck disable=SC2086 |
| 55 | + return ${exit_codes[$call_id]} |
| 56 | +} |
| 57 | + |
| 58 | +declare test_name |
| 59 | +test_toggle() { |
| 60 | + f_call_id=$(alloctmp) |
| 61 | + f_output=$(alloctmp) |
| 62 | + echo 0 >"$f_call_id" |
| 63 | + source "$CURRENT_DIR/toggle.sh" |
| 64 | + |
| 65 | + local expected="$CURRENT_DIR/toggle_tests/$test_name.stdout" |
| 66 | + if [[ $TEST_OVERWRITE = 1 ]]; then |
| 67 | + mkdir -p "$(dirname "$expected")" |
| 68 | + cp "$f_output" "$expected" |
| 69 | + else |
| 70 | + git diff --exit-code "$f_output" "$expected" |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +test_name="open_popup" |
| 75 | +exit_codes=(0 0 0) |
| 76 | +t_toggle_mode="switch" |
| 77 | +t_opened_name="" |
| 78 | +begin_test "$test_name" |
| 79 | +test_toggle --name="p_open" |
| 80 | + |
| 81 | +test_name="close_popup" |
| 82 | +exit_codes=(0 0 0) |
| 83 | +t_toggle_mode="switch" |
| 84 | +t_opened_name="p_close" |
| 85 | +begin_test "$test_name" |
| 86 | +test_toggle --name="p_close" |
| 87 | + |
| 88 | +test_name="switch_popup" |
| 89 | +exit_codes=(0 0 0) |
| 90 | +t_toggle_mode="switch" |
| 91 | +t_opened_name="p_switch_1" |
| 92 | +begin_test "$test_name" |
| 93 | +test_toggle --name="p_switch_2" |
| 94 | + |
| 95 | +test_name="switch_new_popup" |
| 96 | +exit_codes=(0 1 0) |
| 97 | +t_toggle_mode="switch" |
| 98 | +t_opened_name="p_switch_1" |
| 99 | +begin_test "$test_name" |
| 100 | +test_toggle --name="p_switch_2" |
| 101 | + |
| 102 | +test_name="force_close_popup" |
| 103 | +exit_codes=(0 0 0) |
| 104 | +t_toggle_mode="force-close" |
| 105 | +t_opened_name="p_force_close_1" |
| 106 | +begin_test "$test_name" |
| 107 | +test_toggle --name="p_force_close_2" |
| 108 | + |
| 109 | +test_name="open_nested_popup" |
| 110 | +exit_codes=(0 0 0) |
| 111 | +t_toggle_mode="force-open" |
| 112 | +t_opened_name="p_open_nested_1" |
| 113 | +begin_test "$test_name" |
| 114 | +test_toggle --name="p_open_nested_2" |
0 commit comments