#!/usr/bin/env bash
#
# Collection of various test strings that could be the output of the tmux
# 'pane_current_comamnd' message. Included as regression test for updates to
# the inline grep pattern used in the `.tmux.conf` configuration

set -e

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0)

# Import 'vim_pattern'
source 'vim-tmux-navigator.tmux'

match_tests=(
  vim
  Vim
  VIM
  vimdiff
  lvim
  /usr/local/bin/vim
  vi
  gvim
  view
  gview
  nvim
  vimx
  fzf
  .vim-wrapped
  .nvim-wrapped
)
no_match_tests=(
  /Users/christoomey/.vim/thing
  /usr/local/bin/start-vim
  start-vim
  this_is_not_vim
  thisisnotvim
  /vim/is/not/final/path/segment
  file_with_extension.vim
  tvim # there's not yet a vi clone named 'tvim', so the pattern shouldn't match
)

MATCH_RESULT="${GREEN}match${NORMAL}"
NO_MATCH_RESULT="${RED}not match${NORMAL}"

display_matches() {
  local result
  local final_status=0
  for process_name in "$@"; do
    result="$(matches_vim_pattern $process_name)"
    if [[ "${result}" != "${expect_result}" ]]; then
      final_status=1
    fi
    printf "%s %s\n" "${result}" "$process_name"
  done
  return "${final_status}"
}

matches_vim_pattern() {
  if echo "$1" | grep -iqE "^${vim_pattern}$"; then
    echo "${MATCH_RESULT}"
  else
    echo "${NO_MATCH_RESULT}"
  fi
}

main() {
  echo -e "Testing against pattern: ${YELLOW}$vim_pattern${NORMAL}\n"

  local final_status=0
  echo -e "These should all ${MATCH_RESULT}\n----------------------"
  local expect_result="${MATCH_RESULT}"
  display_matches "${match_tests[@]}" || final_status=1

  echo -e "\nThese should all ${NO_MATCH_RESULT}\n--------------------------"
  expect_result="${NO_MATCH_RESULT}"
  display_matches "${no_match_tests[@]}" || final_status=1

  if [[ "${final_status}" == 0 ]]; then
    echo -e "\n${GREEN}All test cases passed!${NORMAL}"
  else
    echo -e "\n${RED}Some test cases are failing${NORMAL}"
  fi

  # Find lines in README.md which redefine 'vim_pattern' so that we can make
  # sure it matches the canonical definition in vim-tmux-navigator.tmux. This
  # includes lines starting with '- vim_pattern', however it skips
  # '+ vim_pattern' since those lines indicate intentional modifications and
  # need manual review.
  IFS='
'
  local -a pattern_copies=($(sed -En "s/^\s*(- )?vim_pattern='(.*)'/\2/p" README.md))

  local pattern_copy
  local readme_status=0
  echo -e "\nChecking README.md documentation"
  for pattern_copy in "${pattern_copies[@]}"; do
    if [[ "${vim_pattern}" != "${pattern_copy}" ]]; then
      echo -n "Update 'vim_pattern' in README.md "
      echo -n "(current line: ${RED}'${pattern_copy}'${NORMAL}) "
      echo "to match ${GREEN}'${vim_pattern}'${NORMAL}"
      readme_status=1
    fi
  done
  if [[ "${readme_status}" == 0 ]]; then
    echo -e "\n${GREEN}README.md is up to date${NORMAL}"
  else
    echo -e "\n${RED}Update README.md to fix defintions of 'vim_pattern'${NORMAL}"
    final_status="${readme_status}"
  fi

  return "${final_status}"
}

main
