github.com/data-DOG/godog@v0.7.9/flags_test.go (about)

     1  package godog
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/DATA-DOG/godog/colors"
    11  )
    12  
    13  func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) {
    14  	var opt Options
    15  	flags := FlagSet(&opt)
    16  	if err := flags.Parse([]string{"--random"}); err != nil {
    17  		t.Fatalf("unable to parse flags: %v", err)
    18  	}
    19  
    20  	if opt.Randomize <= 0 {
    21  		t.Fatal("should have generated random seed")
    22  	}
    23  }
    24  
    25  func TestFlagsShouldRandomizeByGivenSeed(t *testing.T) {
    26  	var opt Options
    27  	flags := FlagSet(&opt)
    28  	if err := flags.Parse([]string{"--random=123"}); err != nil {
    29  		t.Fatalf("unable to parse flags: %v", err)
    30  	}
    31  
    32  	if opt.Randomize != 123 {
    33  		t.Fatalf("expected random seed to be: 123, but it was: %d", opt.Randomize)
    34  	}
    35  }
    36  
    37  func TestFlagsShouldParseFormat(t *testing.T) {
    38  	cases := map[string][]string{
    39  		"pretty":   {},
    40  		"progress": {"-f", "progress"},
    41  		"junit":    {"-f=junit"},
    42  		"custom":   {"--format", "custom"},
    43  		"cust":     {"--format=cust"},
    44  	}
    45  
    46  	for format, args := range cases {
    47  		var opt Options
    48  		flags := FlagSet(&opt)
    49  		if err := flags.Parse(args); err != nil {
    50  			t.Fatalf("unable to parse flags: %v", err)
    51  		}
    52  
    53  		if opt.Format != format {
    54  			t.Fatalf("expected format: %s, but it was: %s", format, opt.Format)
    55  		}
    56  	}
    57  }
    58  
    59  func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
    60  	var buf bytes.Buffer
    61  	output := colors.Uncolored(&buf)
    62  
    63  	// register some custom formatter
    64  	Format("custom", "custom format description", junitFunc)
    65  
    66  	var opt Options
    67  	flags := FlagSet(&opt)
    68  	usage(flags, output)()
    69  
    70  	out := buf.String()
    71  
    72  	for name, desc := range AvailableFormatters() {
    73  		match := fmt.Sprintf("%s: %s\n", name, desc)
    74  		if idx := strings.Index(out, match); idx == -1 {
    75  			t.Fatalf("could not locate format: %s description in flag usage", name)
    76  		}
    77  	}
    78  }
    79  
    80  func TestBindFlagsShouldRespectFlagDefaults(t *testing.T) {
    81  	opts := Options{}
    82  
    83  	BindFlags("flagDefaults.", flag.CommandLine, &opts)
    84  
    85  	if opts.Format != "pretty" {
    86  		t.Fatalf("expected Format: pretty, but it was: %s", opts.Format)
    87  	}
    88  	if opts.Tags != "" {
    89  		t.Fatalf("expected Tags: '', but it was: %s", opts.Tags)
    90  	}
    91  	if opts.Concurrency != 1 {
    92  		t.Fatalf("expected Concurrency: 1, but it was: %d", opts.Concurrency)
    93  	}
    94  	if opts.ShowStepDefinitions {
    95  		t.Fatalf("expected ShowStepDefinitions: false, but it was: %t", opts.ShowStepDefinitions)
    96  	}
    97  	if opts.StopOnFailure {
    98  		t.Fatalf("expected StopOnFailure: false, but it was: %t", opts.StopOnFailure)
    99  	}
   100  	if opts.Strict {
   101  		t.Fatalf("expected Strict: false, but it was: %t", opts.Strict)
   102  	}
   103  	if opts.NoColors {
   104  		t.Fatalf("expected NoColors: false, but it was: %t", opts.NoColors)
   105  	}
   106  	if opts.Randomize != 0 {
   107  		t.Fatalf("expected Randomize: 0, but it was: %d", opts.Randomize)
   108  	}
   109  }
   110  
   111  func TestBindFlagsShouldRespectOptDefaults(t *testing.T) {
   112  	opts := Options{
   113  		Format:              "progress",
   114  		Tags:                "test",
   115  		Concurrency:         2,
   116  		ShowStepDefinitions: true,
   117  		StopOnFailure:       true,
   118  		Strict:              true,
   119  		NoColors:            true,
   120  		Randomize:           int64(7),
   121  	}
   122  
   123  	BindFlags("optDefaults.", flag.CommandLine, &opts)
   124  
   125  	if opts.Format != "progress" {
   126  		t.Fatalf("expected Format: progress, but it was: %s", opts.Format)
   127  	}
   128  	if opts.Tags != "test" {
   129  		t.Fatalf("expected Tags: 'test', but it was: %s", opts.Tags)
   130  	}
   131  	if opts.Concurrency != 2 {
   132  		t.Fatalf("expected Concurrency: 2, but it was: %d", opts.Concurrency)
   133  	}
   134  	if !opts.ShowStepDefinitions {
   135  		t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
   136  	}
   137  	if !opts.StopOnFailure {
   138  		t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
   139  	}
   140  	if !opts.Strict {
   141  		t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
   142  	}
   143  	if !opts.NoColors {
   144  		t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
   145  	}
   146  	if opts.Randomize != 7 {
   147  		t.Fatalf("expected Randomize: 7, but it was: %d", opts.Randomize)
   148  	}
   149  }
   150  
   151  func TestBindFlagsShouldRespectFlagOverrides(t *testing.T) {
   152  	opts := Options{
   153  		Format:              "progress",
   154  		Tags:                "test",
   155  		Concurrency:         2,
   156  		ShowStepDefinitions: true,
   157  		StopOnFailure:       true,
   158  		Strict:              true,
   159  		NoColors:            true,
   160  		Randomize:           11,
   161  	}
   162  	flagSet := flag.FlagSet{}
   163  
   164  	BindFlags("optOverrides.", &flagSet, &opts)
   165  
   166  	flagSet.Parse([]string{
   167  		"--optOverrides.format=junit",
   168  		"--optOverrides.tags=test2",
   169  		"--optOverrides.concurrency=3",
   170  		"--optOverrides.definitions=false",
   171  		"--optOverrides.stop-on-failure=false",
   172  		"--optOverrides.strict=false",
   173  		"--optOverrides.no-colors=false",
   174  		"--optOverrides.random=2",
   175  	})
   176  
   177  	if opts.Format != "junit" {
   178  		t.Fatalf("expected Format: junit, but it was: %s", opts.Format)
   179  	}
   180  	if opts.Tags != "test2" {
   181  		t.Fatalf("expected Tags: 'test2', but it was: %s", opts.Tags)
   182  	}
   183  	if opts.Concurrency != 3 {
   184  		t.Fatalf("expected Concurrency: 3, but it was: %d", opts.Concurrency)
   185  	}
   186  	if opts.ShowStepDefinitions {
   187  		t.Fatalf("expected ShowStepDefinitions: true, but it was: %t", opts.ShowStepDefinitions)
   188  	}
   189  	if opts.StopOnFailure {
   190  		t.Fatalf("expected StopOnFailure: true, but it was: %t", opts.StopOnFailure)
   191  	}
   192  	if opts.Strict {
   193  		t.Fatalf("expected Strict: true, but it was: %t", opts.Strict)
   194  	}
   195  	if opts.NoColors {
   196  		t.Fatalf("expected NoColors: true, but it was: %t", opts.NoColors)
   197  	}
   198  	if opts.Randomize != 2 {
   199  		t.Fatalf("expected Randomize: 2, but it was: %d", opts.Randomize)
   200  	}
   201  }