github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/deps/deps_test.go (about)

     1  // Copyright 2022 PingCAP, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package deps
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  	"go.uber.org/dig"
    21  )
    22  
    23  type a struct {
    24  	val int
    25  }
    26  
    27  type b struct {
    28  	inner *a
    29  }
    30  
    31  type params struct {
    32  	dig.In
    33  
    34  	A *a
    35  	B *b
    36  }
    37  
    38  func TestDepsBasics(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	deps := NewDeps()
    42  	err := deps.Provide(func() *a {
    43  		return &a{val: 1}
    44  	})
    45  	require.NoError(t, err)
    46  
    47  	out, err := deps.Construct(func(input *a) (*b, error) {
    48  		return &b{inner: input}, nil
    49  	})
    50  	require.NoError(t, err)
    51  	require.IsType(t, &b{}, out)
    52  	require.Equal(t, &b{
    53  		inner: &a{val: 1},
    54  	}, out)
    55  
    56  	err = deps.Provide(func(inner *a) *b {
    57  		return &b{inner: inner}
    58  	})
    59  	require.NoError(t, err)
    60  
    61  	var p params
    62  	err = deps.Fill(&p)
    63  	require.NoError(t, err)
    64  	require.Equal(t, params{
    65  		A: &a{val: 1},
    66  		B: &b{inner: &a{val: 1}},
    67  	}, p)
    68  }