github.com/coveo/gotemplate@v2.7.7+incompatible/template/extra_data_test.go (about)

     1  package template
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/coveo/gotemplate/hcl"
     8  	"github.com/coveo/gotemplate/json"
     9  	"github.com/coveo/gotemplate/yaml"
    10  )
    11  
    12  func Test_Data(t *testing.T) {
    13  	t.Parallel()
    14  	template := MustNewTemplate("", nil, "", nil)
    15  	tests := []struct {
    16  		name    string
    17  		test    string
    18  		want    interface{}
    19  		wantErr bool
    20  	}{
    21  		{"Simple hcl", "a = 1", hcl.Dictionary{"a": 1}, false},
    22  		{"Simple yaml", "b: 2", yaml.Dictionary{"b": 2}, false},
    23  		{"Simple json", `{"c": 3}`, json.Dictionary{"c": 3}, false},
    24  		{"Simple string", "string", "string", false},
    25  		{"Error", "a = '", nil, true},
    26  	}
    27  	for _, tt := range tests {
    28  		t.Run(tt.name, func(t *testing.T) {
    29  			got, err := template.dataConverter(tt.test)
    30  			if (err != nil) != tt.wantErr {
    31  				t.Errorf("Template.fromData() error = %v, wantErr %v", err, tt.wantErr)
    32  				return
    33  			}
    34  			if !reflect.DeepEqual(got, tt.want) {
    35  				t.Errorf("Template.fromData()\ngot : %[1]v (%[1]T)\nwant: %[2]v (%[2]T)", got, tt.want)
    36  			}
    37  		})
    38  	}
    39  }
    40  
    41  func Test_YAML(t *testing.T) {
    42  	t.Parallel()
    43  	template := MustNewTemplate("", nil, "", nil)
    44  	tests := []struct {
    45  		name    string
    46  		test    string
    47  		want    interface{}
    48  		wantErr bool
    49  	}{
    50  		{"Simple yaml", "b: 2", yaml.Dictionary{"b": 2}, false},
    51  		{"Simple quoted string", `"string"`, "string", false},
    52  		{"Simple string", "string", "string", false},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := template.yamlConverter(tt.test)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("Template.fromData() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  			if !reflect.DeepEqual(got, tt.want) {
    62  				t.Errorf("Template.fromData() = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func Test_HCL(t *testing.T) {
    69  	t.Parallel()
    70  	template := MustNewTemplate("", nil, "", nil)
    71  	tests := []struct {
    72  		name    string
    73  		test    string
    74  		want    interface{}
    75  		wantErr bool
    76  	}{
    77  		{"Simple hcl", "a = 1", hcl.Dictionary{"a": 1}, false},
    78  		{"Simple string", `"string"`, "string", false},
    79  		{"Simple string", "string", nil, true},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			got, err := template.hclConverter(tt.test)
    84  			if (err != nil) != tt.wantErr {
    85  				t.Errorf("Template.fromData() error = %v, wantErr %v", err, tt.wantErr)
    86  				return
    87  			}
    88  			if !reflect.DeepEqual(got, tt.want) {
    89  				t.Errorf("Template.fromData() = %v, want %v", got, tt.want)
    90  			}
    91  		})
    92  	}
    93  }