19 |
|
|
20 |
class Test_DataBus < TestCase |
class Test_DataBus < TestCase |
21 |
|
|
22 |
def test_01_subscribe |
def test_1_subscribe |
23 |
databus = FreeRIDE::DataBus.new |
bus = FreeRIDE::DataBus.new |
24 |
databus["/"].subscribe {|event, slot| puts "data to #{slot.path}"} |
parentNotified = slotNotified = false |
25 |
id = databus["/foo/bar"].subscribe {|event, slot| puts "Block got #{slot.data} event #{event.to_s}" } |
bus["/"].subscribe do |event, slot| |
26 |
databus["/foo/bar"].data = "data :-)" #=> publishes data |
if event == :notify_data_set |
27 |
databus["/foo/bar"].unsubscribe(id) |
assert_equal("/foo/bar/", slot.path, "Parent was not notified of corrent slot") |
28 |
databus["/foo/bar/int"].validate_with("Does not implement to_i") { | value | value.respond_to? "to_i" } |
assert_equal("test", slot.data, "Parent was not notified with correct data") |
29 |
databus["/foo/bar/int"].data = Hash.new #=> raises Does not implement to_i |
parentNotified = true |
30 |
|
end |
31 |
|
end |
32 |
|
id = bus["/foo/bar"].subscribe do |event, slot| |
33 |
|
if event == :notify_data_set |
34 |
|
assert_equal("test", slot.data, "Slot was not notified with correct data") |
35 |
|
slotNotified = true |
36 |
|
end |
37 |
|
end |
38 |
|
bus["/foo/bar"].data = "test" |
39 |
|
assert_equal("test", bus["/foo/bar"].data, "Value was not set correctly") |
40 |
|
assert(parentNotified, "Parent of /foo/bar was not notifed") |
41 |
|
assert(slotNotified, "Slot /foo/bar was not notified") |
42 |
|
bus["/foo/bar"].unsubscribe(id) |
43 |
|
slotNotified = parentNotified = false |
44 |
|
bus["/foo/bar"].data = "test" |
45 |
|
assert_equal("test", bus["/foo/bar"].data, "Value was not set correctly") |
46 |
|
assert(!slotNotified, "Slot unsubscribed but was notified anyway") |
47 |
|
assert(parentNotified, "Parent is still subscribed but was not notified") |
48 |
|
bus["/foo/bar"].propogate_notifications=false |
49 |
|
parentNotified = false |
50 |
|
bus["/foo/bar"].data = "test" |
51 |
|
assert_equal("test", bus["/foo/bar"].data, "Value was not set correctly") |
52 |
|
assert(!parentNotified, "Propogation of notifications was suspended but was notifed anyway") |
53 |
|
#TODO: nofications for other types of slots and slot creation events |
54 |
|
end |
55 |
|
|
56 |
|
def test_2_validate |
57 |
|
bus = FreeRIDE::DataBus.new |
58 |
|
bus["/foo/bar/int"].validate_with("Does not implement to_i") { | value | value.respond_to? "to_i" } |
59 |
|
bus["/foo/bar/int"].data = 1 |
60 |
|
assert_equal(1, bus["/foo/bar/int"].data) |
61 |
|
assert_exception(RuntimeError) {bus["/foo/bar/int"].data = Hash.new} |
62 |
|
end |
63 |
|
|
64 |
|
def test_3_slottype_data |
65 |
|
bus = FreeRIDE::DataBus.new |
66 |
|
slot = bus["slot"] |
67 |
|
slot.data = 1 |
68 |
|
assert(slot.is_data_slot?) |
69 |
|
assert(!slot.is_queue_slot?) |
70 |
|
assert(!slot.is_stack_slot?) |
71 |
|
assert(!slot.is_proc_slot?) |
72 |
|
assert_equal(1, slot.data) |
73 |
|
assert_exception(RuntimeError) {slot.proc} |
74 |
|
assert_exception(RuntimeError) {slot.queue} |
75 |
|
assert_exception(RuntimeError) {slot.stack} |
76 |
|
assert_exception(RuntimeError) {slot.set_proc {|param| return param} } |
77 |
|
assert_exception(RuntimeError) {slot.call(2) } |
78 |
|
assert_exception(RuntimeError) {slot << 1} |
79 |
|
assert_exception(RuntimeError) {slot.join(1)} |
80 |
|
assert_exception(RuntimeError) {slot.leave} |
81 |
|
assert_exception(RuntimeError) {slot.push(1)} |
82 |
|
assert_exception(RuntimeError) {slot.pop} |
83 |
|
end |
84 |
|
|
85 |
|
def test_4_slottype_queue |
86 |
|
bus = FreeRIDE::DataBus.new |
87 |
|
slot = bus["slot"] |
88 |
|
slot << 1 |
89 |
|
assert(slot.is_queue_slot?) |
90 |
|
assert(!slot.is_data_slot?) |
91 |
|
assert(!slot.is_stack_slot?) |
92 |
|
assert(!slot.is_proc_slot?) |
93 |
|
slot.join(2) |
94 |
|
assert_equal(2, slot.count) |
95 |
|
assert_equal(1, slot.leave) |
96 |
|
assert_equal(2, slot.leave) |
97 |
|
assert_equal(0, slot.count) |
98 |
|
assert_exception(RuntimeError) {slot.proc} |
99 |
|
assert_exception(RuntimeError) {slot.data} |
100 |
|
assert_exception(RuntimeError) {slot.stack} |
101 |
|
assert_exception(RuntimeError) {slot.call(2) } |
102 |
|
assert_exception(RuntimeError) {slot.set_proc {|param| return param} } |
103 |
|
assert_exception(RuntimeError) {slot.push(1)} |
104 |
|
assert_exception(RuntimeError) {slot.pop} |
105 |
end |
end |
106 |
|
|
107 |
|
def test_5_slottype_stack |
108 |
|
bus = FreeRIDE::DataBus.new |
109 |
|
slot = bus["slot"] |
110 |
|
slot.push 1 |
111 |
|
assert(slot.is_stack_slot?) |
112 |
|
assert(!slot.is_queue_slot?) |
113 |
|
assert(!slot.is_data_slot?) |
114 |
|
assert(!slot.is_proc_slot?) |
115 |
|
slot.push 2 |
116 |
|
assert_equal(2, slot.count) |
117 |
|
assert_equal(2, slot.pop) |
118 |
|
assert_equal(1, slot.pop) |
119 |
|
assert_equal(0, slot.count) |
120 |
|
assert_exception(RuntimeError) {slot.proc} |
121 |
|
assert_exception(RuntimeError) {slot.data} |
122 |
|
assert_exception(RuntimeError) {slot.queue} |
123 |
|
assert_exception(RuntimeError) {slot.set_proc {|param| return param} } |
124 |
|
assert_exception(RuntimeError) {slot.call(2) } |
125 |
|
assert_exception(RuntimeError) {slot << 1} |
126 |
|
assert_exception(RuntimeError) {slot.join(1)} |
127 |
|
assert_exception(RuntimeError) {slot.leave} |
128 |
|
end |
129 |
|
|
130 |
|
def test_6_slottype_proc |
131 |
|
bus = FreeRIDE::DataBus.new |
132 |
|
slot = bus["slot"] |
133 |
|
slot.set_proc { |param| assert_equals(4, param) } |
134 |
|
assert(slot.is_proc_slot?) |
135 |
|
assert(!slot.is_stack_slot?) |
136 |
|
assert(!slot.is_queue_slot?) |
137 |
|
assert(!slot.is_data_slot?) |
138 |
|
slot.call(4) |
139 |
|
slot.proc.call(4) |
140 |
|
assert_exception(RuntimeError) {slot.stack} |
141 |
|
assert_exception(RuntimeError) {slot.data} |
142 |
|
assert_exception(RuntimeError) {slot.queue} |
143 |
|
assert_exception(RuntimeError) {slot << 1} |
144 |
|
assert_exception(RuntimeError) {slot.join(1)} |
145 |
|
assert_exception(RuntimeError) {slot.leave} |
146 |
|
assert_exception(RuntimeError) {slot.push(1)} |
147 |
|
assert_exception(RuntimeError) {slot.pop} |
148 |
|
end |
149 |
|
|
150 |
|
def test_7_slot_attributes |
151 |
|
bus = FreeRIDE::DataBus.new |
152 |
|
slot = bus["test/slot"] |
153 |
|
slot.attr_test=1 |
154 |
|
slot.attr_test2=2 |
155 |
|
assert_equal(1, slot.attr_test) |
156 |
|
assert_equal(2, slot.attr_test2) |
157 |
|
assert_nil(slot.attr_test3) |
158 |
|
assert_exception(RuntimeError) {slot.attr_parent=nil} |
159 |
|
assert_exception(RuntimeError) {slot.attr_name=nil} |
160 |
|
assert_exception(RuntimeError) {slot.attr_path=nil} |
161 |
|
assert_equal("slot", slot.attr_name) |
162 |
|
assert_equal(slot.name, slot.attr_name) |
163 |
|
assert_equal("test", slot.attr_parent.name) |
164 |
|
assert_equal(slot.parent.name, slot.attr_parent.name) |
165 |
|
assert_equal("/test/slot/", slot.attr_path) |
166 |
|
assert_equal(slot.path, slot.attr_path) |
167 |
|
end |
168 |
|
|
169 |
end |
end |