github.com/coveo/gotemplate@v2.7.7+incompatible/collections/tests/integrated_data_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/coveo/gotemplate/collections"
     9  	impl "github.com/coveo/gotemplate/collections/implementation"
    10  	"github.com/coveo/gotemplate/hcl"
    11  	"github.com/coveo/gotemplate/json"
    12  	"github.com/coveo/gotemplate/yaml"
    13  )
    14  
    15  type dictionary = map[string]interface{}
    16  
    17  var hclHelper = hcl.DictionaryHelper
    18  var yamlHelper = yaml.DictionaryHelper
    19  var jsonHelper = json.DictionaryHelper
    20  var genHelper = impl.DictionaryHelper
    21  
    22  func TestConvertData(t *testing.T) {
    23  	var out1 interface{}
    24  	type args struct {
    25  		data string
    26  		out  interface{}
    27  	}
    28  	tests := []struct {
    29  		name    string
    30  		args    args
    31  		want    interface{}
    32  		wantErr bool
    33  	}{
    34  		{"Simple value", args{"a = 10", &out1}, map[string]interface{}{"a": 10}, false},
    35  		{"YAML", args{"a: 10", &out1}, dictionary{"a": 10}, false},
    36  		{"HCL", args{`a = 10 b = "Foo"`, &out1}, dictionary{"a": 10, "b": "Foo"}, false},
    37  		{"JSON", args{`{ "a": 10, "b": "Foo" }`, &out1}, dictionary{"a": 10, "b": "Foo"}, false},
    38  		{"Flexible", args{`a = 10 b = Foo`, &out1}, dictionary{"a": 10, "b": "Foo"}, false},
    39  		{"No change", args{"NoChange", &out1}, nil, false},
    40  		{"Invalid", args{"a = 'value", &out1}, nil, true},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			if err := collections.ConvertData(tt.args.data, tt.args.out); (err != nil) != tt.wantErr {
    45  				t.Errorf("ConvertData() error = %v, wantErr %v\n%v", err, tt.wantErr, reflect.ValueOf(tt.args.out).Elem())
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestToBash(t *testing.T) {
    52  	type SubStruct struct {
    53  		U int64
    54  		I interface{}
    55  	}
    56  	type a struct {
    57  		private int
    58  		I       int
    59  		F       float64
    60  		S       string
    61  		A       []interface{}
    62  		M       dictionary
    63  		SS      SubStruct
    64  	}
    65  	tests := []struct {
    66  		name string
    67  		args interface{}
    68  		want interface{}
    69  	}{
    70  		{"Struct conversion", a{
    71  			private: 0,
    72  			I:       123,
    73  			F:       1.23,
    74  			S:       "123",
    75  			A:       []interface{}{1, "2"},
    76  			M: dictionary{
    77  				"a": "a",
    78  				"b": 2,
    79  			},
    80  			SS: SubStruct{64, "Foo"},
    81  		}, strings.TrimSpace(collections.UnIndent(`
    82  		declare -a A
    83  		A=(1 2)
    84  		F=1.23
    85  		I=123
    86  		declare -A M
    87  		M=([a]=a [b]=2)
    88  		S=123
    89  		declare -A SS
    90  		SS=([I]=Foo [U]=64)
    91  		`))},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			if got := collections.ToBash(tt.args); !reflect.DeepEqual(got, tt.want) {
    96  				t.Errorf("ToNativeRepresentation()\ngot : %q\nwant: %q", got, tt.want)
    97  			}
    98  		})
    99  	}
   100  }