-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcallback_test.go
More file actions
105 lines (86 loc) · 2.03 KB
/
callback_test.go
File metadata and controls
105 lines (86 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package logify
import (
"testing"
)
func TestCallback_Set(t *testing.T) {
tests := []struct {
name string
value string
shouldCall bool
expectError bool
}{
{"true", "true", true, false},
{"1", "1", true, false},
{"false", "false", false, false},
{"0", "0", false, false},
{"invalid", "invalid", false, true},
{"empty", "", false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
called := false
callback := &Callback{
fn: func() {
called = true
},
}
err := callback.Set(tt.value)
if (err != nil) != tt.expectError {
t.Errorf("Set() error = %v, expectError %v", err, tt.expectError)
}
if !tt.expectError && called != tt.shouldCall {
t.Errorf("callback called = %v, want %v", called, tt.shouldCall)
}
})
}
}
func TestCallback_String(t *testing.T) {
callback := &Callback{fn: func() {}}
if callback.String() != "false" {
t.Errorf("String() = %q, want \"false\"", callback.String())
}
}
func TestCallback_IsBoolFlag(t *testing.T) {
callback := &Callback{fn: func() {}}
if !callback.IsBoolFlag() {
t.Error("IsBoolFlag() = false, want true")
}
}
func TestCallback_NilFunction(t *testing.T) {
callback := &Callback{fn: nil}
// Should not panic even with nil function
err := callback.Set("true")
if err != nil {
t.Errorf("Set() with nil function error = %v, want nil", err)
}
}
func TestCallback_MultipleCalls(t *testing.T) {
callCount := 0
callback := &Callback{
fn: func() {
callCount++
},
}
// Call multiple times
callback.Set("true")
callback.Set("false") // Should not increment
callback.Set("true")
if callCount != 2 {
t.Errorf("function called %d times, want 2", callCount)
}
}
func TestCallback_WithState(t *testing.T) {
var result string
callback := &Callback{
fn: func() {
result = "callback executed"
},
}
if result != "" {
t.Error("result should be empty before callback")
}
callback.Set("true")
if result != "callback executed" {
t.Errorf("result = %q, want \"callback executed\"", result)
}
}