github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/cpregister/register_comp_provider_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cpregister
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/mohae/deepcopy"
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/erda-project/erda-infra/providers/component-protocol/components/topn/impl"
    25  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    26  	"github.com/erda-project/erda-infra/providers/i18n"
    27  )
    28  
    29  type MockProvider struct {
    30  	// provider
    31  	Tran        i18n.Translator      `i18n:"hello"`
    32  	InParams    MockServiceInParams  `json:"inParams,omitempty"`
    33  	InParamsPtr *MockServiceInParams `json:"inParamsPtr,omitempty"`
    34  
    35  	// component
    36  	impl.DefaultTop
    37  }
    38  
    39  func (m MockProvider) RegisterInitializeOp() (opFunc cptype.OperationFunc) {
    40  	return func(sdk *cptype.SDK) cptype.IStdStructuredPtr { return nil }
    41  }
    42  
    43  type MockServiceInParams struct {
    44  	InParams *MockModel
    45  }
    46  
    47  type MockModel struct {
    48  	StartTime uint64 `json:"startTime,omitempty"`
    49  	EndTime   uint64 `json:"endTime,omitempty"`
    50  	TenantID  string `json:"tenantID,omitempty"`
    51  }
    52  
    53  func Test_copyProvider(t *testing.T) {
    54  	originProvider := &MockProvider{InParamsPtr: &MockServiceInParams{}, Tran: &i18n.NopTranslator{}}
    55  	copiedProvider := copyProvider(originProvider).(*MockProvider)
    56  	fmt.Printf("[provider] origin: %p, copied: %p\n", originProvider, copiedProvider)
    57  	fmt.Printf("[tran] origin: %p, copied: %p\n", originProvider.Tran, copiedProvider.Tran)
    58  	fmt.Printf("[InParams] origin: %p, copied: %p\n", &originProvider.InParams, &copiedProvider.InParams)
    59  	fmt.Printf("[InParamsPtr] origin: %p, copied: %p\n", originProvider.InParamsPtr, copiedProvider.InParamsPtr)
    60  	fmt.Printf("[DefaultTop] origin: %p, copied: %p\n", &originProvider.DefaultTop, &copiedProvider.DefaultTop)
    61  	assert.Equal(t, fmt.Sprintf("%p", originProvider.InParamsPtr), fmt.Sprintf("%p", copiedProvider.InParamsPtr))
    62  
    63  	fmt.Println()
    64  
    65  	deepCopiedProvider := deepcopy.Copy(originProvider).(*MockProvider)
    66  	fmt.Printf("[provider] origin: %p, copied: %p\n", originProvider, deepCopiedProvider)
    67  	fmt.Printf("[tran] origin: %p, copied: %p\n", originProvider.Tran, deepCopiedProvider.Tran)
    68  	fmt.Printf("[InParams] origin: %p, copied: %p\n", &originProvider.InParams, &deepCopiedProvider.InParams)
    69  	fmt.Printf("[InParamsPtr] origin: %p, copied: %p\n", originProvider.InParamsPtr, deepCopiedProvider.InParamsPtr)
    70  	fmt.Printf("[DefaultTop] origin: %p, copied: %p\n", &originProvider.DefaultTop, &deepCopiedProvider.DefaultTop)
    71  	assert.NotEqual(t, fmt.Sprintf("%p", originProvider.InParamsPtr), fmt.Sprintf("%p", deepCopiedProvider.InParamsPtr))
    72  }