github.com/haraldrudell/parl@v0.4.176/pslices/convert-slice_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pslices
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestConvertSliceToInterface(t *testing.T) {
    14  	type a int
    15  	type b any
    16  
    17  	// convert slice of a: int to slice of b:any
    18  	interfaceSlice := ConvertSliceToInterface[
    19  		a,
    20  		b,
    21  	]([]a{1})
    22  
    23  	// slice length should be 1
    24  	if len(interfaceSlice) != 1 {
    25  		t.Errorf("bad ifSlice len: %d exp 1", len(interfaceSlice))
    26  	}
    27  
    28  	messageNI := "not implement"
    29  
    30  	// panic conversion
    31  	var err error
    32  	func() {
    33  		defer func() {
    34  			err = recover().(error)
    35  		}()
    36  
    37  		ConvertSliceToInterface[
    38  			b,
    39  			a,
    40  		]([]b{1})
    41  	}()
    42  
    43  	// should have anic not implement
    44  	if err == nil || !strings.Contains(err.Error(), messageNI) {
    45  		t.Errorf("ConvertSliceToInterface2 err: '%v' exp %q", err, messageNI)
    46  	}
    47  }