github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/api_token_unit_test.go (about)

     1  //go:build unit || ALL
     2  
     3  /*
     4  * Copyright 2023 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  func Test_readFileAndUnmarshalJSON(t *testing.T) {
    15  	type args struct {
    16  		filename string
    17  		object   *testEntity
    18  	}
    19  	tests := []struct {
    20  		name    string
    21  		args    args
    22  		want    *testEntity
    23  		wantErr bool
    24  	}{
    25  		{
    26  			name: "simpleCase",
    27  			args: args{
    28  				filename: "test-resources/test.json",
    29  				object:   &testEntity{},
    30  			},
    31  			want:    &testEntity{Name: "test"},
    32  			wantErr: false,
    33  		},
    34  		{
    35  			name: "emptyFile",
    36  			args: args{
    37  				filename: "test-resources/test_empty.json",
    38  				object:   &testEntity{},
    39  			},
    40  			want:    &testEntity{},
    41  			wantErr: true,
    42  		},
    43  		{
    44  			name: "emptyJSON",
    45  			args: args{
    46  				filename: "test-resources/test_emptyJSON.json",
    47  				object:   &testEntity{},
    48  			},
    49  			want:    &testEntity{},
    50  			wantErr: false,
    51  		},
    52  		{
    53  			name: "nonexistentFile",
    54  			args: args{
    55  				filename: "thisfiledoesntexist.json",
    56  				object:   &testEntity{},
    57  			},
    58  			want:    &testEntity{},
    59  			wantErr: true,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			err := readFileAndUnmarshalJSON(tt.args.filename, tt.args.object)
    65  			if (err != nil) != tt.wantErr {
    66  				t.Errorf("readFileAndUnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
    67  				return
    68  			}
    69  
    70  			if !reflect.DeepEqual(tt.args.object, tt.want) {
    71  				t.Errorf("readFileAndUnmarshalJSON() = %v, want %v", tt.args.object, tt.want)
    72  			}
    73  		})
    74  	}
    75  }