github.com/theliebeskind/genfig@v0.1.5-alpha/parsers/toml_test.go (about)

     1  package parsers_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	. "github.com/theliebeskind/genfig/parsers"
    10  )
    11  
    12  const (
    13  	complexToml = `
    14  	a = "b"
    15  	f = [
    16  		"2",
    17  		"3",
    18  		"g"
    19  	]
    20  	
    21  	[c]
    22  	d = 1
    23  	e = 2
    24  `
    25  )
    26  
    27  var (
    28  	complexTomlResult = map[string]interface{}{"a": "b", "c": map[string]interface{}{"d": int64(1), "e": int64(2)}, "f": []interface{}{"2", "3", "g"}}
    29  )
    30  
    31  func Test_Toml(t *testing.T) {
    32  	type args struct {
    33  		data []byte
    34  	}
    35  	tests := []struct {
    36  		name    string
    37  		args    args
    38  		want    map[string]interface{}
    39  		wantErr bool
    40  	}{
    41  		{"empty data", args{}, nil, true},
    42  		{"invalid data", args{[]byte("foobar´?")}, nil, true},
    43  		{"valid toml", args{[]byte("a=1")}, map[string]interface{}{"a": int64(1)}, false},
    44  		{"complex toml", args{[]byte(complexToml)}, complexTomlResult, false},
    45  	}
    46  	s := TomlStrategy{}
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			got, err := s.Parse(tt.args.data)
    50  			if tt.wantErr {
    51  				require.Error(t, err)
    52  			} else {
    53  				require.NoError(t, err)
    54  			}
    55  			assert.Equal(t, tt.want, got)
    56  		})
    57  	}
    58  }