github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/avalanche/vertex/test_builder.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package vertex
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/MetalBlockchain/metalgo/ids"
    14  	"github.com/MetalBlockchain/metalgo/snow/consensus/avalanche"
    15  )
    16  
    17  var (
    18  	errBuild = errors.New("unexpectedly called Build")
    19  
    20  	_ Builder = (*TestBuilder)(nil)
    21  )
    22  
    23  type TestBuilder struct {
    24  	T             *testing.T
    25  	CantBuildVtx  bool
    26  	BuildStopVtxF func(ctx context.Context, parentIDs []ids.ID) (avalanche.Vertex, error)
    27  }
    28  
    29  func (b *TestBuilder) Default(cant bool) {
    30  	b.CantBuildVtx = cant
    31  }
    32  
    33  func (b *TestBuilder) BuildStopVtx(ctx context.Context, parentIDs []ids.ID) (avalanche.Vertex, error) {
    34  	if b.BuildStopVtxF != nil {
    35  		return b.BuildStopVtxF(ctx, parentIDs)
    36  	}
    37  	if b.CantBuildVtx && b.T != nil {
    38  		require.FailNow(b.T, errBuild.Error())
    39  	}
    40  	return nil, errBuild
    41  }