...

Source file src/github.com/aws/aws-sdk-go-v2/internal/awsutil/copy_test.go

Documentation: github.com/aws/aws-sdk-go-v2/internal/awsutil

     1  package awsutil_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aws/aws-sdk-go-v2/internal/awsutil"
    13  )
    14  
    15  func ExampleCopy() {
    16  	type Foo struct {
    17  		A int
    18  		B []string
    19  	}
    20  
    21  	// Create the initial value
    22  	str1 := "hello"
    23  	str2 := "bye bye"
    24  	f1 := &Foo{A: 1, B: []string{str1, str2}}
    25  
    26  	// Do the copy
    27  	var f2 Foo
    28  	awsutil.Copy(&f2, f1)
    29  
    30  	// Print the result
    31  	fmt.Println(awsutil.Prettify(f2))
    32  
    33  	// Output:
    34  	// {
    35  	//   A: 1,
    36  	//   B: [
    37  	//     "hello",
    38  	//     "bye bye"
    39  	//   ]
    40  	// }
    41  }
    42  
    43  func TestCopy1(t *testing.T) {
    44  	type Bar struct {
    45  		a *int
    46  		B *int
    47  		c int
    48  		D int
    49  	}
    50  	type Foo struct {
    51  		A int
    52  		B []string
    53  		C map[string]int
    54  		D *time.Time
    55  		E *Bar
    56  	}
    57  
    58  	// Create the initial value
    59  	str1 := "hello"
    60  	str2 := "bye bye"
    61  	int1 := 1
    62  	int2 := 2
    63  	intPtr1 := 1
    64  	intPtr2 := 2
    65  	now := time.Now()
    66  	f1 := &Foo{
    67  		A: 1,
    68  		B: []string{str1, str2},
    69  		C: map[string]int{
    70  			"A": int1,
    71  			"B": int2,
    72  		},
    73  		D: &now,
    74  		E: &Bar{
    75  			&intPtr1,
    76  			&intPtr2,
    77  			2,
    78  			3,
    79  		},
    80  	}
    81  
    82  	// Do the copy
    83  	var f2 Foo
    84  	awsutil.Copy(&f2, f1)
    85  
    86  	// Values are equal
    87  	if v1, v2 := f2.A, f1.A; v1 != v2 {
    88  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
    89  	}
    90  	if v1, v2 := f2.B, f1.B; !reflect.DeepEqual(v1, v2) {
    91  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
    92  	}
    93  	if v1, v2 := f2.C, f1.C; !reflect.DeepEqual(v1, v2) {
    94  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
    95  	}
    96  	if v1, v2 := f2.D, f1.D; !v1.Equal(*v2) {
    97  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
    98  	}
    99  	if v1, v2 := f2.E.B, f1.E.B; !reflect.DeepEqual(v1, v2) {
   100  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   101  	}
   102  	if v1, v2 := f2.E.D, f1.E.D; v1 != v2 {
   103  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   104  	}
   105  
   106  	// But pointers are not!
   107  	str3 := "nothello"
   108  	int3 := 57
   109  	f2.A = 100
   110  	f2.B[0] = str3
   111  	f2.C["B"] = int3
   112  	*f2.D = time.Now()
   113  	f2.E.a = &int3
   114  	*f2.E.B = int3
   115  	f2.E.c = 5
   116  	f2.E.D = 5
   117  	if v1, v2 := f2.A, f1.A; v1 == v2 {
   118  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   119  	}
   120  	if v1, v2 := f2.B, f1.B; reflect.DeepEqual(v1, v2) {
   121  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   122  	}
   123  	if v1, v2 := f2.C, f1.C; reflect.DeepEqual(v1, v2) {
   124  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   125  	}
   126  	if v1, v2 := f2.D, f1.D; v1 == v2 {
   127  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   128  	}
   129  	if v1, v2 := f2.E.a, f1.E.a; v1 == v2 {
   130  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   131  	}
   132  	if v1, v2 := f2.E.B, f1.E.B; v1 == v2 {
   133  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   134  	}
   135  	if v1, v2 := f2.E.c, f1.E.c; v1 == v2 {
   136  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   137  	}
   138  	if v1, v2 := f2.E.D, f1.E.D; v1 == v2 {
   139  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   140  	}
   141  }
   142  
   143  func TestCopyNestedWithUnexported(t *testing.T) {
   144  	type Bar struct {
   145  		a int
   146  		B int
   147  	}
   148  	type Foo struct {
   149  		A string
   150  		B Bar
   151  	}
   152  
   153  	f1 := &Foo{A: "string", B: Bar{a: 1, B: 2}}
   154  
   155  	var f2 Foo
   156  	awsutil.Copy(&f2, f1)
   157  
   158  	// Values match
   159  	if v1, v2 := f2.A, f1.A; v1 != v2 {
   160  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   161  	}
   162  	if v1, v2 := f2.B, f1.B; v1 == v2 {
   163  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   164  	}
   165  	if v1, v2 := f2.B.a, f1.B.a; v1 == v2 {
   166  		t.Errorf("expected values to be not equivalent, but received %v", v1)
   167  	}
   168  	if v1, v2 := f2.B.B, f2.B.B; v1 != v2 {
   169  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   170  	}
   171  }
   172  
   173  func TestCopyIgnoreNilMembers(t *testing.T) {
   174  	type Foo struct {
   175  		A *string
   176  		B []string
   177  		C map[string]string
   178  	}
   179  
   180  	f := &Foo{}
   181  	if v1 := f.A; v1 != nil {
   182  		t.Errorf("expected nil, but received %v", v1)
   183  	}
   184  	if v1 := f.B; v1 != nil {
   185  		t.Errorf("expected nil, but received %v", v1)
   186  	}
   187  	if v1 := f.C; v1 != nil {
   188  		t.Errorf("expected nil, but received %v", v1)
   189  	}
   190  
   191  	var f2 Foo
   192  	awsutil.Copy(&f2, f)
   193  	if v1 := f2.A; v1 != nil {
   194  		t.Errorf("expected nil, but received %v", v1)
   195  	}
   196  	if v1 := f2.B; v1 != nil {
   197  		t.Errorf("expected nil, but received %v", v1)
   198  	}
   199  	if v1 := f2.C; v1 != nil {
   200  		t.Errorf("expected nil, but received %v", v1)
   201  	}
   202  
   203  	fcopy := awsutil.CopyOf(f)
   204  	f3 := fcopy.(*Foo)
   205  	if v1 := f3.A; v1 != nil {
   206  		t.Errorf("expected nil, but received %v", v1)
   207  	}
   208  	if v1 := f3.B; v1 != nil {
   209  		t.Errorf("expected nil, but received %v", v1)
   210  	}
   211  	if v1 := f3.C; v1 != nil {
   212  		t.Errorf("expected nil, but received %v", v1)
   213  	}
   214  }
   215  
   216  func TestCopyPrimitive(t *testing.T) {
   217  	str := "hello"
   218  	var s string
   219  	awsutil.Copy(&s, &str)
   220  	if v1, v2 := "hello", s; v1 != v2 {
   221  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   222  	}
   223  }
   224  
   225  func TestCopyNil(t *testing.T) {
   226  	var s string
   227  	awsutil.Copy(&s, nil)
   228  	if v1, v2 := "", s; v1 != v2 {
   229  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   230  	}
   231  }
   232  
   233  func TestCopyReader(t *testing.T) {
   234  	var buf io.Reader = bytes.NewReader([]byte("hello world"))
   235  	var r io.Reader
   236  	awsutil.Copy(&r, buf)
   237  	b, err := ioutil.ReadAll(r)
   238  	if err != nil {
   239  		t.Errorf("expected no error, but received %v", err)
   240  	}
   241  	if v1, v2 := []byte("hello world"), b; !bytes.Equal(v1, v2) {
   242  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   243  	}
   244  
   245  	// empty bytes because this is not a deep copy
   246  	b, err = ioutil.ReadAll(buf)
   247  	if err != nil {
   248  		t.Errorf("expected no error, but received %v", err)
   249  	}
   250  	if v1, v2 := []byte(""), b; !bytes.Equal(v1, v2) {
   251  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   252  	}
   253  }
   254  
   255  func TestCopyDifferentStructs(t *testing.T) {
   256  	type SrcFoo struct {
   257  		A                int
   258  		B                []string
   259  		C                map[string]int
   260  		SrcUnique        string
   261  		SameNameDiffType int
   262  		unexportedPtr    *int
   263  		ExportedPtr      *int
   264  	}
   265  	type DstFoo struct {
   266  		A                int
   267  		B                []string
   268  		C                map[string]int
   269  		DstUnique        int
   270  		SameNameDiffType string
   271  		unexportedPtr    *int
   272  		ExportedPtr      *int
   273  	}
   274  
   275  	// Create the initial value
   276  	str1 := "hello"
   277  	str2 := "bye bye"
   278  	int1 := 1
   279  	int2 := 2
   280  	f1 := &SrcFoo{
   281  		A: 1,
   282  		B: []string{str1, str2},
   283  		C: map[string]int{
   284  			"A": int1,
   285  			"B": int2,
   286  		},
   287  		SrcUnique:        "unique",
   288  		SameNameDiffType: 1,
   289  		unexportedPtr:    &int1,
   290  		ExportedPtr:      &int2,
   291  	}
   292  
   293  	// Do the copy
   294  	var f2 DstFoo
   295  	awsutil.Copy(&f2, f1)
   296  
   297  	// Values are equal
   298  	if v1, v2 := f2.A, f1.A; v1 != v2 {
   299  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   300  	}
   301  	if v1, v2 := f2.B, f1.B; !reflect.DeepEqual(v1, v2) {
   302  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   303  	}
   304  	if v1, v2 := f2.C, f1.C; !reflect.DeepEqual(v1, v2) {
   305  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   306  	}
   307  	if v1, v2 := "unique", f1.SrcUnique; v1 != v2 {
   308  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   309  	}
   310  	if v1, v2 := 1, f1.SameNameDiffType; v1 != v2 {
   311  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   312  	}
   313  	if v1, v2 := 0, f2.DstUnique; v1 != v2 {
   314  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   315  	}
   316  	if v1, v2 := "", f2.SameNameDiffType; v1 != v2 {
   317  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   318  	}
   319  	if v1, v2 := int1, *f1.unexportedPtr; v1 != v2 {
   320  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   321  	}
   322  	if v1 := f2.unexportedPtr; v1 != nil {
   323  		t.Errorf("expected nil, but received %v", v1)
   324  	}
   325  	if v1, v2 := int2, *f1.ExportedPtr; v1 != v2 {
   326  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   327  	}
   328  	if v1, v2 := int2, *f2.ExportedPtr; v1 != v2 {
   329  		t.Errorf("expected values to be equivalent but received %v and %v", v1, v2)
   330  	}
   331  }
   332  
   333  func ExampleCopyOf() {
   334  	type Foo struct {
   335  		A int
   336  		B []string
   337  	}
   338  
   339  	// Create the initial value
   340  	str1 := "hello"
   341  	str2 := "bye bye"
   342  	f1 := &Foo{A: 1, B: []string{str1, str2}}
   343  
   344  	// Do the copy
   345  	v := awsutil.CopyOf(f1)
   346  	var f2 *Foo = v.(*Foo)
   347  
   348  	// Print the result
   349  	fmt.Println(awsutil.Prettify(f2))
   350  
   351  	// Output:
   352  	// &{
   353  	//   A: 1,
   354  	//   B: [
   355  	//     "hello",
   356  	//     "bye bye"
   357  	//   ]
   358  	// }
   359  }
   360  

View as plain text