...

Source file src/github.com/spf13/pflag/printusage_test.go

Documentation: github.com/spf13/pflag

     1  package pflag
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  const expectedOutput = `      --long-form    Some description
    10        --long-form2   Some description
    11                         with multiline
    12    -s, --long-name    Some description
    13    -t, --long-name2   Some description with
    14                         multiline
    15  `
    16  
    17  func setUpPFlagSet(buf io.Writer) *FlagSet {
    18  	f := NewFlagSet("test", ExitOnError)
    19  	f.Bool("long-form", false, "Some description")
    20  	f.Bool("long-form2", false, "Some description\n  with multiline")
    21  	f.BoolP("long-name", "s", false, "Some description")
    22  	f.BoolP("long-name2", "t", false, "Some description with\n  multiline")
    23  	f.SetOutput(buf)
    24  	return f
    25  }
    26  
    27  func TestPrintUsage(t *testing.T) {
    28  	buf := bytes.Buffer{}
    29  	f := setUpPFlagSet(&buf)
    30  	f.PrintDefaults()
    31  	res := buf.String()
    32  	if res != expectedOutput {
    33  		t.Errorf("Expected \n%s \nActual \n%s", expectedOutput, res)
    34  	}
    35  }
    36  
    37  func setUpPFlagSet2(buf io.Writer) *FlagSet {
    38  	f := NewFlagSet("test", ExitOnError)
    39  	f.Bool("long-form", false, "Some description")
    40  	f.Bool("long-form2", false, "Some description\n  with multiline")
    41  	f.BoolP("long-name", "s", false, "Some description")
    42  	f.BoolP("long-name2", "t", false, "Some description with\n  multiline")
    43  	f.StringP("some-very-long-arg", "l", "test", "Some very long description having break the limit")
    44  	f.StringP("other-very-long-arg", "o", "long-default-value", "Some very long description having break the limit")
    45  	f.String("some-very-long-arg2", "very long default value", "Some very long description\nwith line break\nmultiple")
    46  	f.SetOutput(buf)
    47  	return f
    48  }
    49  
    50  const expectedOutput2 = `      --long-form                    Some description
    51        --long-form2                   Some description
    52                                         with multiline
    53    -s, --long-name                    Some description
    54    -t, --long-name2                   Some description with
    55                                         multiline
    56    -o, --other-very-long-arg string   Some very long description having
    57                                       break the limit (default
    58                                       "long-default-value")
    59    -l, --some-very-long-arg string    Some very long description having
    60                                       break the limit (default "test")
    61        --some-very-long-arg2 string   Some very long description
    62                                       with line break
    63                                       multiple (default "very long default
    64                                       value")
    65  `
    66  
    67  func TestPrintUsage_2(t *testing.T) {
    68  	buf := bytes.Buffer{}
    69  	f := setUpPFlagSet2(&buf)
    70  	res := f.FlagUsagesWrapped(80)
    71  	if res != expectedOutput2 {
    72  		t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res)
    73  	}
    74  }
    75  

View as plain text