goyave.dev/goyave/v4@v4.4.11/util/reflectutil/reflectutil_test.go (about)

     1  package reflectutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestOnly(t *testing.T) {
    10  	type Data struct {
    11  		Field string
    12  		Slice []float64
    13  		Num   int
    14  	}
    15  	type Promote struct {
    16  		Other string
    17  		Data
    18  	}
    19  	type PromotePtr struct {
    20  		*Data
    21  		Other string
    22  	}
    23  
    24  	data := map[string]interface{}{
    25  		"field": "value",
    26  		"num":   42,
    27  		"slice": []float64{2, 4, 8},
    28  	}
    29  	expected := map[string]interface{}{
    30  		"field": "value",
    31  		"slice": []float64{2, 4, 8},
    32  	}
    33  	res := Only(data, "field", "slice")
    34  	assert.Equal(t, expected, res)
    35  	assert.Equal(t, data["slice"], res["slice"])
    36  
    37  	model := Data{
    38  		Field: "value",
    39  		Num:   42,
    40  		Slice: []float64{3, 6, 9},
    41  	}
    42  	expected = map[string]interface{}{
    43  		"Field": "value",
    44  		"Slice": []float64{3, 6, 9},
    45  	}
    46  	res = Only(model, "Field", "Slice")
    47  	assert.Equal(t, expected, res)
    48  	assert.Equal(t, model.Slice, res["Slice"])
    49  
    50  	res = Only(&model, "Field", "Slice")
    51  	assert.Equal(t, expected, res)
    52  	assert.Equal(t, model.Slice, res["Slice"])
    53  
    54  	// Promoted fields
    55  	promote := Promote{
    56  		Data: Data{
    57  			Field: "value",
    58  			Num:   42,
    59  			Slice: []float64{3, 6, 9},
    60  		},
    61  		Other: "test",
    62  	}
    63  	expected = map[string]interface{}{
    64  		"Field": "value",
    65  		"Slice": []float64{3, 6, 9},
    66  		"Other": "test",
    67  	}
    68  	res = Only(promote, "Field", "Slice", "Other")
    69  	assert.Equal(t, expected, res)
    70  	assert.Equal(t, promote.Slice, res["Slice"])
    71  
    72  	// Promoted fields ptr
    73  	promotePtr := PromotePtr{
    74  		Data: &Data{
    75  			Field: "value",
    76  			Num:   42,
    77  			Slice: []float64{3, 6, 9},
    78  		},
    79  		Other: "test",
    80  	}
    81  	expected = map[string]interface{}{
    82  		"Field": "value",
    83  		"Slice": []float64{3, 6, 9},
    84  		"Other": "test",
    85  	}
    86  	res = Only(promotePtr, "Field", "Slice", "Other")
    87  	assert.Equal(t, expected, res)
    88  	assert.Equal(t, promote.Slice, res["Slice"])
    89  
    90  	// Promoted fields ptr nil
    91  	promotePtr = PromotePtr{
    92  		Other: "test",
    93  	}
    94  	expected = map[string]interface{}{
    95  		"Other": "test",
    96  	}
    97  	res = Only(promotePtr, "Field", "Slice", "Other")
    98  	assert.Equal(t, expected, res)
    99  }
   100  
   101  func TestOnlyError(t *testing.T) {
   102  	dataInt := map[int]interface{}{
   103  		1: "value",
   104  		3: 42,
   105  		4: []float64{2, 4, 8},
   106  	}
   107  	assert.Panics(t, func() {
   108  		Only(dataInt, "3", "5")
   109  	})
   110  
   111  	assert.Panics(t, func() {
   112  		Only("not a struct")
   113  	})
   114  }
   115  
   116  func TestOnlyConflictingPromotedFields(t *testing.T) {
   117  	type Data struct {
   118  		Field string
   119  	}
   120  	type Promote struct {
   121  		Field string
   122  		Data
   123  	}
   124  
   125  	data := Promote{
   126  		Data: Data{
   127  			Field: "in data",
   128  		},
   129  		Field: "in promote",
   130  	}
   131  	expected := map[string]interface{}{
   132  		"Field": "in promote",
   133  	}
   134  	res := Only(data, "Field")
   135  	assert.Equal(t, expected, res)
   136  }