...

Source file src/github.com/peterbourgon/ff/v3/fftoml/fftoml_test.go

Documentation: github.com/peterbourgon/ff/v3/fftoml

     1  package fftoml_test
     2  
     3  import (
     4  	"flag"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/peterbourgon/ff/v3"
    10  	"github.com/peterbourgon/ff/v3/fftest"
    11  	"github.com/peterbourgon/ff/v3/fftoml"
    12  )
    13  
    14  func TestParser(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	for _, testcase := range []struct {
    18  		name string
    19  		file string
    20  		want fftest.Vars
    21  	}{
    22  		{
    23  			name: "empty input",
    24  			file: "testdata/empty.toml",
    25  			want: fftest.Vars{},
    26  		},
    27  		{
    28  			name: "basic KV pairs",
    29  			file: "testdata/basic.toml",
    30  			want: fftest.Vars{
    31  				S: "s",
    32  				I: 10,
    33  				F: 3.14e10,
    34  				B: true,
    35  				D: 5 * time.Second,
    36  				X: []string{"1", "a", "👍"},
    37  			},
    38  		},
    39  		{
    40  			name: "bad TOML file",
    41  			file: "testdata/bad.toml",
    42  			want: fftest.Vars{WantParseErrorString: "keys cannot contain { character"},
    43  		},
    44  	} {
    45  		t.Run(testcase.name, func(t *testing.T) {
    46  			fs, vars := fftest.Pair()
    47  			vars.ParseError = ff.Parse(fs, []string{},
    48  				ff.WithConfigFile(testcase.file),
    49  				ff.WithConfigFileParser(fftoml.Parser),
    50  			)
    51  			fftest.Compare(t, &testcase.want, vars)
    52  		})
    53  	}
    54  }
    55  
    56  func TestParser_WithTables(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	type fields struct {
    60  		String  string
    61  		Float   float64
    62  		Strings fftest.StringSlice
    63  	}
    64  
    65  	expected := fields{
    66  		String:  "a string",
    67  		Float:   1.23,
    68  		Strings: fftest.StringSlice{"one", "two", "three"},
    69  	}
    70  
    71  	for _, testcase := range []struct {
    72  		name string
    73  		opts []fftoml.Option
    74  		// expectations
    75  		stringKey  string
    76  		floatKey   string
    77  		stringsKey string
    78  	}{
    79  		{
    80  			name:       "defaults",
    81  			stringKey:  "string.key",
    82  			floatKey:   "float.nested.key",
    83  			stringsKey: "strings.nested.key",
    84  		},
    85  		{
    86  			name:       "defaults",
    87  			opts:       []fftoml.Option{fftoml.WithTableDelimiter("-")},
    88  			stringKey:  "string-key",
    89  			floatKey:   "float-nested-key",
    90  			stringsKey: "strings-nested-key",
    91  		},
    92  	} {
    93  		t.Run(testcase.name, func(t *testing.T) {
    94  			var (
    95  				found fields
    96  				fs    = flag.NewFlagSet("fftest", flag.ContinueOnError)
    97  			)
    98  
    99  			fs.StringVar(&found.String, testcase.stringKey, "", "string")
   100  			fs.Float64Var(&found.Float, testcase.floatKey, 0, "float64")
   101  			fs.Var(&found.Strings, testcase.stringsKey, "string slice")
   102  
   103  			if err := ff.Parse(fs, []string{},
   104  				ff.WithConfigFile("testdata/table.toml"),
   105  				ff.WithConfigFileParser(fftoml.New(testcase.opts...).Parse),
   106  			); err != nil {
   107  				t.Fatal(err)
   108  			}
   109  
   110  			if !reflect.DeepEqual(expected, found) {
   111  				t.Errorf(`expected %v, to be %v`, found, expected)
   112  			}
   113  		})
   114  	}
   115  
   116  }
   117  

View as plain text