...

Source file src/github.com/chaos-mesh/chaos-mesh/api/genericwebhook/bfs_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/genericwebhook

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package genericwebhook
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/onsi/gomega"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  )
    25  
    26  func TestBfs(t *testing.T) {
    27  	g := gomega.NewGomegaWithT(t)
    28  
    29  	{
    30  		type Test struct {
    31  			A, B, C int
    32  		}
    33  
    34  		testStruct := &Test{1, 2, 3}
    35  		walker := NewFieldWalker(testStruct, func(path *field.Path, obj interface{}, field *reflect.StructField) bool {
    36  			val := obj.(*int)
    37  			*val = 2
    38  			return true
    39  		})
    40  		walker.Walk()
    41  		g.Expect(testStruct).To(gomega.Equal(&Test{2, 2, 2}))
    42  	}
    43  
    44  	{
    45  		type Test struct {
    46  			A, B int
    47  			C    *int
    48  		}
    49  
    50  		testC := 3
    51  		two := 2
    52  		testStruct := &Test{1, 2, &testC}
    53  		walker := NewFieldWalker(testStruct, func(path *field.Path, obj interface{}, field *reflect.StructField) bool {
    54  			switch obj := obj.(type) {
    55  			case *int:
    56  				*obj = 2
    57  			case **int:
    58  				*obj = &two
    59  			default:
    60  				panic("unexpected type")
    61  			}
    62  			return true
    63  		})
    64  		walker.Walk()
    65  		g.Expect(testStruct).To(gomega.Equal(&Test{2, 2, &two}))
    66  	}
    67  
    68  	{
    69  		type Inside struct {
    70  			A, B int
    71  		}
    72  		type DeepTest struct {
    73  			A, B int
    74  			C    Inside
    75  		}
    76  
    77  		testStruct := &DeepTest{1, 2, Inside{3, 4}}
    78  		walker := NewFieldWalker(testStruct, func(path *field.Path, obj interface{}, field *reflect.StructField) bool {
    79  			switch obj := obj.(type) {
    80  			case *int:
    81  				*obj = 2
    82  			case *Inside:
    83  				*obj = Inside{2, 2}
    84  			default:
    85  				panic("unexpected type")
    86  			}
    87  
    88  			return false
    89  		})
    90  		walker.Walk()
    91  		g.Expect(testStruct).To(gomega.Equal(&DeepTest{2, 2, Inside{2, 2}}))
    92  	}
    93  
    94  	{
    95  		type Inside struct {
    96  			A, B int
    97  		}
    98  		type DeepTest struct {
    99  			A, B int
   100  			C    Inside
   101  		}
   102  
   103  		testStruct := &DeepTest{1, 2, Inside{3, 4}}
   104  		walker := NewFieldWalker(testStruct, func(path *field.Path, obj interface{}, field *reflect.StructField) bool {
   105  			switch obj := obj.(type) {
   106  			case *int:
   107  				*obj = 2
   108  			case *Inside:
   109  				return true
   110  			default:
   111  				panic("unexpected type")
   112  			}
   113  
   114  			return false
   115  		})
   116  		walker.Walk()
   117  		g.Expect(testStruct).To(gomega.Equal(&DeepTest{2, 2, Inside{2, 2}}))
   118  	}
   119  }
   120