* Added command line tool example similar to 'sopranocmd'
[kdebindings.git] / ruby / qtruby / test / unittests.rb
blobfd9c690da0ad6fada3a6e822ff20c451d71df8ce
1 require 'Qt'
2 require 'test/unit'
4 class TestQtRuby < Test::Unit::TestCase
6   def setup
7     @app = Qt::Application.instance || Qt::Application.new(ARGV)
8     assert @app
9   end
11   def test_link_against_qt4
12     assert_raise(NoMethodError) { @app.setMainWidget(nil) }
13   end
15   def test_qapplication_methods
16    assert @app == Qt::Application::instance
17    assert @app == Qt::CoreApplication::instance
18    assert @app == Qt::Application.instance
19    assert @app == Qt::CoreApplication.instance
20    assert @app == $qApp
21   end
23   def test_qapplication_inheritance
24    assert @app.inherits("Qt::Application")
25    assert @app.inherits("Qt::CoreApplication")
26    assert @app.inherits("Qt::Object")
27   end
29   def test_widget_inheritance
30     widget = Qt::Widget.new(nil)
31     assert widget.inherits("Qt::Widget")
32     assert widget.inherits("Qt::Object")
33     assert widget.inherits("QObject")
34   end
36   def test_qstring_marshall
37     widget = Qt::Widget.new(nil)
38     assert widget.objectName.nil?
39     widget.objectName = "Barney"
40     assert widget.objectName == "Barney"
41   end
43   def test_widgetlist
44     w1 = Qt::Widget.new(nil)
45     w2 = Qt::Widget.new(w1)
46     w3 = Qt::Widget.new(w1)
47     w4 = Qt::Widget.new(w2)
49     assert w1.children == [ w2, w3 ]
50   end
52   def test_find_children
53     w = Qt::Widget.new(nil)
54     assert_raise(TypeError) { w.findChildren(nil) }
56     assert w.findChildren(Qt::Widget) == [ ]
57     w2 = Qt::Widget.new(w)
59     assert w.findChildren(Qt::Widget) == [ w2 ]
60     assert w.findChildren(Qt::Object) == [ w2 ]
61     assert w.findChildren(Qt::LineEdit) == [ ]
62     assert w.findChildren(Qt::Widget,"Bob") == [ ]
63     assert w.findChildren(Qt::Object,"Bob") == [ ]
65     w2.objectName = "Bob"
67     assert w.findChildren(Qt::Widget) == [ w2 ]
68     assert w.findChildren(Qt::Object) == [ w2 ]
69     assert w.findChildren(Qt::Widget,"Bob") == [ w2 ]
70     assert w.findChildren(Qt::Object,"Bob") == [ w2 ]
71     assert w.findChildren(Qt::LineEdit, "Bob") == [ ]
73     w3 = Qt::Widget.new(w)
74     w4 = Qt::LineEdit.new(w2)
75     w4.setObjectName("Bob")
77     assert w.findChildren(Qt::Widget) == [ w4, w2, w3 ]
78     assert w.findChildren(Qt::LineEdit) == [ w4 ]
79     assert w.findChildren(Qt::Widget,"Bob") == [ w4, w2 ]    
80     assert w.findChildren(Qt::LineEdit,"Bob") == [ w4 ]    
81   end
83   def test_find_child
84     w = Qt::Widget.new(nil)
85     assert_raise(TypeError) { w.findChild(nil) }
87     assert_nil w.findChild(Qt::Widget)
88     w2 = Qt::Widget.new(w)
90     w3 = Qt::Widget.new(w)
91     w3.objectName = "Bob"
92     w4 = Qt::LineEdit.new(w2)
93     w4.objectName = "Bob"
95     assert w.findChild(Qt::Widget,"Bob") == w3
96     assert w.findChild(Qt::LineEdit,"Bob") == w4
97   end
99   def test_boolean_marshalling
100     assert Qt::Variant.new(true).toBool
101     assert !Qt::Variant.new(false).toBool
103     assert !Qt::Boolean.new(true).nil?
104     assert Qt::Boolean.new(false).nil?
106     # Invalid variant conversion should change b to false
107     b = Qt::Boolean.new(true)
108     v = Qt::Variant.new("Blah")
109     v.toInt(b);
111     assert b.nil?
112   end
114   def test_intp_marshalling
115     assert Qt::Integer.new(100).value == 100
116   end
118   def test_variant_conversions
119     v = Qt::Variant.new(Qt::Variant::Invalid)
121     assert !v.isValid
122     assert v.isNull
124     v = Qt::Variant.new(55)
125     assert v.toInt == 55
126     assert v.toUInt == 55
127     assert v.toLongLong == 55
128     assert v.toULongLong == 55
129     assert Qt::Variant.new(-55).toLongLong == -55
130     assert Qt::Variant.new(-55).toULongLong == 18446744073709551561
131     assert v.toDouble == 55.0
132     assert v.toChar == Qt::Char.new(55)
133     assert v.toString == "55"
134     assert v.toStringList == [ ]
137     assert Qt::Variant.new("Blah").toStringList == [ "Blah" ]
139     assert Qt::Variant.new(Qt::Size.new(30,40)).toSize == Qt::Size.new(30,40)
140     assert Qt::Variant.new(Qt::SizeF.new(20,30)).toSizeF == Qt::SizeF.new(20,30)
142     assert Qt::Variant.new(Qt::Rect.new(30,40,10,10)).toRect == Qt::Rect.new(30,40,10,10)
143     assert Qt::Variant.new(Qt::RectF.new(20,30,10,10)).toRectF == Qt::RectF.new(20,30,10,10)
145     assert Qt::Variant.new(Qt::Point.new(30,40)).toPoint == Qt::Point.new(30,40)
146     assert Qt::Variant.new(Qt::PointF.new(20,30)).toPointF == Qt::PointF.new(20,30)
149   end