code.vegaprotocol.io/vega@v0.79.0/libs/errors/errors_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package errors_test
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/libs/errors"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestCircularreferences(t *testing.T) {
    28  	parent := errors.NewCumulatedErrors()
    29  	child := errors.NewCumulatedErrors()
    30  	nested := errors.NewCumulatedErrors()
    31  	errs := []error{
    32  		fmt.Errorf("simple error 1"),
    33  		fmt.Errorf("simple error 2"),
    34  		fmt.Errorf("simple error 3"),
    35  	}
    36  	t.Run("try to add parent to itself", func(t *testing.T) {
    37  		parent.Add(parent)
    38  		expect := "<self reference>"
    39  		require.True(t, parent.HasAny())
    40  		require.Equal(t, expect, parent.Error())
    41  	})
    42  	t.Run("try nesting without circular references", func(t *testing.T) {
    43  		child.Add(errs[0])
    44  		parent.Add(child)
    45  		expect := fmt.Sprintf("<self reference>, also %s", errs[0].Error())
    46  		require.Equal(t, expect, parent.Error())
    47  	})
    48  	t.Run("try adding empty cumulated error", func(t *testing.T) {
    49  		parent.Add(nested)
    50  		// still the same expected value
    51  		expect := fmt.Sprintf("<self reference>, also %s", errs[0].Error())
    52  		require.False(t, nested.HasAny())
    53  		require.Equal(t, expect, parent.Error())
    54  		// adding to the nested error should not affect the parent.
    55  		nested.Add(errs[1])
    56  		require.True(t, nested.HasAny())
    57  		require.Equal(t, expect, parent.Error())
    58  	})
    59  	t.Run("try nesting both parent and child, and adding to both", func(t *testing.T) {
    60  		nested.Add(errs[2])
    61  		nested.Add(child)
    62  		nested.Add(parent)
    63  		child.Add(nested)
    64  		parent.Add(nested)
    65  		// self reference>, also simple error 1, also simple error 2, also simple error 3, also simple error 1, also <self reference>, also simple error 1
    66  		expect := fmt.Sprintf("<self reference>, also %s, also %s, also %s, also %s, also <self reference>, also %s", errs[0].Error(), errs[1].Error(), errs[2].Error(), errs[0].Error(), errs[0].Error())
    67  		require.Equal(t, expect, parent.Error())
    68  	})
    69  }