github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/context/store/storeconfig_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  )
     8  
     9  type testCtx struct{}
    10  type testEP1 struct{}
    11  type testEP2 struct{}
    12  type testEP3 struct{}
    13  
    14  func TestConfigModification(t *testing.T) {
    15  	cfg := NewConfig(func() interface{} { return &testCtx{} }, EndpointTypeGetter("ep1", func() interface{} { return &testEP1{} }))
    16  	assert.Equal(t, &testCtx{}, cfg.contextType())
    17  	assert.Equal(t, &testEP1{}, cfg.endpointTypes["ep1"]())
    18  	cfgCopy := cfg
    19  
    20  	// modify existing endpoint
    21  	cfg.SetEndpoint("ep1", func() interface{} { return &testEP2{} })
    22  	// add endpoint
    23  	cfg.SetEndpoint("ep2", func() interface{} { return &testEP3{} })
    24  	assert.Equal(t, &testCtx{}, cfg.contextType())
    25  	assert.Equal(t, &testEP2{}, cfg.endpointTypes["ep1"]())
    26  	assert.Equal(t, &testEP3{}, cfg.endpointTypes["ep2"]())
    27  	// check it applied on already initialized store
    28  	assert.Equal(t, &testCtx{}, cfgCopy.contextType())
    29  	assert.Equal(t, &testEP2{}, cfgCopy.endpointTypes["ep1"]())
    30  	assert.Equal(t, &testEP3{}, cfgCopy.endpointTypes["ep2"]())
    31  }