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

     1  //go:build unit || ALL
     2  
     3  /*
     4   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  )
    15  
    16  func Test_splitParent(t *testing.T) {
    17  	type args struct {
    18  		parent    string
    19  		separator string
    20  	}
    21  	tests := []struct {
    22  		name       string
    23  		args       args
    24  		wantFirst  string
    25  		wantSecond string
    26  		wantThird  string
    27  	}{
    28  		{
    29  			name:       "Empty",
    30  			args:       args{parent: "", separator: "|"},
    31  			wantFirst:  "",
    32  			wantSecond: "",
    33  			wantThird:  "",
    34  		},
    35  		{
    36  			name:       "One",
    37  			wantFirst:  "",
    38  			wantSecond: "",
    39  			wantThird:  "",
    40  		},
    41  		{
    42  			name:       "Two",
    43  			args:       args{parent: "first|second", separator: "|"},
    44  			wantFirst:  "first",
    45  			wantSecond: "second",
    46  			wantThird:  "",
    47  		},
    48  		{
    49  			name:       "Three",
    50  			args:       args{parent: "first|second|third", separator: "|"},
    51  			wantFirst:  "first",
    52  			wantSecond: "second",
    53  			wantThird:  "third",
    54  		},
    55  		{
    56  			name:       "Four",
    57  			args:       args{parent: "first|second|third|fourth", separator: "|"},
    58  			wantFirst:  "",
    59  			wantSecond: "",
    60  			wantThird:  "",
    61  		},
    62  	}
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			gotFirst, gotSecond, gotThird := splitParent(tt.args.parent, tt.args.separator)
    66  			if gotFirst != tt.wantFirst {
    67  				t.Errorf("splitParent() gotFirst = %v, want %v", gotFirst, tt.wantFirst)
    68  			}
    69  			if gotSecond != tt.wantSecond {
    70  				t.Errorf("splitParent() gotSecond = %v, want %v", gotSecond, tt.wantSecond)
    71  			}
    72  			if gotThird != tt.wantThird {
    73  				t.Errorf("splitParent() gotThird = %v, want %v", gotThird, tt.wantThird)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  // goldenString is a test helper to manage Golden files. It supports `update` parameter which may be
    80  // useful for writing such files (manual or automated way).
    81  func goldenString(t *testing.T, goldenFile string, actual string, update bool) string {
    82  	t.Helper()
    83  
    84  	goldenPath := "../test-resources/golden/" + t.Name() + "_" + goldenFile + ".golden"
    85  
    86  	f, err := os.OpenFile(filepath.Clean(goldenPath), os.O_RDWR|os.O_CREATE, 0600)
    87  	if err != nil {
    88  		t.Fatalf("unable to find golden file '%s': %s", goldenPath, err)
    89  	}
    90  	defer safeClose(f)
    91  
    92  	if update {
    93  		_, err := f.WriteString(actual)
    94  		if err != nil {
    95  			t.Fatalf("error writing to file %s: %s", goldenPath, err)
    96  		}
    97  
    98  		return actual
    99  	}
   100  
   101  	content, err := io.ReadAll(f)
   102  	if err != nil {
   103  		t.Fatalf("error opening file %s: %s", goldenPath, err)
   104  	}
   105  	return string(content)
   106  }
   107  
   108  // goldenBytes wraps goldenString and returns []byte
   109  func goldenBytes(t *testing.T, goldenFile string, actual []byte, update bool) []byte {
   110  	return []byte(goldenString(t, goldenFile, string(actual), update))
   111  }