2024-09-07 02:47:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2024-11-04 08:32:39 +00:00
|
|
|
"strings"
|
2024-09-07 02:47:11 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mappu/miqt/qt"
|
|
|
|
)
|
|
|
|
|
2024-10-19 02:53:05 +00:00
|
|
|
func testMarshalling(t *testing.T) {
|
2024-09-07 02:47:11 +00:00
|
|
|
|
|
|
|
// Bool
|
|
|
|
t.Run("Bool", func(t *testing.T) {
|
2024-10-26 01:18:41 +00:00
|
|
|
b := qt.NewQCheckBox2()
|
2024-09-07 02:47:11 +00:00
|
|
|
expect := true
|
|
|
|
b.SetChecked(expect)
|
|
|
|
|
|
|
|
got := b.IsChecked()
|
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Bool: expected %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Int
|
|
|
|
t.Run("Int", func(t *testing.T) {
|
|
|
|
s := qt.NewQSize()
|
|
|
|
expect := 128
|
|
|
|
s.SetWidth(expect)
|
|
|
|
|
|
|
|
// Get
|
|
|
|
got := s.Width()
|
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Int: expected %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// QString
|
|
|
|
t.Run("QString", func(t *testing.T) {
|
2024-10-26 01:18:41 +00:00
|
|
|
w := qt.NewQWidget2()
|
2024-09-07 02:47:11 +00:00
|
|
|
expect := "Sample text"
|
|
|
|
w.SetToolTip(expect)
|
|
|
|
|
|
|
|
got := w.ToolTip()
|
|
|
|
if got != expect {
|
|
|
|
t.Errorf("QString: expected %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// QList<int>
|
|
|
|
t.Run("QList<int>", func(t *testing.T) {
|
|
|
|
expect := []int{10, 20, 30, 40, 50}
|
|
|
|
s := qt.NewQVersionNumber2(expect)
|
|
|
|
|
|
|
|
got := s.Segments()
|
|
|
|
if !reflect.DeepEqual(got, expect) {
|
|
|
|
t.Errorf("QList<int>: expected %#v, got %#v", expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// QStringList
|
|
|
|
t.Run("QStringList", func(t *testing.T) {
|
2024-10-26 01:18:41 +00:00
|
|
|
c := qt.NewQInputDialog2()
|
2024-09-07 02:47:11 +00:00
|
|
|
expect := []string{"foo", "bar", "baz", "quux"}
|
|
|
|
c.SetComboBoxItems(expect)
|
|
|
|
|
|
|
|
got := c.ComboBoxItems()
|
|
|
|
if !reflect.DeepEqual(got, expect) {
|
|
|
|
t.Errorf("QStringList: expected %#v, got %#v", expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// QList<Qt type>
|
|
|
|
t.Run("QList<Q*>", func(t *testing.T) {
|
|
|
|
|
|
|
|
var expect []qt.QKeySequence
|
|
|
|
expect = append(expect, *qt.QKeySequence_FromString("F1"))
|
|
|
|
expect = append(expect, *qt.QKeySequence_FromString("F2"))
|
|
|
|
expect = append(expect, *qt.QKeySequence_FromString("F3"))
|
|
|
|
|
|
|
|
c := qt.NewQAction()
|
|
|
|
c.SetShortcuts(expect)
|
|
|
|
|
|
|
|
got := c.Shortcuts()
|
|
|
|
if len(expect) != len(got) {
|
|
|
|
t.Fatalf("QList<Q*>: expected len=%d, got len=%d", len(expect), len(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(expect); i++ {
|
|
|
|
if got[i].ToString() != expect[i].ToString() {
|
|
|
|
t.Errorf("QList<Q*>: expected %#v, got %#v", expect, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-04 08:32:39 +00:00
|
|
|
// QByteArray
|
2024-10-18 23:46:15 +00:00
|
|
|
t.Run("QByteArray", func(t *testing.T) {
|
2024-09-07 02:47:11 +00:00
|
|
|
|
2024-10-18 23:46:15 +00:00
|
|
|
input := "foo bar baz"
|
|
|
|
ba := qt.QFile_EncodeName(input)
|
|
|
|
got := qt.QFile_DecodeName(ba)
|
2024-09-07 02:47:11 +00:00
|
|
|
|
2024-10-18 23:46:15 +00:00
|
|
|
if input != got {
|
|
|
|
t.Fatalf("QByteArray: expected %q, got %q", input, got)
|
2024-09-07 02:47:11 +00:00
|
|
|
}
|
2024-11-04 08:32:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// QMap
|
|
|
|
t.Run("QMap", func(t *testing.T) {
|
|
|
|
input := make(map[string]qt.QVariant)
|
|
|
|
input["foo"] = *qt.NewQVariant14("FOO")
|
|
|
|
input["bar"] = *qt.NewQVariant14("BAR")
|
|
|
|
input["baz"] = *qt.NewQVariant14("BAZ")
|
|
|
|
|
|
|
|
qtobj := qt.QJsonObject_FromVariantMap(input)
|
|
|
|
got := qtobj.ToVariantMap()
|
|
|
|
|
|
|
|
if len(got) != len(input) {
|
|
|
|
t.Fatalf("QMap: expected len %d, got len %d", len(input), len(got))
|
|
|
|
}
|
2024-09-07 02:47:11 +00:00
|
|
|
|
2024-11-04 08:32:39 +00:00
|
|
|
for src_key, _ := range input {
|
|
|
|
qvalue, ok := got[src_key]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("QMap: missing entry %q", src_key)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotValue := qvalue.ToString()
|
|
|
|
expectValue := strings.ToUpper(src_key)
|
|
|
|
if gotValue != expectValue {
|
|
|
|
t.Fatalf("QMap: single value expected %q, got %q", expectValue, gotValue)
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 02:47:11 +00:00
|
|
|
})
|
2024-11-04 08:32:39 +00:00
|
|
|
|
2024-10-19 02:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalling(t *testing.T) {
|
|
|
|
|
|
|
|
qt.NewQApplication(os.Args)
|
|
|
|
|
|
|
|
// In case of heap corruption, run the whole test multiple times.
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
testMarshalling(t)
|
|
|
|
}
|
2024-09-07 02:47:11 +00:00
|
|
|
|
|
|
|
}
|