...

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

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

     1  package awsutil_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go-v2/internal/awsutil"
     8  )
     9  
    10  type Struct struct {
    11  	A []Struct
    12  	z []Struct
    13  	B *Struct
    14  	D *Struct
    15  	C string
    16  	E map[string]string
    17  }
    18  
    19  var data = Struct{
    20  	A: []Struct{{C: "value1"}, {C: "value2"}, {C: "value3"}},
    21  	z: []Struct{{C: "value1"}, {C: "value2"}, {C: "value3"}},
    22  	B: &Struct{B: &Struct{C: "terminal"}, D: &Struct{C: "terminal2"}},
    23  	C: "initial",
    24  }
    25  var data2 = Struct{A: []Struct{
    26  	{A: []Struct{{C: "1"}, {C: "1"}, {C: "1"}, {C: "1"}, {C: "1"}}},
    27  	{A: []Struct{{C: "2"}, {C: "2"}, {C: "2"}, {C: "2"}, {C: "2"}}},
    28  }}
    29  
    30  func TestValueAtPathSuccess(t *testing.T) {
    31  	var testCases = []struct {
    32  		expect []interface{}
    33  		data   interface{}
    34  		path   string
    35  	}{
    36  		{[]interface{}{"initial"}, data, "C"},
    37  		{[]interface{}{"value1"}, data, "A[0].C"},
    38  		{[]interface{}{"value2"}, data, "A[1].C"},
    39  		{[]interface{}{"value3"}, data, "A[2].C"},
    40  		{[]interface{}{"value3"}, data, "a[2].c"},
    41  		{[]interface{}{"value3"}, data, "A[-1].C"},
    42  		{[]interface{}{"value1", "value2", "value3"}, data, "A[].C"},
    43  		{[]interface{}{"terminal"}, data, "B . B . C"},
    44  		{[]interface{}{"initial"}, data, "A.D.X || C"},
    45  		{[]interface{}{"initial"}, data, "A[0].B || C"},
    46  		{[]interface{}{
    47  			Struct{A: []Struct{{C: "1"}, {C: "1"}, {C: "1"}, {C: "1"}, {C: "1"}}},
    48  			Struct{A: []Struct{{C: "2"}, {C: "2"}, {C: "2"}, {C: "2"}, {C: "2"}}},
    49  		}, data2, "A"},
    50  	}
    51  	for i, c := range testCases {
    52  		v, err := awsutil.ValuesAtPath(c.data, c.path)
    53  		if err != nil {
    54  			t.Errorf("case %v, expected no error, %v", i, c.path)
    55  		}
    56  		if e, a := c.expect, v; !awsutil.DeepEqual(e, a) {
    57  			t.Errorf("case %v, %v", i, c.path)
    58  		}
    59  	}
    60  }
    61  
    62  func TestValueAtPathFailure(t *testing.T) {
    63  	var testCases = []struct {
    64  		expect      []interface{}
    65  		errContains string
    66  		data        interface{}
    67  		path        string
    68  	}{
    69  		{nil, "", data, "C.x"},
    70  		{nil, "SyntaxError: Invalid token: tDot", data, ".x"},
    71  		{nil, "", data, "X.Y.Z"},
    72  		{nil, "", data, "A[100].C"},
    73  		{nil, "", data, "A[3].C"},
    74  		{nil, "", data, "B.B.C.Z"},
    75  		{nil, "", data, "z[-1].C"},
    76  		{nil, "", nil, "A.B.C"},
    77  		{[]interface{}{}, "", Struct{}, "A"},
    78  		{nil, "", data, "A[0].B.C"},
    79  		{nil, "", data, "D"},
    80  	}
    81  
    82  	for i, c := range testCases {
    83  		v, err := awsutil.ValuesAtPath(c.data, c.path)
    84  		if c.errContains != "" {
    85  			if !strings.Contains(err.Error(), c.errContains) {
    86  				t.Errorf("case %v, expected error, %v", i, c.path)
    87  			}
    88  			continue
    89  		} else {
    90  			if err != nil {
    91  				t.Errorf("case %v, expected no error, %v", i, c.path)
    92  			}
    93  		}
    94  		if e, a := c.expect, v; !awsutil.DeepEqual(e, a) {
    95  			t.Errorf("case %v, %v", i, c.path)
    96  		}
    97  	}
    98  }
    99  
   100  func TestSetValueAtPathSuccess(t *testing.T) {
   101  	var s Struct
   102  	awsutil.SetValueAtPath(&s, "C", "test1")
   103  	awsutil.SetValueAtPath(&s, "B.B.C", "test2")
   104  	awsutil.SetValueAtPath(&s, "B.D.C", "test3")
   105  	if e, a := "test1", s.C; e != a {
   106  		t.Errorf("expected %v, but received %v", e, a)
   107  	}
   108  	if e, a := "test2", s.B.B.C; e != a {
   109  		t.Errorf("expected %v, but received %v", e, a)
   110  	}
   111  	if e, a := "test3", s.B.D.C; e != a {
   112  		t.Errorf("expected %v, but received %v", e, a)
   113  	}
   114  
   115  	awsutil.SetValueAtPath(&s, "B.*.C", "test0")
   116  	if e, a := "test0", s.B.B.C; e != a {
   117  		t.Errorf("expected %v, but received %v", e, a)
   118  	}
   119  	if e, a := "test0", s.B.D.C; e != a {
   120  		t.Errorf("expected %v, but received %v", e, a)
   121  	}
   122  
   123  	var s2 Struct
   124  	awsutil.SetValueAtPath(&s2, "b.b.c", "test0")
   125  	if e, a := "test0", s2.B.B.C; e != a {
   126  		t.Errorf("expected %v, but received %v", e, a)
   127  	}
   128  	awsutil.SetValueAtPath(&s2, "A", []Struct{{}})
   129  	if e, a := []Struct{{}}, s2.A; !awsutil.DeepEqual(e, a) {
   130  		t.Errorf("expected %v, but received %v", e, a)
   131  	}
   132  
   133  	str := "foo"
   134  
   135  	s3 := Struct{}
   136  	awsutil.SetValueAtPath(&s3, "b.b.c", str)
   137  	if e, a := "foo", s3.B.B.C; e != a {
   138  		t.Errorf("expected %v, but received %v", e, a)
   139  	}
   140  
   141  	s3 = Struct{B: &Struct{B: &Struct{C: str}}}
   142  	awsutil.SetValueAtPath(&s3, "b.b.c", nil)
   143  	if e, a := "", s3.B.B.C; e != a {
   144  		t.Errorf("expected %v, but received %v", e, a)
   145  	}
   146  
   147  	s3 = Struct{}
   148  	awsutil.SetValueAtPath(&s3, "b.b.c", nil)
   149  	if e, a := "", s3.B.B.C; e != a {
   150  		t.Errorf("expected %v, but received %v", e, a)
   151  	}
   152  
   153  	s3 = Struct{}
   154  	awsutil.SetValueAtPath(&s3, "b.b.c", &str)
   155  	if e, a := "foo", s3.B.B.C; e != a {
   156  		t.Errorf("expected %v, but received %v", e, a)
   157  	}
   158  
   159  	var s4 struct{ Name *string }
   160  	awsutil.SetValueAtPath(&s4, "Name", str)
   161  	if e, a := str, *s4.Name; e != a {
   162  		t.Errorf("expected %v, but received %v", e, a)
   163  	}
   164  
   165  	s4 = struct{ Name *string }{}
   166  	awsutil.SetValueAtPath(&s4, "Name", nil)
   167  	if e, a := (*string)(nil), s4.Name; e != a {
   168  		t.Errorf("expected %v, but received %v", e, a)
   169  	}
   170  
   171  	s4 = struct{ Name *string }{Name: &str}
   172  	awsutil.SetValueAtPath(&s4, "Name", nil)
   173  	if e, a := (*string)(nil), s4.Name; e != a {
   174  		t.Errorf("expected %v, but received %v", e, a)
   175  	}
   176  
   177  	s4 = struct{ Name *string }{}
   178  	awsutil.SetValueAtPath(&s4, "Name", &str)
   179  	if e, a := str, *s4.Name; e != a {
   180  		t.Errorf("expected %v, but received %v", e, a)
   181  	}
   182  }
   183  

View as plain text