...

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

Documentation: github.com/urfave/cli/v2

     1  //go:build !urfave_cli_no_docs
     2  // +build !urfave_cli_no_docs
     3  
     4  package cli
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  )
    10  
    11  func TestToMarkdownFull(t *testing.T) {
    12  	// Given
    13  	app := testApp()
    14  
    15  	// When
    16  	res, err := app.ToMarkdown()
    17  
    18  	// Then
    19  	expect(t, err, nil)
    20  	expectFileContent(t, "testdata/expected-doc-full.md", res)
    21  }
    22  
    23  func TestToMarkdownNoFlags(t *testing.T) {
    24  	// Given
    25  	app := testApp()
    26  	app.Flags = nil
    27  
    28  	// When
    29  	res, err := app.ToMarkdown()
    30  
    31  	// Then
    32  	expect(t, err, nil)
    33  	expectFileContent(t, "testdata/expected-doc-no-flags.md", res)
    34  }
    35  
    36  func TestToMarkdownNoCommands(t *testing.T) {
    37  	// Given
    38  	app := testApp()
    39  	app.Commands = nil
    40  
    41  	// When
    42  	res, err := app.ToMarkdown()
    43  
    44  	// Then
    45  	expect(t, err, nil)
    46  	expectFileContent(t, "testdata/expected-doc-no-commands.md", res)
    47  }
    48  
    49  func TestToMarkdownNoAuthors(t *testing.T) {
    50  	// Given
    51  	app := testApp()
    52  	app.Authors = []*Author{}
    53  
    54  	// When
    55  	res, err := app.ToMarkdown()
    56  
    57  	// Then
    58  	expect(t, err, nil)
    59  	expectFileContent(t, "testdata/expected-doc-no-authors.md", res)
    60  }
    61  
    62  func TestToMarkdownNoUsageText(t *testing.T) {
    63  	// Given
    64  	app := testApp()
    65  	app.UsageText = ""
    66  
    67  	// When
    68  	res, err := app.ToMarkdown()
    69  
    70  	// Then
    71  	expect(t, err, nil)
    72  	expectFileContent(t, "testdata/expected-doc-no-usagetext.md", res)
    73  }
    74  
    75  func TestToMan(t *testing.T) {
    76  	// Given
    77  	app := testApp()
    78  
    79  	// When
    80  	res, err := app.ToMan()
    81  
    82  	// Then
    83  	expect(t, err, nil)
    84  	expectFileContent(t, "testdata/expected-doc-full.man", res)
    85  }
    86  
    87  func TestToManParseError(t *testing.T) {
    88  	// Given
    89  	app := testApp()
    90  
    91  	// When
    92  	// temporarily change the global variable for testing
    93  	tmp := MarkdownDocTemplate
    94  	MarkdownDocTemplate = `{{ .App.Name`
    95  	_, err := app.ToMan()
    96  	MarkdownDocTemplate = tmp
    97  
    98  	// Then
    99  	expect(t, err, errors.New(`template: cli:1: unclosed action`))
   100  }
   101  
   102  func TestToManWithSection(t *testing.T) {
   103  	// Given
   104  	app := testApp()
   105  
   106  	// When
   107  	res, err := app.ToManWithSection(8)
   108  
   109  	// Then
   110  	expect(t, err, nil)
   111  	expectFileContent(t, "testdata/expected-doc-full.man", res)
   112  }
   113  
   114  func Test_prepareUsageText(t *testing.T) {
   115  	t.Run("no UsageText provided", func(t *testing.T) {
   116  		// Given
   117  		cmd := Command{}
   118  
   119  		// When
   120  		res := prepareUsageText(&cmd)
   121  
   122  		// Then
   123  		expect(t, res, "")
   124  	})
   125  
   126  	t.Run("single line UsageText", func(t *testing.T) {
   127  		// Given
   128  		cmd := Command{UsageText: "Single line usage text"}
   129  
   130  		// When
   131  		res := prepareUsageText(&cmd)
   132  
   133  		// Then
   134  		expect(t, res, ">Single line usage text\n")
   135  	})
   136  
   137  	t.Run("multiline UsageText", func(t *testing.T) {
   138  		// Given
   139  		cmd := Command{
   140  			UsageText: `
   141  Usage for the usage text
   142  - Should be a part of the same code block
   143  `,
   144  		}
   145  
   146  		// When
   147  		res := prepareUsageText(&cmd)
   148  
   149  		// Then
   150  		test := `    Usage for the usage text
   151      - Should be a part of the same code block
   152  `
   153  		expect(t, res, test)
   154  	})
   155  
   156  	t.Run("multiline UsageText has formatted embedded markdown", func(t *testing.T) {
   157  		// Given
   158  		cmd := Command{
   159  			UsageText: `
   160  Usage for the usage text
   161  
   162  ` + "```" + `
   163  func() { ... }
   164  ` + "```" + `
   165  
   166  Should be a part of the same code block
   167  `,
   168  		}
   169  
   170  		// When
   171  		res := prepareUsageText(&cmd)
   172  
   173  		// Then
   174  		test := `    Usage for the usage text
   175      
   176      ` + "```" + `
   177      func() { ... }
   178      ` + "```" + `
   179      
   180      Should be a part of the same code block
   181  `
   182  		expect(t, res, test)
   183  	})
   184  }
   185  
   186  func Test_prepareUsage(t *testing.T) {
   187  	t.Run("no Usage provided", func(t *testing.T) {
   188  		// Given
   189  		cmd := Command{}
   190  
   191  		// When
   192  		res := prepareUsage(&cmd, "")
   193  
   194  		// Then
   195  		expect(t, res, "")
   196  	})
   197  
   198  	t.Run("simple Usage", func(t *testing.T) {
   199  		// Given
   200  		cmd := Command{Usage: "simple usage text"}
   201  
   202  		// When
   203  		res := prepareUsage(&cmd, "")
   204  
   205  		// Then
   206  		expect(t, res, cmd.Usage+"\n")
   207  	})
   208  
   209  	t.Run("simple Usage with UsageText", func(t *testing.T) {
   210  		// Given
   211  		cmd := Command{Usage: "simple usage text"}
   212  
   213  		// When
   214  		res := prepareUsage(&cmd, "a non-empty string")
   215  
   216  		// Then
   217  		expect(t, res, cmd.Usage+"\n\n")
   218  	})
   219  }
   220  

View as plain text