(t *testing.T)
| 134 | } |
| 135 | |
| 136 | func TestPrintFlagsHTMLShowsDefaultValues(t *testing.T) { |
| 137 | |
| 138 | type TestOptions struct { |
| 139 | Limit int |
| 140 | Template string |
| 141 | Fork bool |
| 142 | NoArchive bool |
| 143 | Topic []string |
| 144 | } |
| 145 | opts := TestOptions{} |
| 146 | |
| 147 | // Int flag should show it |
| 148 | c := &cobra.Command{} |
| 149 | c.Flags().IntVar(&opts.Limit, "limit", 30, "Some limit") |
| 150 | flags := c.NonInheritedFlags() |
| 151 | buf := new(bytes.Buffer) |
| 152 | flags.SetOutput(buf) |
| 153 | |
| 154 | if err := printFlagsHTML(buf, flags); err != nil { |
| 155 | t.Fatalf("printFlagsHTML failed: %s", err.Error()) |
| 156 | } |
| 157 | output := buf.String() |
| 158 | |
| 159 | checkStringContains(t, output, "(default 30)") |
| 160 | |
| 161 | // Bool flag should hide it if default is false |
| 162 | c = &cobra.Command{} |
| 163 | c.Flags().BoolVar(&opts.Fork, "fork", false, "Show only forks") |
| 164 | |
| 165 | flags = c.NonInheritedFlags() |
| 166 | buf = new(bytes.Buffer) |
| 167 | flags.SetOutput(buf) |
| 168 | |
| 169 | if err := printFlagsHTML(buf, flags); err != nil { |
| 170 | t.Fatalf("printFlagsHTML failed: %s", err.Error()) |
| 171 | } |
| 172 | output = buf.String() |
| 173 | |
| 174 | checkStringOmits(t, output, "(default ") |
| 175 | |
| 176 | // Bool flag should show it if default is true |
| 177 | c = &cobra.Command{} |
| 178 | c.Flags().BoolVar(&opts.NoArchive, "no-archived", true, "Hide archived") |
| 179 | flags = c.NonInheritedFlags() |
| 180 | buf = new(bytes.Buffer) |
| 181 | flags.SetOutput(buf) |
| 182 | |
| 183 | if err := printFlagsHTML(buf, flags); err != nil { |
| 184 | t.Fatalf("printFlagsHTML failed: %s", err.Error()) |
| 185 | } |
| 186 | output = buf.String() |
| 187 | |
| 188 | checkStringContains(t, output, "(default true)") |
| 189 | |
| 190 | // String flag should show it if default is not an empty string |
| 191 | c = &cobra.Command{} |
| 192 | c.Flags().StringVar(&opts.Template, "template", "T1", "Some template") |
| 193 | flags = c.NonInheritedFlags() |
nothing calls this directly
no test coverage detected