...

Source file src/github.com/urfave/cli/v2/suggestions_test.go

Documentation: github.com/urfave/cli/v2

     1  //go:build !urfave_cli_no_suggest
     2  // +build !urfave_cli_no_suggest
     3  
     4  package cli
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestSuggestFlag(t *testing.T) {
    13  	// Given
    14  	app := testApp()
    15  
    16  	for _, testCase := range []struct {
    17  		provided, expected string
    18  	}{
    19  		{"", ""},
    20  		{"a", "--another-flag"},
    21  		{"hlp", "--help"},
    22  		{"k", ""},
    23  		{"s", "-s"},
    24  	} {
    25  		// When
    26  		res := suggestFlag(app.Flags, testCase.provided, false)
    27  
    28  		// Then
    29  		expect(t, res, testCase.expected)
    30  	}
    31  }
    32  
    33  func TestSuggestFlagHideHelp(t *testing.T) {
    34  	// Given
    35  	app := testApp()
    36  
    37  	// When
    38  	res := suggestFlag(app.Flags, "hlp", true)
    39  
    40  	// Then
    41  	expect(t, res, "--fl")
    42  }
    43  
    44  func TestSuggestFlagFromError(t *testing.T) {
    45  	// Given
    46  	app := testApp()
    47  
    48  	for _, testCase := range []struct {
    49  		command, provided, expected string
    50  	}{
    51  		{"", "hel", "--help"},
    52  		{"", "soccer", "--socket"},
    53  		{"config", "anot", "--another-flag"},
    54  	} {
    55  		// When
    56  		res, _ := app.suggestFlagFromError(
    57  			errors.New(providedButNotDefinedErrMsg+testCase.provided),
    58  			testCase.command,
    59  		)
    60  
    61  		// Then
    62  		expect(t, res, fmt.Sprintf(SuggestDidYouMeanTemplate+"\n\n", testCase.expected))
    63  	}
    64  }
    65  
    66  func TestSuggestFlagFromErrorWrongError(t *testing.T) {
    67  	// Given
    68  	app := testApp()
    69  
    70  	// When
    71  	_, err := app.suggestFlagFromError(errors.New("invalid"), "")
    72  
    73  	// Then
    74  	expect(t, true, err != nil)
    75  }
    76  
    77  func TestSuggestFlagFromErrorWrongCommand(t *testing.T) {
    78  	// Given
    79  	app := testApp()
    80  
    81  	// When
    82  	_, err := app.suggestFlagFromError(
    83  		errors.New(providedButNotDefinedErrMsg+"flag"),
    84  		"invalid",
    85  	)
    86  
    87  	// Then
    88  	expect(t, true, err != nil)
    89  }
    90  
    91  func TestSuggestFlagFromErrorNoSuggestion(t *testing.T) {
    92  	// Given
    93  	app := testApp()
    94  
    95  	// When
    96  	_, err := app.suggestFlagFromError(
    97  		errors.New(providedButNotDefinedErrMsg+""),
    98  		"",
    99  	)
   100  
   101  	// Then
   102  	expect(t, true, err != nil)
   103  }
   104  
   105  func TestSuggestCommand(t *testing.T) {
   106  	// Given
   107  	app := testApp()
   108  
   109  	for _, testCase := range []struct {
   110  		provided, expected string
   111  	}{
   112  		{"", ""},
   113  		{"conf", "config"},
   114  		{"i", "i"},
   115  		{"information", "info"},
   116  		{"not-existing", "info"},
   117  	} {
   118  		// When
   119  		res := suggestCommand(app.Commands, testCase.provided)
   120  
   121  		// Then
   122  		expect(t, res, fmt.Sprintf(SuggestDidYouMeanTemplate, testCase.expected))
   123  	}
   124  }
   125  
   126  func ExampleApp_Suggest() {
   127  	app := &App{
   128  		Name:                  "greet",
   129  		Suggest:               true,
   130  		HideHelp:              false,
   131  		HideHelpCommand:       true,
   132  		CustomAppHelpTemplate: "(this space intentionally left blank)\n",
   133  		Flags: []Flag{
   134  			&StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
   135  		},
   136  		Action: func(cCtx *Context) error {
   137  			fmt.Printf("Hello %v\n", cCtx.String("name"))
   138  			return nil
   139  		},
   140  	}
   141  
   142  	if err := app.Run([]string{"greet", "--nema", "chipmunk"}); err == nil {
   143  		return
   144  	}
   145  	// Output:
   146  	// Incorrect Usage: flag provided but not defined: -nema
   147  	//
   148  	// Did you mean "--name"?
   149  	//
   150  	// (this space intentionally left blank)
   151  }
   152  
   153  func ExampleApp_Suggest_command() {
   154  	app := &App{
   155  		Name:                  "greet",
   156  		Suggest:               true,
   157  		HideHelpCommand:       true,
   158  		CustomAppHelpTemplate: "(this space intentionally left blank)\n",
   159  		Flags: []Flag{
   160  			&StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
   161  		},
   162  		Action: func(cCtx *Context) error {
   163  			fmt.Printf("Hello %v\n", cCtx.String("name"))
   164  			return nil
   165  		},
   166  		Commands: []*Command{
   167  			{
   168  				Name:               "neighbors",
   169  				HideHelp:           false,
   170  				CustomHelpTemplate: "(this space intentionally left blank)\n",
   171  				Flags: []Flag{
   172  					&BoolFlag{Name: "smiling"},
   173  				},
   174  				Action: func(cCtx *Context) error {
   175  					if cCtx.Bool("smiling") {
   176  						fmt.Println("😀")
   177  					}
   178  					fmt.Println("Hello, neighbors")
   179  					return nil
   180  				},
   181  			},
   182  		},
   183  	}
   184  
   185  	if err := app.Run([]string{"greet", "neighbors", "--sliming"}); err == nil {
   186  		return
   187  	}
   188  	// Output:
   189  	// Incorrect Usage: flag provided but not defined: -sliming
   190  	//
   191  	// Did you mean "--smiling"?
   192  	//
   193  	// (this space intentionally left blank)
   194  }
   195  

View as plain text