github.com/TBD54566975/ftl@v0.219.0/internal/reflect/reflect_test.go (about)

     1  package reflect
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type structOfPointers struct {
     8  	intPtr    *int
     9  	floatPtr  *float64
    10  	structPtr *structOfPointers
    11  }
    12  
    13  func TestCopyStructOfPointers(t *testing.T) {
    14  	one := 1
    15  	two := 2
    16  	half := 0.5
    17  	quarter := 0.25
    18  
    19  	for _, tt := range []struct {
    20  		name  string
    21  		input structOfPointers
    22  	}{
    23  		{
    24  			"StructWithinStruct",
    25  			structOfPointers{
    26  				intPtr:   &one,
    27  				floatPtr: &half,
    28  				structPtr: &structOfPointers{
    29  					intPtr:    &two,
    30  					floatPtr:  &quarter,
    31  					structPtr: nil,
    32  				},
    33  			},
    34  		},
    35  		{
    36  			"StructWithinStructWithinStruct",
    37  			structOfPointers{
    38  				intPtr:   &one,
    39  				floatPtr: &half,
    40  				structPtr: &structOfPointers{
    41  					intPtr:   &two,
    42  					floatPtr: &quarter,
    43  					structPtr: &structOfPointers{
    44  						intPtr:    &two,
    45  						floatPtr:  &quarter,
    46  						structPtr: nil,
    47  					},
    48  				},
    49  			},
    50  		},
    51  		{
    52  			"StructWithNils",
    53  			structOfPointers{
    54  				intPtr:    nil,
    55  				floatPtr:  nil,
    56  				structPtr: nil,
    57  			},
    58  		},
    59  		{
    60  			"StructWithStructWithNils",
    61  			structOfPointers{
    62  				intPtr:   &one,
    63  				floatPtr: &half,
    64  				structPtr: &structOfPointers{
    65  					intPtr:    nil,
    66  					floatPtr:  nil,
    67  					structPtr: nil,
    68  				},
    69  			},
    70  		},
    71  	} {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			output := DeepCopy(tt.input)
    74  			testEqualityOfStruct(t, &tt.input, &output)
    75  			// #nosec G601
    76  		})
    77  	}
    78  }
    79  
    80  func testEqualityOfStruct(t *testing.T, expected, actual *structOfPointers) {
    81  	t.Helper()
    82  	if expected == nil || actual == nil {
    83  		if expected != actual {
    84  			t.Errorf("struct point does not match nil struct pointer. expected %v, got %v", expected, actual)
    85  		}
    86  		return
    87  	}
    88  
    89  	testEqualityOfInt(t, expected.intPtr, actual.intPtr)
    90  	testEqualityOfFloat64(t, expected.floatPtr, actual.floatPtr)
    91  	testEqualityOfStruct(t, expected.structPtr, actual.structPtr)
    92  }
    93  
    94  func testEqualityOfInt(t *testing.T, expected, actual *int) {
    95  	t.Helper()
    96  	if expected == nil || actual == nil {
    97  		if expected != actual {
    98  			t.Errorf("int point does not match nil int pointer. expected %v, got %v", expected, actual)
    99  		}
   100  		return
   101  	}
   102  }
   103  
   104  func testEqualityOfFloat64(t *testing.T, expected, actual *float64) {
   105  	t.Helper()
   106  	if expected == nil || actual == nil {
   107  		if expected != actual {
   108  			t.Errorf("float point does not match nil float pointer. expected %v, got %v", expected, actual)
   109  		}
   110  		return
   111  	}
   112  }