github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/mapexamples/clone_Of_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_Clone(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.Of(employers)
    23  	delete(copy, "jun")
    24  	bob["name"] = "Superbob"
    25  
    26  	fmt.Printf("%v\n", employers) //map[devops:map[name:Superbob] jun:map[name:Tom]]
    27  	fmt.Printf("%v\n", copy)      //map[devops:map[name:Superbob]]
    28  
    29  	assert.NotSame(t, copy, employers)
    30  
    31  	assert.Equal(t, "Superbob", copy["devops"]["name"])
    32  
    33  	assert.Contains(t, employers, "jun")
    34  	assert.NotContains(t, copy, "jun")
    35  
    36  }