github.com/coveo/gotemplate@v2.7.7+incompatible/json/json_test.go (about)

     1  package json
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func Test_list_String(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		l    jsonList
    13  		want string
    14  	}{
    15  		{"Nil", nil, "null"},
    16  		{"Empty list", jsonList{}, "[]"},
    17  		{"List of int", jsonList{1, 2, 3}, "[1,2,3]"},
    18  		{"List of string", strFixture, `["Hello","World,","I'm","Foo","Bar!"]`},
    19  	}
    20  	for _, tt := range tests {
    21  		t.Run(tt.name, func(t *testing.T) {
    22  			if got := tt.l.String(); got != tt.want {
    23  				t.Errorf("jsonList.String() = %v, want %v", got, tt.want)
    24  			}
    25  		})
    26  	}
    27  }
    28  
    29  func Test_dict_String(t *testing.T) {
    30  	tests := []struct {
    31  		name string
    32  		d    jsonDict
    33  		want string
    34  	}{
    35  		{"nil", nil, "null"},
    36  		{"Empty dict", jsonDict{}, "{}"},
    37  		{"Map", dictFixture, `{"float":1.23,"int":123,"list":[1,"two"],"listInt":[1,2,3],"map":{"sub1":1,"sub2":"two"},"mapInt":{"1":1,"2":"two"},"string":"Foo bar"}`},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			if got := tt.d.String(); got != tt.want {
    42  				t.Errorf("jsonList.String():\n  %v\n  %v", got, tt.want)
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  func TestUnmarshal(t *testing.T) {
    49  	tests := []struct {
    50  		name    string
    51  		json    string
    52  		want    interface{}
    53  		wantErr bool
    54  	}{
    55  		{"Empty", "", nil, true},
    56  		{"Empty list", "[]", jsonList{}, false},
    57  		{"List of int", "[1,2,3]", jsonList{1, 2, 3}, false},
    58  		{"Map", fmt.Sprint(dictFixture), dictFixture, false},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			var out interface{}
    63  			err := Unmarshal([]byte(tt.json), &out)
    64  			if (err != nil) != tt.wantErr {
    65  				t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
    66  			}
    67  			if err == nil && !reflect.DeepEqual(out, tt.want) {
    68  				t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestUnmarshalToMap(t *testing.T) {
    75  	tests := []struct {
    76  		name    string
    77  		json    string
    78  		want    interface{}
    79  		wantErr bool
    80  	}{
    81  		{"Empty", "", nil, true},
    82  		{"Empty list", "[]", nil, true},
    83  		{"List of int", "[1,2,3]", nil, true},
    84  		{"Map", fmt.Sprint(dictFixture), dictFixture.Native(), false},
    85  	}
    86  	for _, tt := range tests {
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			out := make(map[string]interface{})
    89  			err := Unmarshal([]byte(tt.json), &out)
    90  			if (err != nil) != tt.wantErr {
    91  				t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
    92  			}
    93  			if err == nil && !reflect.DeepEqual(out, tt.want) {
    94  				t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want)
    95  			}
    96  		})
    97  	}
    98  }