From 26a3ff944bbcbcc71483d53aef3c17d09e1d29eb Mon Sep 17 00:00:00 2001 From: Tom5521 Date: Sat, 19 Oct 2024 16:03:16 -0300 Subject: [PATCH] Add more tests --- justfile | 3 ++ pkg/v2/gtools/closures.go | 5 ++- pkg/v2/gtools/examples_test.go | 2 + pkg/v2/gtools/gtools_test.go | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 justfile create mode 100644 pkg/v2/gtools/gtools_test.go diff --git a/justfile b/justfile new file mode 100644 index 0000000..12fd31f --- /dev/null +++ b/justfile @@ -0,0 +1,3 @@ +test: + go test -v ./internal/walk/walk_test.go + go test -v ./pkg/v2/gtools/gtools_test.go diff --git a/pkg/v2/gtools/closures.go b/pkg/v2/gtools/closures.go index 6637497..b84a881 100644 --- a/pkg/v2/gtools/closures.go +++ b/pkg/v2/gtools/closures.go @@ -12,7 +12,10 @@ func NewFactorySetup(f func(listitem ListItem)) func(*glib.Object) { func NewFactoryBind(f func(listitem ListItem, pos int)) func(*glib.Object) { return func(obj *glib.Object) { - i := obj.Cast().(ListItem) + i, ok := obj.Cast().(ListItem) + if !ok { + panic("Invalid interface conversion! glib.Objector -> ListItem") + } f(i, int(i.Position())) } } diff --git a/pkg/v2/gtools/examples_test.go b/pkg/v2/gtools/examples_test.go index 9289615..0220221 100644 --- a/pkg/v2/gtools/examples_test.go +++ b/pkg/v2/gtools/examples_test.go @@ -8,6 +8,8 @@ import ( ) func ExampleFetchObjects() { + gtk.Init() + const xml = ` diff --git a/pkg/v2/gtools/gtools_test.go b/pkg/v2/gtools/gtools_test.go new file mode 100644 index 0000000..ac12950 --- /dev/null +++ b/pkg/v2/gtools/gtools_test.go @@ -0,0 +1,69 @@ +package gtools_test + +import ( + "testing" + + "github.com/Tom5521/gtk4tools/pkg/v2/gtools" + "github.com/diamondburned/gotk4/pkg/gtk/v4" +) + +func TestInitGTK(t *testing.T) { + if !gtk.InitCheck() { + t.Log("Failed to init GTK") + t.FailNow() + } +} + +func TestFetch(t *testing.T) { + const xml = ` + + + + Hello World + +` + + ui := new( + struct { + Window *gtk.Window `gtk:"window"` + }, + ) + + gtools.FetchObjects(ui, xml) + + if ui.Window == nil { + t.Log("ui.Window is nil") + t.FailNow() + } + if ui.Window.Title() != "Hello World" { + t.Log("ui.Window.Title() isn't 'Hello World'") + t.Fail() + } +} + +var ( + modeller gtools.Modeller[string] + items = []string{"1", "2", "3", "4"} +) + +func testModeller(t *testing.T) { + if len(items) != modeller.Len() { + t.Log("Modeller len is greater than slices len") + t.FailNow() + } + + if items[1] != modeller.At(1) { + t.Log("Modeller item 1 isn't equal to slice item 1") + t.FailNow() + } +} + +func TestModel(t *testing.T) { + modeller = gtools.NewModel(items...) + testModeller(t) +} + +func TestModelVar(t *testing.T) { + modeller = gtools.NewModelVar(&items) + testModeller(t) +}