github.com/theliebeskind/genfig@v0.1.5-alpha/parsers/yaml_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  	complexYaml = `
    14  a: b
    15  c:
    16    d: 1
    17    e: 2
    18  f:
    19    - 2
    20    - "3"
    21    - g	
    22  `
    23  	complexJson = `
    24  {
    25  	"a": "b",
    26  	"c": {
    27  		"d": 1,
    28  		"e": 2
    29  	},
    30  	"f": [
    31  		2,  
    32  		"3",
    33  		"g"
    34  	]
    35  }
    36  `
    37  )
    38  
    39  var (
    40  	complexYamlResult = map[string]interface{}{"a": "b", "c": map[interface{}]interface{}{"d": 1, "e": 2}, "f": []interface{}{2, "3", "g"}}
    41  )
    42  
    43  func Test_Yaml(t *testing.T) {
    44  	type args struct {
    45  		data []byte
    46  	}
    47  	tests := []struct {
    48  		name    string
    49  		args    args
    50  		want    map[string]interface{}
    51  		wantErr bool
    52  	}{
    53  		{"empty data", args{}, nil, true},
    54  		{"invalid data", args{[]byte("foobar´?")}, nil, true},
    55  		{"valid yaml", args{[]byte("a: 1")}, map[string]interface{}{"a": 1}, false},
    56  		{"vaild json", args{[]byte(`{"a": 1}`)}, map[string]interface{}{"a": 1}, false},
    57  		{"complex yaml", args{[]byte(complexYaml)}, complexYamlResult, false},
    58  		{"complex json", args{[]byte(complexJson)}, complexYamlResult, false},
    59  	}
    60  	s := YamlStrategy{}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			got, err := s.Parse(tt.args.data)
    64  			if tt.wantErr {
    65  				require.Error(t, err)
    66  			} else {
    67  				require.NoError(t, err)
    68  			}
    69  			assert.Equal(t, tt.want, got)
    70  		})
    71  	}
    72  }