/ tests / test_ext_completers.sh
test_ext_completers.sh
  1  #!/bin/bash
  2  # Test external completers and literal punctuation handling
  3  
  4  # shellcheck disable=SC1091
  5  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6  REPO_ROOT="$(dirname "$SCRIPT_DIR")"
  7  
  8  source "$REPO_ROOT/contrib/bar_complete"
  9  
 10  echo "Testing external completers and literal punctuation..."
 11  echo ""
 12  
 13  # Test 1: External completer function
 14  echo "Test 1: Testing external completer setup"
 15  
 16  # Create a mock external completer function
 17  function test_ext_complete()
 18  {
 19      echo "option1"
 20      echo "option2"
 21      echo "option3"
 22  }
 23  
 24  # Initialize registry and add the external completer
 25  __bar_init_completion_registry
 26  __bar_protoregistry["testproto"]="ext test_ext_complete"
 27  
 28  # Get the expanded completer
 29  completer=$(__bar_get_completer "" "testproto")
 30  echo "  Expanded completer: $completer"
 31  
 32  if [[ "$completer" == "__bar_comp_ext test_ext_complete" ]]; then
 33      echo "✓ PASS: External completer expanded correctly"
 34  else
 35      echo "✗ FAIL: Expected '__bar_comp_ext test_ext_complete', got '$completer'"
 36  fi
 37  
 38  # Test 2: Call the external completer
 39  echo ""
 40  echo "Test 2: Calling external completer"
 41  
 42  # Mock bar --bare to return some results
 43  function bar()
 44  {
 45      if [[ "$1" == "--bare" && "$2" == "test_ext_complete" ]]; then
 46          echo "result1"
 47          echo "result2"
 48          return 0
 49      fi
 50      return 1
 51  }
 52  
 53  results=$(__bar_comp_ext "test_ext_complete" "res")
 54  result_count=$(echo "$results" | wc -l)
 55  
 56  if [[ $result_count -ge 2 ]]; then
 57      echo "✓ PASS: External completer returned results"
 58  else
 59      echo "✗ FAIL: External completer did not return expected results"
 60  fi
 61  
 62  echo ""
 63  echo "Test 3: Testing literal punctuation in prototypes"
 64  
 65  # Test parsing of <+toolchain>
 66  echo "  Testing <+toolchain> parsing..."
 67  protos=$(_bar_parse_protos "<+toolchain>")
 68  proto_array=()
 69  while IFS= read -r line; do
 70      [[ -n "$line" ]] && proto_array+=("$line")
 71  done <<< "$protos"
 72  
 73  echo "  Parsed prototypes: ${proto_array[*]}"
 74  if [[ "${proto_array[0]}" == "+toolchain" ]]; then
 75      echo "✓ PASS: <+toolchain> parsed as '+toolchain'"
 76  else
 77      echo "✗ FAIL: Expected '+toolchain', got '${proto_array[0]}'"
 78  fi
 79  
 80  # Test parsing of <rule:>
 81  echo ""
 82  echo "  Testing <rule:> parsing..."
 83  protos=$(_bar_parse_protos "<rule:>")
 84  proto_array=()
 85  while IFS= read -r line; do
 86      [[ -n "$line" ]] && proto_array+=("$line")
 87  done <<< "$protos"
 88  
 89  echo "  Parsed prototypes: ${proto_array[*]}"
 90  if [[ "${proto_array[0]}" == "rule:" ]]; then
 91      echo "✓ PASS: <rule:> parsed as 'rule:'"
 92  else
 93      echo "✗ FAIL: Expected 'rule:', got '${proto_array[0]}'"
 94  fi
 95  
 96  # Test parsing of [+toolchain]
 97  echo ""
 98  echo "  Testing [+toolchain] parsing..."
 99  protos=$(_bar_parse_protos "[+toolchain]")
100  proto_array=()
101  while IFS= read -r line; do
102      [[ -n "$line" ]] && proto_array+=("$line")
103  done <<< "$protos"
104  
105  echo "  Parsed prototypes: ${proto_array[*]}"
106  if [[ "${proto_array[0]}" == "[+toolchain]" ]]; then
107      echo "✓ PASS: [+toolchain] parsed as '[+toolchain]' (optional)"
108  else
109      echo "✗ FAIL: Expected '[+toolchain]', got '${proto_array[0]}'"
110  fi
111  
112  echo ""
113  echo "Test 4: Testing literal punctuation extraction"
114  
115  # Test _bar_extract_literal_punct function
116  echo "  Testing _bar_extract_literal_punct with '+toolchain'..."
117  result=$(_bar_extract_literal_punct "+toolchain")
118  result_array=()
119  while IFS= read -r line; do
120      result_array+=("$line")
121  done <<< "$result"
122  
123  if [[ "${result_array[0]}" == "toolchain" && "${result_array[1]}" == "+" && "${result_array[2]}" == "" ]]; then
124      echo "✓ PASS: '+toolchain' → proto='toolchain', prefix='+', suffix=''"
125  else
126      echo "✗ FAIL: Expected proto='toolchain', prefix='+', suffix=''"
127      echo "  Got: proto='${result_array[0]}', prefix='${result_array[1]}', suffix='${result_array[2]}'"
128  fi
129  
130  echo "  Testing _bar_extract_literal_punct with 'rule:'..."
131  result=$(_bar_extract_literal_punct "rule:")
132  result_array=()
133  while IFS= read -r line; do
134      result_array+=("$line")
135  done <<< "$result"
136  
137  if [[ "${result_array[0]}" == "rule" && "${result_array[1]}" == "" && "${result_array[2]}" == ":" ]]; then
138      echo "✓ PASS: 'rule:' → proto='rule', prefix='', suffix=':'"
139  else
140      echo "✗ FAIL: Expected proto='rule', prefix='', suffix=':'"
141      echo "  Got: proto='${result_array[0]}', prefix='${result_array[1]}', suffix='${result_array[2]}'"
142  fi
143  
144  echo "  Testing _bar_extract_literal_punct with '--flag'..."
145  result=$(_bar_extract_literal_punct "--flag")
146  result_array=()
147  while IFS= read -r line; do
148      result_array+=("$line")
149  done <<< "$result"
150  
151  if [[ "${result_array[0]}" == "--flag" && "${result_array[1]}" == "" && "${result_array[2]}" == "" ]]; then
152      echo "✓ PASS: '--flag' → proto='--flag' (-- not stripped)"
153  else
154      echo "✗ FAIL: Expected proto='--flag' with no literals"
155      echo "  Got: proto='${result_array[0]}', prefix='${result_array[1]}', suffix='${result_array[2]}'"
156  fi
157  
158  echo "  Testing _bar_extract_literal_punct with 'name..'..."
159  result=$(_bar_extract_literal_punct "name..")
160  result_array=()
161  while IFS= read -r line; do
162      result_array+=("$line")
163  done <<< "$result"
164  
165  if [[ "${result_array[0]}" == "name.." && "${result_array[1]}" == "" && "${result_array[2]}" == "" ]]; then
166      echo "✓ PASS: 'name..' → proto='name..' (.. not stripped)"
167  else
168      echo "✗ FAIL: Expected proto='name..' with no literals"
169      echo "  Got: proto='${result_array[0]}', prefix='${result_array[1]}', suffix='${result_array[2]}'"
170  fi
171  
172  echo ""
173  echo "External completer tests complete"