github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/lists/remove_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 TestRemoveOrdered(t *testing.T) {
    12  	tests := []struct {
    13  		Slice    []any
    14  		Index    int
    15  		Expected []any
    16  		Error    bool
    17  	}{
    18  		{
    19  			Slice:    []any{"foo", "bar", "baz"},
    20  			Index:    -1,
    21  			Expected: nil,
    22  			Error:    true,
    23  		},
    24  		{
    25  			Slice:    []any{"foo", "bar", "baz"},
    26  			Index:    0,
    27  			Expected: []any{"bar", "baz"},
    28  		},
    29  		{
    30  			Slice:    []any{"foo", "bar", "baz"},
    31  			Index:    1,
    32  			Expected: []any{"foo", "baz"},
    33  		},
    34  		{
    35  			Slice:    []any{"foo", "bar", "baz"},
    36  			Index:    2,
    37  			Expected: []any{"foo", "bar"},
    38  		},
    39  		{
    40  			Slice:    []any{"foo", "bar", "baz"},
    41  			Index:    3,
    42  			Expected: nil,
    43  			Error:    true,
    44  		},
    45  
    46  		/////
    47  
    48  		{
    49  			Slice:    []any{5, 10, 15},
    50  			Index:    -1,
    51  			Expected: nil,
    52  			Error:    true,
    53  		},
    54  		{
    55  			Slice:    []any{5, 10, 15},
    56  			Index:    0,
    57  			Expected: []any{10, 15},
    58  		},
    59  		{
    60  			Slice:    []any{5, 10, 15},
    61  			Index:    1,
    62  			Expected: []any{5, 15},
    63  		},
    64  		{
    65  			Slice:    []any{5, 10, 15},
    66  			Index:    2,
    67  			Expected: []any{5, 10},
    68  		},
    69  		{
    70  			Slice:    []any{5, 10, 15},
    71  			Index:    3,
    72  			Expected: nil,
    73  			Error:    true,
    74  		},
    75  	}
    76  
    77  	count.Tests(t, len(tests))
    78  
    79  	for i, test := range tests {
    80  		expJson := json.LazyLogging(test.Expected)
    81  		actual, err := lists.RemoveOrdered(test.Slice, test.Index)
    82  		actJson := json.LazyLogging(actual)
    83  
    84  		if expJson != actJson || ((err != nil) != test.Error) {
    85  			t.Errorf("Unexpected return in test %d:", i)
    86  			t.Logf("  Slice:    %s", json.LazyLogging(test.Slice))
    87  			t.Logf("  Index:    %d", test.Index)
    88  			t.Logf("  Expected: %s", expJson)
    89  			t.Logf("  Actual:   %s", actJson)
    90  			t.Logf("  exp err:  %v", test.Error)
    91  			t.Logf("  act err:  %v", err)
    92  		}
    93  
    94  	}
    95  }
    96  
    97  func TestRemoveUnordered(t *testing.T) {
    98  	tests := []struct {
    99  		Slice    []any
   100  		Index    int
   101  		Expected []any
   102  		Error    bool
   103  	}{
   104  		{
   105  			Slice:    []any{"foo", "bar", "baz"},
   106  			Index:    -1,
   107  			Expected: nil,
   108  			Error:    true,
   109  		},
   110  		{
   111  			Slice:    []any{"foo", "bar", "baz"},
   112  			Index:    0,
   113  			Expected: []any{"baz", "bar"},
   114  		},
   115  		{
   116  			Slice:    []any{"foo", "bar", "baz"},
   117  			Index:    1,
   118  			Expected: []any{"foo", "baz"},
   119  		},
   120  		{
   121  			Slice:    []any{"foo", "bar", "baz"},
   122  			Index:    2,
   123  			Expected: []any{"foo", "bar"},
   124  		},
   125  		{
   126  			Slice:    []any{"foo", "bar", "baz"},
   127  			Index:    3,
   128  			Expected: nil,
   129  			Error:    true,
   130  		},
   131  
   132  		/////
   133  
   134  		{
   135  			Slice:    []any{5, 10, 15},
   136  			Index:    -1,
   137  			Expected: nil,
   138  			Error:    true,
   139  		},
   140  		{
   141  			Slice:    []any{5, 10, 15},
   142  			Index:    0,
   143  			Expected: []any{15, 10},
   144  		},
   145  		{
   146  			Slice:    []any{5, 10, 15},
   147  			Index:    1,
   148  			Expected: []any{5, 15},
   149  		},
   150  		{
   151  			Slice:    []any{5, 10, 15},
   152  			Index:    2,
   153  			Expected: []any{5, 10},
   154  		},
   155  		{
   156  			Slice:    []any{5, 10, 15},
   157  			Index:    3,
   158  			Expected: nil,
   159  			Error:    true,
   160  		},
   161  	}
   162  
   163  	count.Tests(t, len(tests))
   164  
   165  	for i, test := range tests {
   166  		expJson := json.LazyLogging(test.Expected)
   167  		actual, err := lists.RemoveUnordered(test.Slice, test.Index)
   168  		actJson := json.LazyLogging(actual)
   169  
   170  		if expJson != actJson || ((err != nil) != test.Error) {
   171  			t.Errorf("Unexpected return in test %d:", i)
   172  			t.Logf("  Slice:    %s", json.LazyLogging(test.Slice))
   173  			t.Logf("  Index:    %d", test.Index)
   174  			t.Logf("  Expected: %s", expJson)
   175  			t.Logf("  Actual:   %s", actJson)
   176  			t.Logf("  exp err:  %v", test.Error)
   177  			t.Logf("  act err:  %v", err)
   178  		}
   179  
   180  	}
   181  }