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

     1  package lists_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/lang/types"
     7  	"github.com/lmorg/murex/test/count"
     8  	"github.com/lmorg/murex/utils/json"
     9  	"github.com/lmorg/murex/utils/lists"
    10  )
    11  
    12  func TestCountInt(t *testing.T) {
    13  	type testCountT struct {
    14  		List   []int
    15  		Output map[string]int
    16  		Error  bool
    17  	}
    18  
    19  	tests := []testCountT{
    20  		{
    21  			List: []int{0, 1, 2, 3, 2, 1},
    22  			Output: map[string]int{
    23  				"0": 1,
    24  				"1": 2,
    25  				"2": 2,
    26  				"3": 1,
    27  			},
    28  		},
    29  	}
    30  
    31  	count.Tests(t, len(tests))
    32  
    33  	for i, test := range tests {
    34  		jExp := json.LazyLogging(test.Output)
    35  		actual, err := lists.Count(test.List)
    36  		jAct := json.LazyLogging(actual)
    37  
    38  		if jExp != jAct || (err == nil) == test.Error {
    39  			t.Errorf("Test %d failed", i)
    40  			t.Logf("  List:     %s", json.LazyLogging(test.List))
    41  			t.Logf("  Expected: %s", jExp)
    42  			t.Logf("  Actual:   %s", jAct)
    43  			t.Logf("  exp err:  %v", test.Error)
    44  			t.Logf("  err msg:  %s", err)
    45  		}
    46  	}
    47  }
    48  
    49  func TestCountFloat(t *testing.T) {
    50  	type testCountT struct {
    51  		List   []float64
    52  		Output map[string]int
    53  		Error  bool
    54  	}
    55  
    56  	tests := []testCountT{
    57  		{
    58  			List: []float64{0, 1, 2, 3.3, 2, 1.1},
    59  			Output: map[string]int{
    60  				"0":   1,
    61  				"1":   1,
    62  				"1.1": 1,
    63  				"2":   2,
    64  				"3.3": 1,
    65  			},
    66  		},
    67  	}
    68  
    69  	count.Tests(t, len(tests))
    70  
    71  	for i, test := range tests {
    72  		jExp := json.LazyLogging(test.Output)
    73  		actual, err := lists.Count(test.List)
    74  		jAct := json.LazyLogging(actual)
    75  
    76  		if jExp != jAct || (err == nil) == test.Error {
    77  			t.Errorf("Test %d failed", i)
    78  			t.Logf("  List:     %s", json.LazyLogging(test.List))
    79  			t.Logf("  Expected: %s", jExp)
    80  			t.Logf("  Actual:   %s", jAct)
    81  			t.Logf("  exp err:  %v", test.Error)
    82  			t.Logf("  err msg:  %s", err)
    83  		}
    84  	}
    85  }
    86  
    87  func TestCountString(t *testing.T) {
    88  	type testCountT struct {
    89  		List   []string
    90  		Output map[string]int
    91  		Error  bool
    92  	}
    93  
    94  	tests := []testCountT{
    95  		{
    96  			List: []string{"0", "a", "b", "c", "b", "a"},
    97  			Output: map[string]int{
    98  				"0": 1,
    99  				"a": 2,
   100  				"b": 2,
   101  				"c": 1,
   102  			},
   103  		},
   104  	}
   105  
   106  	count.Tests(t, len(tests))
   107  
   108  	for i, test := range tests {
   109  		jExp := json.LazyLogging(test.Output)
   110  		actual, err := lists.Count(test.List)
   111  		jAct := json.LazyLogging(actual)
   112  
   113  		if jExp != jAct || (err == nil) == test.Error {
   114  			t.Errorf("Test %d failed", i)
   115  			t.Logf("  List:     %s", json.LazyLogging(test.List))
   116  			t.Logf("  Expected: %s", jExp)
   117  			t.Logf("  Actual:   %s", jAct)
   118  			t.Logf("  exp err:  %v", test.Error)
   119  			t.Logf("  err msg:  %s", err)
   120  		}
   121  	}
   122  }
   123  
   124  func TestCountBool(t *testing.T) {
   125  	type testCountT struct {
   126  		List   []bool
   127  		Output map[string]int
   128  		Error  bool
   129  	}
   130  
   131  	tests := []testCountT{
   132  		{
   133  			List: []bool{true, false, true},
   134  			Output: map[string]int{
   135  				types.TrueString:  2,
   136  				types.FalseString: 1,
   137  			},
   138  		},
   139  	}
   140  
   141  	count.Tests(t, len(tests))
   142  
   143  	for i, test := range tests {
   144  		jExp := json.LazyLogging(test.Output)
   145  		actual, err := lists.Count(test.List)
   146  		jAct := json.LazyLogging(actual)
   147  
   148  		if jExp != jAct || (err == nil) == test.Error {
   149  			t.Errorf("Test %d failed", i)
   150  			t.Logf("  List:     %s", json.LazyLogging(test.List))
   151  			t.Logf("  Expected: %s", jExp)
   152  			t.Logf("  Actual:   %s", jAct)
   153  			t.Logf("  exp err:  %v", test.Error)
   154  			t.Logf("  err msg:  %s", err)
   155  		}
   156  	}
   157  }
   158  
   159  func TestCountInterface(t *testing.T) {
   160  	type testCountT struct {
   161  		List   []interface{}
   162  		Output map[string]int
   163  		Error  bool
   164  	}
   165  
   166  	tests := []testCountT{
   167  		{
   168  			List: []interface{}{1.11, 2.2, 3, "a", "b", "c", true, 2.2, 1.11, 1.11, "b", "c", "c"},
   169  			Output: map[string]int{
   170  				"1.11":           3,
   171  				"2.2":            2,
   172  				"3":              1,
   173  				"a":              1,
   174  				"b":              2,
   175  				"c":              3,
   176  				types.TrueString: 1,
   177  			},
   178  		},
   179  	}
   180  
   181  	count.Tests(t, len(tests))
   182  
   183  	for i, test := range tests {
   184  		jExp := json.LazyLogging(test.Output)
   185  		actual, err := lists.Count(test.List)
   186  		jAct := json.LazyLogging(actual)
   187  
   188  		if jExp != jAct || (err == nil) == test.Error {
   189  			t.Errorf("Test %d failed", i)
   190  			t.Logf("  List:     %s", json.LazyLogging(test.List))
   191  			t.Logf("  Expected: %s", jExp)
   192  			t.Logf("  Actual:   %s", jAct)
   193  			t.Logf("  exp err:  %v", test.Error)
   194  			t.Logf("  err msg:  %s", err)
   195  		}
   196  	}
   197  }