Skip to content

Commit 79781ed

Browse files
committed
debugui: improve tests for control IDs
Closes #18
1 parent 754f1ce commit 79781ed

File tree

6 files changed

+75
-43
lines changed

6 files changed

+75
-43
lines changed

control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ func (c *Context) Label(text string) {
152152
})
153153
}
154154

155-
func (c *Context) button(label string, idStr string, opt option) bool {
155+
func (c *Context) button(label string, idStr string, opt option) (controlID, bool) {
156156
id := c.idFromString(idStr)
157-
return c.control(id, opt, func(bounds image.Rectangle) Response {
157+
return id, c.control(id, opt, func(bounds image.Rectangle) Response {
158158
var res Response
159159
// handle click
160160
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {

export_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3+
4+
package debugui
5+
6+
import "strings"
7+
8+
type ControlID = controlID
9+
10+
func (c *Context) ButtonID(label string) ControlID {
11+
label, idStr, _ := strings.Cut(label, idSeparator)
12+
id, _ := c.button(label, idStr, optionAlignCenter)
13+
return id
14+
}

id_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3+
4+
package debugui_test
5+
6+
import (
7+
"image"
8+
"testing"
9+
10+
"github.com/ebitengine/debugui"
11+
)
12+
13+
func TestMultipleButtonsInForLoop(t *testing.T) {
14+
d := debugui.New()
15+
d.Update(func(ctx *debugui.Context) {
16+
ctx.Window("Window", image.Rect(0, 0, 100, 100), func(layout debugui.ContainerLayout) {
17+
var id debugui.ControlID
18+
for range 10 {
19+
id2 := ctx.ButtonID("a")
20+
if id2 == 0 {
21+
t.Errorf("Caller() returned 0")
22+
continue
23+
}
24+
if id == 0 {
25+
id = id2
26+
continue
27+
}
28+
if id != id2 {
29+
t.Errorf("Caller() returned different values: %d and %d", id, id2)
30+
}
31+
}
32+
if id == 0 {
33+
t.Errorf("Caller() returned 0")
34+
}
35+
})
36+
})
37+
}
38+
39+
func TestMultipleButtonsOnOneLine(t *testing.T) {
40+
d := debugui.New()
41+
d.Update(func(ctx *debugui.Context) {
42+
ctx.Window("Window", image.Rect(0, 0, 100, 100), func(layout debugui.ContainerLayout) {
43+
idA := ctx.ButtonID("a")
44+
idB := ctx.ButtonID("b")
45+
if idA == idB {
46+
t.Errorf("Button() returned the same value twice: %d", idA)
47+
}
48+
idC, idD := ctx.ButtonID("c"), ctx.ButtonID("d")
49+
if idC == idD {
50+
t.Errorf("Button() returned the same value twice: %d", idC)
51+
}
52+
})
53+
})
54+
}

internal/caller/caller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
)
1919

2020
// Caller returns a program counter of the caller outside of this module.
21-
func Caller() (pc uintptr) {
21+
func Caller() uintptr {
2222
debugUIFileDirOnce.Do(func() {
2323
pkg, err := build.Default.Import("github.com/ebitengine/debugui", "", build.FindOnly)
2424
if err != nil {
@@ -40,7 +40,8 @@ func Caller() (pc uintptr) {
4040
// The file should be with a slash, but just in case, convert it.
4141
file = filepath.ToSlash(file)
4242

43-
if strings.HasSuffix(path.Base(file), "_test.go") {
43+
// TODO: This is heuristic. Use a file set.
44+
if strings.HasSuffix(path.Base(file), "_test.go") && path.Base(file) != "export_test.go" {
4445
return pc
4546
}
4647

internal/caller/caller_test.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

widget.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const idSeparator = "\x00"
1212

1313
func (c *Context) Button(label string) bool {
1414
label, idStr, _ := strings.Cut(label, idSeparator)
15-
return c.button(label, idStr, optionAlignCenter)
15+
_, result := c.button(label, idStr, optionAlignCenter)
16+
return result
1617
}
1718

1819
func (c *Context) TextBox(buf *string) Response {

0 commit comments

Comments
 (0)