github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/lists/generics_test.go (about)

     1  package lists_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/test/count"
     7  	"github.com/lmorg/murex/utils/json"
     8  	"github.com/lmorg/murex/utils/lists"
     9  )
    10  
    11  func TestGenericToString(t *testing.T) {
    12  	tests := []struct {
    13  		Source   any
    14  		Expected []string
    15  		Error    bool
    16  	}{
    17  		{
    18  			Source:   []interface{}{1, 2, 3},
    19  			Expected: []string{"1", "2", "3"},
    20  			Error:    false,
    21  		},
    22  		{
    23  			Source:   []int{1, 2, 3},
    24  			Expected: nil,
    25  			Error:    true,
    26  		},
    27  		{
    28  			Source:   []interface{}{"1", "2", "3"},
    29  			Expected: []string{"1", "2", "3"},
    30  			Error:    false,
    31  		},
    32  		{
    33  			Source:   []string{"1", "2", "3"},
    34  			Expected: []string{"1", "2", "3"},
    35  			Error:    false,
    36  		},
    37  	}
    38  
    39  	count.Tests(t, len(tests))
    40  
    41  	for i, test := range tests {
    42  		actual, err := lists.GenericToString(test.Source)
    43  		expJson := json.LazyLoggingPretty(test.Expected)
    44  		actJson := json.LazyLoggingPretty(actual)
    45  
    46  		if expJson != actJson || (err != nil) != test.Error {
    47  			t.Errorf("Conversion failed in test %d", i)
    48  			t.Logf("  Source:   %s", json.LazyLoggingPretty(test.Source))
    49  			t.Logf("  Expected: %s", expJson)
    50  			t.Logf("  Actual:   %s", actJson)
    51  			t.Logf("  exp err:  %v", test.Error)
    52  			t.Logf("  act err:  %v", err)
    53  		}
    54  	}
    55  }