github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/mapexamples/clone_Deep_test.go (about)

     1  package mapexamples
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/map_/clone"
    10  )
    11  
    12  func Test_CloneDeep(t *testing.T) {
    13  
    14  	var bob = map[string]string{"name": "Bob"}
    15  	var tom = map[string]string{"name": "Tom"}
    16  
    17  	var employers = map[string]map[string]string{
    18  		"devops": bob,
    19  		"jun":    tom,
    20  	}
    21  
    22  	copy := clone.Deep(employers, func(employer map[string]string) map[string]string {
    23  		return clone.Of(employer)
    24  	})
    25  	delete(copy, "jun")
    26  	bob["name"] = "Superbob"
    27  
    28  	fmt.Printf("%v\n", employers) //map[devops:map[name:Superbob] jun:map[name:Tom]]
    29  	fmt.Printf("%v\n", copy)      //map[devops:map[name:Bob]]
    30  
    31  	assert.NotSame(t, copy, employers)
    32  
    33  	assert.Equal(t, "Bob", copy["devops"]["name"])
    34  
    35  	assert.Contains(t, employers, "jun")
    36  	assert.NotContains(t, copy, "jun")
    37  
    38  }