pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/usage/completion/bash/bash_test.go (about)

     1  package bash
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"testing"
    12  
    13  	"pkg.re/essentialkaos/ek.v12/usage"
    14  
    15  	. "pkg.re/essentialkaos/check.v1"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  const _RESULT_FILES = `# Completion for test
    21  # This completion is automatically generated
    22  
    23  _test() {
    24    local cur prev cmds opts show_files
    25  
    26    COMPREPLY=()
    27    cur="${COMP_WORDS[COMP_CWORD]}"
    28    prev="${COMP_WORDS[COMP_CWORD-1]}"
    29  
    30    cmds="print clean"
    31    opts="--option-aaa --option-bbb --option-ccc"
    32    show_files="true"
    33    file_glob="[sr]pm"
    34  
    35    case $prev in
    36      clean)
    37        opts="--option-ddd --option-eee"
    38        COMPREPLY=($(compgen -W "$opts" -- "$cur"))
    39        return 0
    40        ;;
    41  
    42    esac
    43  
    44    if [[ $cur == -* ]] ; then
    45      COMPREPLY=($(compgen -W "$opts" -- "$cur"))
    46      return 0
    47    fi
    48  
    49    _test_filter "$cmds" "$opts" "$show_files" "$file_glob"
    50  }
    51  
    52  _test_filter() {
    53    local cmds="$1"
    54    local opts="$2"
    55    local show_files="$3"
    56    local file_glob="$4"
    57  
    58    local cmd1 cmd2
    59  
    60    for cmd1 in $cmds ; do
    61      for cmd2 in ${COMP_WORDS[*]} ; do
    62        if [[ "$cmd1" == "$cmd2" ]] ; then
    63          if [[ -z "$show_files" ]] ; then
    64            COMPREPLY=($(compgen -W "$opts" -- "$cur"))
    65          else
    66            _filedir "$file_glob"
    67          fi
    68  
    69          return 0
    70        fi
    71      done
    72    done
    73  
    74    if [[ -z "$show_files" ]] ; then
    75      COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
    76      return 0
    77    fi
    78  
    79    _filedir "$file_glob"
    80  }
    81  
    82  complete -F _test test -o filenames
    83  `
    84  
    85  const _RESULT_NO_FILES = `# Completion for test
    86  # This completion is automatically generated
    87  
    88  _test() {
    89    local cur prev cmds opts show_files
    90  
    91    COMPREPLY=()
    92    cur="${COMP_WORDS[COMP_CWORD]}"
    93    prev="${COMP_WORDS[COMP_CWORD-1]}"
    94  
    95    cmds="print clean"
    96    opts="--option-aaa --option-bbb --option-ccc"
    97    show_files=""
    98    file_glob=""
    99  
   100    case $prev in
   101      clean)
   102        opts="--option-ddd --option-eee"
   103        COMPREPLY=($(compgen -W "$opts" -- "$cur"))
   104        return 0
   105        ;;
   106  
   107    esac
   108  
   109    if [[ $cur == -* ]] ; then
   110      COMPREPLY=($(compgen -W "$opts" -- "$cur"))
   111      return 0
   112    fi
   113  
   114    _test_filter "$cmds" "$opts" "$show_files" "$file_glob"
   115  }
   116  
   117  _test_filter() {
   118    local cmds="$1"
   119    local opts="$2"
   120    local show_files="$3"
   121    local file_glob="$4"
   122  
   123    local cmd1 cmd2
   124  
   125    for cmd1 in $cmds ; do
   126      for cmd2 in ${COMP_WORDS[*]} ; do
   127        if [[ "$cmd1" == "$cmd2" ]] ; then
   128          if [[ -z "$show_files" ]] ; then
   129            COMPREPLY=($(compgen -W "$opts" -- "$cur"))
   130          else
   131            _filedir "$file_glob"
   132          fi
   133  
   134          return 0
   135        fi
   136      done
   137    done
   138  
   139    if [[ -z "$show_files" ]] ; then
   140      COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
   141      return 0
   142    fi
   143  
   144    _filedir "$file_glob"
   145  }
   146  
   147  complete -F _test test 
   148  `
   149  
   150  // ////////////////////////////////////////////////////////////////////////////////// //
   151  
   152  func Test(t *testing.T) { TestingT(t) }
   153  
   154  type BashSuite struct{}
   155  
   156  // ////////////////////////////////////////////////////////////////////////////////// //
   157  
   158  var _ = Suite(&BashSuite{})
   159  
   160  // ////////////////////////////////////////////////////////////////////////////////// //
   161  
   162  func (s *BashSuite) TestGenerator(c *C) {
   163  	completion := Generate(genTestUsageInfo(true), "test", "[sr]pm")
   164  	c.Assert(completion, Equals, _RESULT_FILES)
   165  
   166  	completion = Generate(genTestUsageInfo(false), "test")
   167  	c.Assert(completion, Equals, _RESULT_NO_FILES)
   168  }
   169  
   170  func (s *BashSuite) TestAuxi(c *C) {
   171  	info := usage.NewInfo("")
   172  
   173  	c.Assert(isCommandHandlersRequired(info), Equals, false)
   174  	c.Assert(genCommandsHandlers(info), Equals, "")
   175  }
   176  
   177  // ////////////////////////////////////////////////////////////////////////////////// //
   178  
   179  func genTestUsageInfo(withFiles bool) *usage.Info {
   180  	var info *usage.Info
   181  
   182  	if withFiles {
   183  		info = usage.NewInfo("", "spec-file")
   184  	} else {
   185  		info = usage.NewInfo("")
   186  	}
   187  
   188  	info.AddCommand("print", "Print command")
   189  	info.AddCommand("clean", "Clean command")
   190  
   191  	info.AddOption("a:option-aaa", "Test option A")
   192  	info.AddOption("b:option-bbb", "Test option B", "?bbb")
   193  	info.AddOption("option-ccc", "Test option C")
   194  
   195  	info.AddOption("d:option-ddd", "Test option D")
   196  	info.AddOption("e:option-eee", "Test option E")
   197  
   198  	info.BoundOptions("clean", "d:option-ddd", "e:option-eee", "unknown")
   199  
   200  	return info
   201  }