code.cestus.io/tools/fabricator@v0.4.3/pkg/ff/fftoml/fftoml_test.go (about)

     1  package fftoml_test
     2  
     3  import (
     4  	"flag"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"code.cestus.io/tools/fabricator/pkg/ff"
    10  	"code.cestus.io/tools/fabricator/pkg/ff/fftest"
    11  	"code.cestus.io/tools/fabricator/pkg/ff/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  			if err := fftest.Compare(&testcase.want, vars); err != nil {
    52  				t.Fatal(err)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestParser_WithTables(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	type fields struct {
    62  		String  string
    63  		Float   float64
    64  		Strings fftest.StringSlice
    65  	}
    66  
    67  	expected := fields{
    68  		String:  "a string",
    69  		Float:   1.23,
    70  		Strings: fftest.StringSlice{"one", "two", "three"},
    71  	}
    72  
    73  	for _, testcase := range []struct {
    74  		name string
    75  		opts []fftoml.Option
    76  		// expectations
    77  		stringKey  string
    78  		floatKey   string
    79  		stringsKey string
    80  	}{
    81  		{
    82  			name:       "defaults",
    83  			stringKey:  "string.key",
    84  			floatKey:   "float.nested.key",
    85  			stringsKey: "strings.nested.key",
    86  		},
    87  		{
    88  			name:       "defaults",
    89  			opts:       []fftoml.Option{fftoml.WithTableDelimiter("-")},
    90  			stringKey:  "string-key",
    91  			floatKey:   "float-nested-key",
    92  			stringsKey: "strings-nested-key",
    93  		},
    94  	} {
    95  		t.Run(testcase.name, func(t *testing.T) {
    96  			var (
    97  				found fields
    98  				fs    = flag.NewFlagSet("fftest", flag.ContinueOnError)
    99  			)
   100  
   101  			fs.StringVar(&found.String, testcase.stringKey, "", "string")
   102  			fs.Float64Var(&found.Float, testcase.floatKey, 0, "float64")
   103  			fs.Var(&found.Strings, testcase.stringsKey, "string slice")
   104  
   105  			if err := ff.Parse(fs, []string{},
   106  				ff.WithConfigFile("testdata/table.toml"),
   107  				ff.WithConfigFileParser(fftoml.New(testcase.opts...).Parse),
   108  			); err != nil {
   109  				t.Fatal(err)
   110  			}
   111  
   112  			if !reflect.DeepEqual(expected, found) {
   113  				t.Errorf(`expected %v, to be %v`, found, expected)
   114  			}
   115  		})
   116  	}
   117  
   118  }