github.com/coveo/gotemplate@v2.7.7+incompatible/errors/errors_test.go (about) 1 package errors 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestMust(t *testing.T) { 11 type i = interface{} 12 tests := []struct { 13 name string 14 result []i 15 want i 16 wantErr bool 17 }{ 18 {"Nil", nil, nil, false}, 19 {"1 arg", []i{"Hello"}, nil, true}, 20 {"1 arg, nil", []i{nil}, nil, false}, 21 {"2 arg, second not nul", []i{"Hello", "World"}, nil, true}, 22 {"2 arg, second nul", []i{"Hello", nil}, "Hello", false}, 23 {"3 arg, last nul", []i{"Hello", "World", nil}, []i{"Hello", "World"}, false}, 24 } 25 for _, tt := range tests { 26 var err error 27 t.Run(tt.name, func(t *testing.T) { 28 defer func() { err = Trap(err, recover()) }() 29 if got := Must(tt.result...); !reflect.DeepEqual(got, tt.want) { 30 t.Errorf("Must() = %v, want %v", got, tt.want) 31 } 32 }) 33 if (err != nil) != tt.wantErr { 34 t.Errorf("CreateList() error = %v, wantErr %v", err, tt.wantErr) 35 } 36 } 37 } 38 39 func TestPrint(t *testing.T) { 40 Printf("This is an error") 41 var err error 42 func() { 43 defer func() { err = Trap(err, recover()) }() 44 Raise("This is also an error") 45 }() 46 assert.NotNil(t, err, "Error should be not nil") 47 Print(err) 48 49 err = nil 50 func() { 51 defer func() { err = Trap(err, recover()) }() 52 panic(911) 53 }() 54 assert.NotNil(t, err, "Error should be not nil") 55 Print(err) 56 57 // We left the err intact to test array creation 58 func() { 59 defer func() { err = Trap(err, recover()) }() 60 panic(TemplateNotFoundError{"filename"}) 61 }() 62 assert.NotNil(t, err, "Error should be not nil") 63 Print(err) 64 65 err = nil 66 func() { 67 defer func() { err = Trap(err, recover()) }() 68 // No error 69 }() 70 assert.Nil(t, err, "Error should be nil") 71 }