-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_test.go
More file actions
80 lines (66 loc) · 2.03 KB
/
Copy pathcontainer_test.go
File metadata and controls
80 lines (66 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
package fasttui
import (
"sync"
"testing"
)
type containerLineComponent struct {
lines []string
}
func (c *containerLineComponent) Render(width int) []string { return c.lines }
func (c *containerLineComponent) HandleInput(string) {}
func (c *containerLineComponent) WantsKeyRelease() bool { return false }
func (c *containerLineComponent) Invalidate() {}
func TestContainer_RenderSnapshot(t *testing.T) {
c := NewContainer()
c.AddChild(&containerLineComponent{lines: []string{"a"}})
c.AddChild(&containerLineComponent{lines: []string{"b", "c"}})
got := c.Render(80)
want := []string{"a", "b", "c"}
if len(got) != len(want) {
t.Fatalf("Render() = %#v, want %#v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("line %d = %q, want %q", i, got[i], want[i])
}
}
}
func TestContainer_GetChildrenReturnsCopy(t *testing.T) {
c := NewContainer()
child := &containerLineComponent{lines: []string{"x"}}
c.AddChild(child)
children := c.GetChildren()
children[0] = nil
if c.GetChildren()[0] != child {
t.Fatal("GetChildren should return a copy, not the internal slice")
}
}
func TestContainer_RenderDoesNotHoldLockDuringChildRender(t *testing.T) {
c := NewContainer()
block := make(chan struct{})
child := &blockingRenderComponent{block: block}
c.AddChild(child)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_ = c.Render(80)
}()
// Render is blocked inside child; container mutations must still proceed.
c.AddChild(&containerLineComponent{lines: []string{"added"}})
close(block)
wg.Wait()
if len(c.GetChildren()) != 2 {
t.Fatalf("expected 2 children after concurrent AddChild, got %d", len(c.GetChildren()))
}
}
type blockingRenderComponent struct {
block chan struct{}
}
func (b *blockingRenderComponent) Render(width int) []string {
<-b.block
return []string{"done"}
}
func (b *blockingRenderComponent) HandleInput(string) {}
func (b *blockingRenderComponent) WantsKeyRelease() bool { return false }
func (b *blockingRenderComponent) Invalidate() {}