gitstack

grindlemire/go-tui code browser

2.3 KB Go 108 lines 2026-06-03 · d15bb9f raw
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
106
107
108
package tui

import (
	"sync"
	"testing"
)

func TestDirty_MarkDirty(t *testing.T) {
	// Reset state before test
	testApp.resetDirty()

	// Initially not dirty
	if testApp.checkAndClearDirty() {
		t.Error("checkAndClearDirty() should return false when not marked dirty")
	}

	// Mark dirty
	testApp.MarkDirty()

	// Now should be dirty
	if !testApp.checkAndClearDirty() {
		t.Error("checkAndClearDirty() should return true after MarkDirty()")
	}
}

func TestDirty_CheckAndClearDirty(t *testing.T) {
	type tc struct {
		markDirty    bool
		expectFirst  bool
		expectSecond bool
	}

	tests := map[string]tc{
		"returns true and clears flag when dirty": {
			markDirty:    true,
			expectFirst:  true,
			expectSecond: false,
		},
		"returns false when not dirty": {
			markDirty:    false,
			expectFirst:  false,
			expectSecond: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			// Reset state before test
			testApp.resetDirty()

			if tt.markDirty {
				testApp.MarkDirty()
			}

			// First check
			first := testApp.checkAndClearDirty()
			if first != tt.expectFirst {
				t.Errorf("first checkAndClearDirty() = %v, want %v", first, tt.expectFirst)
			}

			// Second check should always be false (flag was cleared)
			second := testApp.checkAndClearDirty()
			if second != tt.expectSecond {
				t.Errorf("second checkAndClearDirty() = %v, want %v", second, tt.expectSecond)
			}
		})
	}
}

func TestDirty_ConcurrentMarkDirty(t *testing.T) {
	// Reset state before test
	testApp.resetDirty()

	// Spawn multiple goroutines that call MarkDirty concurrently
	var wg sync.WaitGroup
	const numGoroutines = 100

	for range numGoroutines {
		wg.Go(func() {
			testApp.MarkDirty()
		})
	}

	wg.Wait()

	// After all goroutines complete, dirty flag should be set
	if !testApp.checkAndClearDirty() {
		t.Error("checkAndClearDirty() should return true after concurrent MarkDirty() calls")
	}

	// And now it should be cleared
	if testApp.checkAndClearDirty() {
		t.Error("checkAndClearDirty() should return false after first check cleared the flag")
	}
}

func TestDirty_ResetDirty(t *testing.T) {
	// Mark dirty
	testApp.MarkDirty()

	// Reset should clear
	testApp.resetDirty()

	// Should not be dirty anymore
	if testApp.checkAndClearDirty() {
		t.Error("checkAndClearDirty() should return false after resetDirty()")
	}
}