|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +type SimpleStruct struct { |
| 8 | + Name string |
| 9 | + Value int |
| 10 | +} |
| 11 | + |
| 12 | +type NestedStruct struct { |
| 13 | + Simple *SimpleStruct |
| 14 | + Values []int |
| 15 | + Map map[string]interface{} |
| 16 | +} |
| 17 | + |
| 18 | +type ComplexStruct struct { |
| 19 | + Nested *NestedStruct |
| 20 | + StructMap map[string]*SimpleStruct |
| 21 | + StructArr []*SimpleStruct |
| 22 | +} |
| 23 | + |
| 24 | +func TestIsEmpty(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + value interface{} |
| 28 | + expected bool |
| 29 | + }{ |
| 30 | + // Nil values |
| 31 | + {"nil", nil, true}, |
| 32 | + {"nil pointer", (*SimpleStruct)(nil), true}, |
| 33 | + |
| 34 | + // Basic types |
| 35 | + {"empty string", "", true}, |
| 36 | + {"non-empty string", "test", false}, |
| 37 | + {"zero int", 0, true}, |
| 38 | + {"non-zero int", 42, false}, |
| 39 | + {"false bool", false, true}, |
| 40 | + {"true bool", true, false}, |
| 41 | + |
| 42 | + // Slices and arrays |
| 43 | + {"empty slice", []int{}, true}, |
| 44 | + {"nil slice", []int(nil), true}, |
| 45 | + {"non-empty slice", []int{1, 2, 3}, false}, |
| 46 | + {"slice with zero values", []int{0, 0, 0}, true}, |
| 47 | + {"slice with mixed values", []int{0, 1, 0}, false}, |
| 48 | + |
| 49 | + // Maps |
| 50 | + {"empty map", map[string]int{}, true}, |
| 51 | + {"nil map", map[string]int(nil), true}, |
| 52 | + {"map with values", map[string]int{"one": 1, "two": 2}, false}, |
| 53 | + {"map with zero values", map[string]int{"one": 0, "two": 0}, true}, |
| 54 | + {"map with mixed values", map[string]int{"one": 0, "two": 2}, false}, |
| 55 | + |
| 56 | + // Simple struct |
| 57 | + {"empty struct", SimpleStruct{}, true}, |
| 58 | + {"partially filled struct", SimpleStruct{Name: "test"}, false}, |
| 59 | + {"fully filled struct", SimpleStruct{Name: "test", Value: 42}, false}, |
| 60 | + |
| 61 | + // Pointers to simple structs |
| 62 | + {"pointer to empty struct", &SimpleStruct{}, true}, |
| 63 | + {"pointer to partially filled struct", &SimpleStruct{Name: "test"}, false}, |
| 64 | + |
| 65 | + // Nested structs with nil fields |
| 66 | + {"nested struct with all nil", NestedStruct{}, true}, |
| 67 | + {"nested struct with simple", NestedStruct{Simple: &SimpleStruct{Name: "test"}}, false}, |
| 68 | + {"nested struct with empty array", NestedStruct{Values: []int{}}, true}, |
| 69 | + {"nested struct with non-empty array", NestedStruct{Values: []int{1, 2}}, false}, |
| 70 | + |
| 71 | + // Complex nested scenarios |
| 72 | + { |
| 73 | + "complex all empty", |
| 74 | + &ComplexStruct{ |
| 75 | + Nested: &NestedStruct{ |
| 76 | + Simple: &SimpleStruct{}, |
| 77 | + Values: []int{}, |
| 78 | + Map: map[string]interface{}{}, |
| 79 | + }, |
| 80 | + StructMap: map[string]*SimpleStruct{}, |
| 81 | + StructArr: []*SimpleStruct{}, |
| 82 | + }, |
| 83 | + true, |
| 84 | + }, |
| 85 | + { |
| 86 | + "complex with one non-empty value", |
| 87 | + &ComplexStruct{ |
| 88 | + Nested: &NestedStruct{ |
| 89 | + Simple: &SimpleStruct{Name: "test"}, |
| 90 | + Values: []int{}, |
| 91 | + Map: map[string]interface{}{}, |
| 92 | + }, |
| 93 | + StructMap: map[string]*SimpleStruct{}, |
| 94 | + StructArr: []*SimpleStruct{}, |
| 95 | + }, |
| 96 | + false, |
| 97 | + }, |
| 98 | + { |
| 99 | + "complex with non-empty map", |
| 100 | + &ComplexStruct{ |
| 101 | + Nested: &NestedStruct{ |
| 102 | + Simple: &SimpleStruct{}, |
| 103 | + Values: []int{}, |
| 104 | + Map: map[string]interface{}{"key": "value"}, |
| 105 | + }, |
| 106 | + StructMap: map[string]*SimpleStruct{}, |
| 107 | + StructArr: []*SimpleStruct{}, |
| 108 | + }, |
| 109 | + false, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, test := range tests { |
| 114 | + t.Run(test.name, func(t *testing.T) { |
| 115 | + result := IsEmpty(test.value) |
| 116 | + if result != test.expected { |
| 117 | + t.Errorf("IsEmpty(%v) = %v, expected %v", test.value, result, test.expected) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments