...

Source file src/dario.cat/mergo/issue230_test.go

Documentation: dario.cat/mergo

     1  package mergo_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"dario.cat/mergo"
     7  )
     8  
     9  var testDataM = []struct {
    10  	M1                     mapTest
    11  	M2                     mapTest
    12  	WithOverrideEmptyValue bool
    13  	ExpectedMap            map[int]int
    14  }{
    15  	{
    16  		M1: mapTest{
    17  			M: map[int]int{1: 1, 3: 3},
    18  		},
    19  		M2: mapTest{
    20  			M: map[int]int{1: 2, 2: 2},
    21  		},
    22  		WithOverrideEmptyValue: true,
    23  		ExpectedMap:            map[int]int{1: 1, 3: 3},
    24  	},
    25  	{
    26  		M1: mapTest{
    27  			M: map[int]int{1: 1, 3: 3},
    28  		},
    29  		M2: mapTest{
    30  			M: map[int]int{1: 2, 2: 2},
    31  		},
    32  		WithOverrideEmptyValue: false,
    33  		ExpectedMap:            map[int]int{1: 1, 2: 2, 3: 3},
    34  	},
    35  	{
    36  		M1: mapTest{
    37  			M: map[int]int{},
    38  		},
    39  		M2: mapTest{
    40  			M: map[int]int{1: 2, 2: 2},
    41  		},
    42  		WithOverrideEmptyValue: true,
    43  		ExpectedMap:            map[int]int{},
    44  	},
    45  	{
    46  		M1: mapTest{
    47  			M: map[int]int{},
    48  		},
    49  		M2: mapTest{
    50  			M: map[int]int{1: 2, 2: 2},
    51  		},
    52  		WithOverrideEmptyValue: false,
    53  		ExpectedMap:            map[int]int{1: 2, 2: 2},
    54  	},
    55  }
    56  
    57  func withOverrideEmptyValue(enable bool) func(*mergo.Config) {
    58  	if enable {
    59  		return mergo.WithOverwriteWithEmptyValue
    60  	}
    61  
    62  	return mergo.WithOverride
    63  }
    64  
    65  func TestMergeMapWithOverride(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	for _, data := range testDataM {
    69  		err := mergo.Merge(&data.M2, data.M1, withOverrideEmptyValue(data.WithOverrideEmptyValue))
    70  		if err != nil {
    71  			t.Errorf("Error while merging %s", err)
    72  		}
    73  
    74  		if len(data.M2.M) != len(data.ExpectedMap) {
    75  			t.Errorf("Got %d elements in map, but expected %d", len(data.M2.M), len(data.ExpectedMap))
    76  			return
    77  		}
    78  
    79  		for i, val := range data.M2.M {
    80  			if val != data.ExpectedMap[i] {
    81  				t.Errorf("Expected value: %d, but got %d while merging map", data.ExpectedMap[i], val)
    82  			}
    83  		}
    84  	}
    85  }
    86  

View as plain text