github.com/enetx/g@v1.0.80/tests/filters_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g/f"
     7  )
     8  
     9  func TestComparable(t *testing.T) {
    10  	tests := []struct {
    11  		name  string
    12  		value any
    13  		want  bool
    14  	}{
    15  		{"Int", 10, true},
    16  		{"String", "Hello", true},
    17  		{"Slice", []int{1, 2, 3}, false},
    18  		{"Map", make(map[string]int), false},
    19  		{"Struct", struct{ X int }{}, true},
    20  		{"Func", (func())(nil), false},
    21  	}
    22  	for _, tt := range tests {
    23  		t.Run(tt.name, func(t *testing.T) {
    24  			if got := f.Comparable(tt.value); got != tt.want {
    25  				t.Errorf("Comparable() = %v, want %v", got, tt.want)
    26  			}
    27  		})
    28  	}
    29  }
    30  
    31  func TestZero(t *testing.T) {
    32  	// Testing Zero function with integers
    33  	if !f.Zero(0) {
    34  		t.Errorf("Zero(0) returned false, expected true")
    35  	}
    36  
    37  	if f.Zero(5) {
    38  		t.Errorf("Zero(5) returned true, expected false")
    39  	}
    40  
    41  	// Testing Zero function with floats
    42  	if !f.Zero(0.0) {
    43  		t.Errorf("Zero(0.0) returned false, expected true")
    44  	}
    45  
    46  	if f.Zero(3.14) {
    47  		t.Errorf("Zero(3.14) returned true, expected false")
    48  	}
    49  
    50  	// Testing Zero function with strings
    51  	if !f.Zero("") {
    52  		t.Errorf("Zero(\"\") returned false, expected true")
    53  	}
    54  
    55  	if f.Zero("hello") {
    56  		t.Errorf("Zero(\"hello\") returned true, expected false")
    57  	}
    58  }
    59  
    60  func TestEven(t *testing.T) {
    61  	// Testing Even function with positive even integers
    62  	if !f.Even(2) {
    63  		t.Errorf("Even(2) returned false, expected true")
    64  	}
    65  
    66  	if !f.Even(100) {
    67  		t.Errorf("Even(100) returned false, expected true")
    68  	}
    69  
    70  	// Testing Even function with positive odd integers
    71  	if f.Even(3) {
    72  		t.Errorf("Even(3) returned true, expected false")
    73  	}
    74  
    75  	if f.Even(101) {
    76  		t.Errorf("Even(101) returned true, expected false")
    77  	}
    78  
    79  	// Testing Even function with negative even integers
    80  	if !f.Even(-2) {
    81  		t.Errorf("Even(-2) returned false, expected true")
    82  	}
    83  
    84  	if !f.Even(-100) {
    85  		t.Errorf("Even(-100) returned false, expected true")
    86  	}
    87  
    88  	// Testing Even function with negative odd integers
    89  	if f.Even(-3) {
    90  		t.Errorf("Even(-3) returned true, expected false")
    91  	}
    92  
    93  	if f.Even(-101) {
    94  		t.Errorf("Even(-101) returned true, expected false")
    95  	}
    96  }
    97  
    98  func TestOdd(t *testing.T) {
    99  	// Testing Odd function with positive odd integers
   100  	if !f.Odd(3) {
   101  		t.Errorf("Odd(3) returned false, expected true")
   102  	}
   103  
   104  	if !f.Odd(101) {
   105  		t.Errorf("Odd(101) returned false, expected true")
   106  	}
   107  
   108  	// Testing Odd function with positive even integers
   109  	if f.Odd(2) {
   110  		t.Errorf("Odd(2) returned true, expected false")
   111  	}
   112  
   113  	if f.Odd(100) {
   114  		t.Errorf("Odd(100) returned true, expected false")
   115  	}
   116  
   117  	// Testing Odd function with negative odd integers
   118  	if !f.Odd(-3) {
   119  		t.Errorf("Odd(-3) returned false, expected true")
   120  	}
   121  
   122  	if !f.Odd(-101) {
   123  		t.Errorf("Odd(-101) returned false, expected true")
   124  	}
   125  
   126  	// Testing Odd function with negative even integers
   127  	if f.Odd(-2) {
   128  		t.Errorf("Odd(-2) returned true, expected false")
   129  	}
   130  
   131  	if f.Odd(-100) {
   132  		t.Errorf("Odd(-100) returned true, expected false")
   133  	}
   134  }
   135  
   136  func TestEq(t *testing.T) {
   137  	// Test case 1: Test equality of integers
   138  	if !f.Eq(5)(5) {
   139  		t.Errorf("Test case 1: Expected true, got false")
   140  	}
   141  
   142  	// Test case 2: Test inequality of strings
   143  	if f.Eq("hello")("world") {
   144  		t.Errorf("Test case 2: Expected false, got true")
   145  	}
   146  }
   147  
   148  func TestNe(t *testing.T) {
   149  	// Test case 1: Test inequality of integers
   150  	if !f.Ne(5)(6) {
   151  		t.Errorf("Test case 1: Expected true, got false")
   152  	}
   153  
   154  	// Test case 2: Test equality of strings
   155  	if !f.Ne("hello")("world") {
   156  		t.Errorf("Test case 2: Expected true, got false")
   157  	}
   158  }
   159  
   160  func TestEqd(t *testing.T) {
   161  	// Test case 1: Test deep equality of slices
   162  	slice1 := []int{1, 2, 3}
   163  	slice2 := []int{1, 2, 3}
   164  	if !f.Eqd(slice1)(slice2) {
   165  		t.Errorf("Test case 1: Expected true, got false")
   166  	}
   167  
   168  	// Test case 2: Test deep inequality of maps
   169  	map1 := map[string]int{"a": 1, "b": 2}
   170  	map2 := map[string]int{"a": 1, "c": 3}
   171  	if f.Eqd(map1)(map2) {
   172  		t.Errorf("Test case 2: Expected false, got true")
   173  	}
   174  }
   175  
   176  func TestNed(t *testing.T) {
   177  	// Test case 1: Test deep inequality of slices
   178  	slice1 := []int{1, 2, 3}
   179  	slice2 := []int{1, 2, 4}
   180  	if !f.Ned(slice1)(slice2) {
   181  		t.Errorf("Test case 1: Expected true, got false")
   182  	}
   183  
   184  	// Test case 2: Test deep equality of structs
   185  	type Person struct {
   186  		Name string
   187  		Age  int
   188  	}
   189  
   190  	person1 := Person{Name: "Alice", Age: 30}
   191  	person2 := Person{Name: "Bob", Age: 25}
   192  	if !f.Ned(person1)(person2) {
   193  		t.Errorf("Test case 2: Expected true, got false")
   194  	}
   195  }
   196  
   197  func TestGt(t *testing.T) {
   198  	// Test case 1: Test greater than comparison of integers
   199  	if !f.Gt(10)(15) {
   200  		t.Errorf("Test case 1: Expected true, got false")
   201  	}
   202  
   203  	// Test case 2: Test greater than comparison of floats
   204  	if !f.Gt(3.14)(3.1416) {
   205  		t.Errorf("Test case 2: Expected true, got false")
   206  	}
   207  }
   208  
   209  func TestGte(t *testing.T) {
   210  	// Test case 1: Test greater than or equal to comparison of integers
   211  	if !f.Gte(10)(10) {
   212  		t.Errorf("Test case 1: Expected true, got false")
   213  	}
   214  
   215  	// Test case 2: Test greater than or equal to comparison of floats
   216  	if !f.Gte(3.14)(3.14) {
   217  		t.Errorf("Test case 2: Expected true, got false")
   218  	}
   219  }
   220  
   221  func TestLt(t *testing.T) {
   222  	// Test case 1: Test less than comparison of integers
   223  	if !f.Lt(5)(3) {
   224  		t.Errorf("Test case 1: Expected true, got false")
   225  	}
   226  
   227  	// Test case 2: Test less than comparison of floats
   228  	if !f.Lt(3.14)(2.71) {
   229  		t.Errorf("Test case 2: Expected true, got false")
   230  	}
   231  }
   232  
   233  func TestLte(t *testing.T) {
   234  	// Test case 1: Test less than or equal to comparison of integers
   235  	if !f.Lte(5)(5) {
   236  		t.Errorf("Test case 1: Expected true, got false")
   237  	}
   238  
   239  	// Test case 2: Test less than or equal to comparison of floats
   240  	if !f.Lte(3.14)(3.14) {
   241  		t.Errorf("Test case 2: Expected true, got false")
   242  	}
   243  }