|
| 1 | +#!/usr/bin/env zsh |
| 2 | +# Tests for czruby tab completion |
| 3 | + |
| 4 | +source "${0:A:h}/test_helper.zsh" |
| 5 | + |
| 6 | +test_completion_function_exists() { |
| 7 | + fpath=("$CZRUBY_ROOT" $fpath) |
| 8 | + autoload -Uz _czruby |
| 9 | + |
| 10 | + if ! whence -w _czruby >/dev/null 2>&1; then |
| 11 | + echo "ERROR: Completion function _czruby not found" |
| 12 | + return 1 |
| 13 | + fi |
| 14 | + return 0 |
| 15 | +} |
| 16 | + |
| 17 | +test_extract_ruby_names_from_paths() { |
| 18 | + local ruby1=$(create_mock_ruby "3.3.0") |
| 19 | + local ruby2=$(create_mock_ruby "truffleruby-21.1.0") |
| 20 | + rubies=("system" "$ruby1" "$ruby2") |
| 21 | + |
| 22 | + # Expected basenames: system, 3.3.0, truffleruby-21.1.0 |
| 23 | + local name1="${ruby1:t}" |
| 24 | + local name2="${ruby2:t}" |
| 25 | + |
| 26 | + assert_equals "3.3.0" "$name1" || return 1 |
| 27 | + assert_equals "truffleruby-21.1.0" "$name2" || return 1 |
| 28 | +} |
| 29 | + |
| 30 | +test_parse_hyphenated_ruby_names() { |
| 31 | + local key="truffleruby-21.1.0" |
| 32 | + local engine="${key%%-*}" |
| 33 | + local version="${key#*-}" |
| 34 | + |
| 35 | + assert_equals "truffleruby" "$engine" || return 1 |
| 36 | + assert_equals "21.1.0" "$version" || return 1 |
| 37 | +} |
| 38 | + |
| 39 | +test_parse_plain_ruby_names() { |
| 40 | + local key="3.3.0" |
| 41 | + |
| 42 | + # No hyphen, so engine defaults to ruby |
| 43 | + if [[ "$key" =~ "-" ]]; then |
| 44 | + echo "ERROR: Should not contain hyphen" |
| 45 | + return 1 |
| 46 | + fi |
| 47 | + return 0 |
| 48 | +} |
| 49 | + |
| 50 | +test_completion_with_empty_rubies() { |
| 51 | + rubies=() |
| 52 | + |
| 53 | + fpath=("$CZRUBY_ROOT" $fpath) |
| 54 | + autoload -Uz _czruby |
| 55 | + |
| 56 | + # Should not crash |
| 57 | + return 0 |
| 58 | +} |
| 59 | + |
| 60 | +test_parse_multiple_hyphens() { |
| 61 | + local key="ruby-3.3.0-preview1" |
| 62 | + local engine="${key%%-*}" |
| 63 | + local version="${key#*-}" |
| 64 | + |
| 65 | + # Should split on first hyphen only |
| 66 | + assert_equals "ruby" "$engine" || return 1 |
| 67 | + assert_equals "3.3.0-preview1" "$version" || return 1 |
| 68 | +} |
| 69 | + |
| 70 | +# Run tests |
| 71 | +echo "Running czruby completion tests..." |
| 72 | +echo "========================================" |
| 73 | + |
| 74 | +run_test "Completion function exists" test_completion_function_exists |
| 75 | +run_test "Extract ruby names from paths" test_extract_ruby_names_from_paths |
| 76 | +run_test "Parse hyphenated ruby names" test_parse_hyphenated_ruby_names |
| 77 | +run_test "Parse plain ruby names" test_parse_plain_ruby_names |
| 78 | +run_test "Handle empty rubies array" test_completion_with_empty_rubies |
| 79 | +run_test "Parse multiple hyphens" test_parse_multiple_hyphens |
| 80 | + |
| 81 | +print_summary |
0 commit comments