1package cli 2 3import ( 4 "io/ioutil" 5 "testing" 6) 7 8func testApp() *App { 9 app := NewApp() 10 app.Name = "greet" 11 app.Flags = []Flag{ 12 StringFlag{ 13 Name: "socket, s", 14 Usage: "some 'usage' text", 15 Value: "value", 16 TakesFile: true, 17 }, 18 StringFlag{Name: "flag, fl, f"}, 19 BoolFlag{ 20 Name: "another-flag, b", 21 Usage: "another usage text", 22 }, 23 } 24 app.Commands = []Command{{ 25 Aliases: []string{"c"}, 26 Flags: []Flag{ 27 StringFlag{ 28 Name: "flag, fl, f", 29 TakesFile: true, 30 }, 31 BoolFlag{ 32 Name: "another-flag, b", 33 Usage: "another usage text", 34 }, 35 }, 36 Name: "config", 37 Usage: "another usage test", 38 Subcommands: []Command{{ 39 Aliases: []string{"s", "ss"}, 40 Flags: []Flag{ 41 StringFlag{Name: "sub-flag, sub-fl, s"}, 42 BoolFlag{ 43 Name: "sub-command-flag, s", 44 Usage: "some usage text", 45 }, 46 }, 47 Name: "sub-config", 48 Usage: "another usage test", 49 }}, 50 }, { 51 Aliases: []string{"i", "in"}, 52 Name: "info", 53 Usage: "retrieve generic information", 54 }, { 55 Name: "some-command", 56 }, { 57 Name: "hidden-command", 58 Hidden: true, 59 }} 60 app.UsageText = "app [first_arg] [second_arg]" 61 app.Usage = "Some app" 62 app.Author = "Harrison" 63 app.Email = "harrison@lolwut.com" 64 app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}} 65 return app 66} 67 68func expectFileContent(t *testing.T, file, expected string) { 69 data, err := ioutil.ReadFile(file) 70 expect(t, err, nil) 71 expect(t, string(data), expected) 72} 73 74func TestToMarkdownFull(t *testing.T) { 75 // Given 76 app := testApp() 77 78 // When 79 res, err := app.ToMarkdown() 80 81 // Then 82 expect(t, err, nil) 83 expectFileContent(t, "testdata/expected-doc-full.md", res) 84} 85 86func TestToMarkdownNoFlags(t *testing.T) { 87 // Given 88 app := testApp() 89 app.Flags = nil 90 91 // When 92 res, err := app.ToMarkdown() 93 94 // Then 95 expect(t, err, nil) 96 expectFileContent(t, "testdata/expected-doc-no-flags.md", res) 97} 98 99func TestToMarkdownNoCommands(t *testing.T) { 100 // Given 101 app := testApp() 102 app.Commands = nil 103 104 // When 105 res, err := app.ToMarkdown() 106 107 // Then 108 expect(t, err, nil) 109 expectFileContent(t, "testdata/expected-doc-no-commands.md", res) 110} 111 112func TestToMan(t *testing.T) { 113 // Given 114 app := testApp() 115 116 // When 117 res, err := app.ToMan() 118 119 // Then 120 expect(t, err, nil) 121 expectFileContent(t, "testdata/expected-doc-full.man", res) 122} 123