github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/dependencies_test.go (about)

     1  package host
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  )
     7  
     8  // TestComposeErrors checks that composeErrors is correctly composing errors
     9  // and handling edge cases.
    10  func TestComposeErrors(t *testing.T) {
    11  	if testing.Short() {
    12  		t.SkipNow()
    13  	}
    14  	t.Parallel()
    15  
    16  	trials := []struct {
    17  		inputErrors           []error
    18  		nilReturn             bool
    19  		expectedComposedError string
    20  	}{
    21  		{
    22  			nil,
    23  			true,
    24  			"",
    25  		},
    26  		{
    27  			make([]error, 0),
    28  			true,
    29  			"",
    30  		},
    31  		{
    32  			[]error{errors.New("single error")},
    33  			false,
    34  			"single error",
    35  		},
    36  		{
    37  			[]error{
    38  				errors.New("first error"),
    39  				errors.New("second error"),
    40  			},
    41  			false,
    42  			"first error; second error",
    43  		},
    44  		{
    45  			[]error{
    46  				errors.New("first error"),
    47  				errors.New("second error"),
    48  				errors.New("third error"),
    49  			},
    50  			false,
    51  			"first error; second error; third error",
    52  		},
    53  		{
    54  			[]error{
    55  				nil,
    56  				errors.New("second error"),
    57  				errors.New("third error"),
    58  			},
    59  			false,
    60  			"second error; third error",
    61  		},
    62  		{
    63  			[]error{
    64  				errors.New("first error"),
    65  				nil,
    66  				nil,
    67  			},
    68  			false,
    69  			"first error",
    70  		},
    71  		{
    72  			[]error{
    73  				nil,
    74  				nil,
    75  				nil,
    76  			},
    77  			true,
    78  			"",
    79  		},
    80  	}
    81  	for _, trial := range trials {
    82  		err := composeErrors(trial.inputErrors...)
    83  		if trial.nilReturn {
    84  			if err != nil {
    85  				t.Error("composeError failed a test, expecting nil, got", err)
    86  			}
    87  		} else {
    88  			if err == nil {
    89  				t.Error("not expecting a nil error when doing composition")
    90  			}
    91  			if err.Error() != trial.expectedComposedError {
    92  				t.Error("composeError failed a test, expecting", trial.expectedComposedError, "got", err.Error())
    93  			}
    94  		}
    95  	}
    96  }