github.com/maps90/godog@v0.7.5-0.20170923143419-0093943021d4/flags_test.go (about)

     1  package godog
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/DATA-DOG/godog/colors"
    10  )
    11  
    12  func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) {
    13  	var opt Options
    14  	flags := FlagSet(&opt)
    15  	if err := flags.Parse([]string{"--random"}); err != nil {
    16  		t.Fatalf("unable to parse flags: %v", err)
    17  	}
    18  
    19  	if opt.Randomize <= 0 {
    20  		t.Fatal("should have generated random seed")
    21  	}
    22  }
    23  
    24  func TestFlagsShouldRandomizeByGivenSeed(t *testing.T) {
    25  	var opt Options
    26  	flags := FlagSet(&opt)
    27  	if err := flags.Parse([]string{"--random=123"}); err != nil {
    28  		t.Fatalf("unable to parse flags: %v", err)
    29  	}
    30  
    31  	if opt.Randomize != 123 {
    32  		t.Fatalf("expected random seed to be: 123, but it was: %d", opt.Randomize)
    33  	}
    34  }
    35  
    36  func TestFlagsShouldParseFormat(t *testing.T) {
    37  	cases := map[string][]string{
    38  		"pretty":   {},
    39  		"progress": {"-f", "progress"},
    40  		"junit":    {"-f=junit"},
    41  		"custom":   {"--format", "custom"},
    42  		"cust":     {"--format=cust"},
    43  	}
    44  
    45  	for format, args := range cases {
    46  		var opt Options
    47  		flags := FlagSet(&opt)
    48  		if err := flags.Parse(args); err != nil {
    49  			t.Fatalf("unable to parse flags: %v", err)
    50  		}
    51  
    52  		if opt.Format != format {
    53  			t.Fatalf("expected format: %s, but it was: %s", format, opt.Format)
    54  		}
    55  	}
    56  }
    57  
    58  func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
    59  	var buf bytes.Buffer
    60  	output := colors.Uncolored(&buf)
    61  
    62  	// register some custom formatter
    63  	Format("custom", "custom format description", junitFunc)
    64  
    65  	var opt Options
    66  	flags := FlagSet(&opt)
    67  	usage(flags, output)()
    68  
    69  	out := buf.String()
    70  
    71  	for name, desc := range AvailableFormatters() {
    72  		match := fmt.Sprintf("%s: %s\n", name, desc)
    73  		if idx := strings.Index(out, match); idx == -1 {
    74  			t.Fatalf("could not locate format: %s description in flag usage", name)
    75  		}
    76  	}
    77  }