github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/avalanche/vertex/test_parser.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/snow/consensus/avalanche"
    14  )
    15  
    16  var (
    17  	errParse = errors.New("unexpectedly called Parse")
    18  
    19  	_ Parser = (*TestParser)(nil)
    20  )
    21  
    22  type TestParser struct {
    23  	T            *testing.T
    24  	CantParseVtx bool
    25  	ParseVtxF    func(context.Context, []byte) (avalanche.Vertex, error)
    26  }
    27  
    28  func (p *TestParser) Default(cant bool) {
    29  	p.CantParseVtx = cant
    30  }
    31  
    32  func (p *TestParser) ParseVtx(ctx context.Context, b []byte) (avalanche.Vertex, error) {
    33  	if p.ParseVtxF != nil {
    34  		return p.ParseVtxF(ctx, b)
    35  	}
    36  	if p.CantParseVtx && p.T != nil {
    37  		require.FailNow(p.T, errParse.Error())
    38  	}
    39  	return nil, errParse
    40  }