no :name in LWES::Event#to_hash for subclasses
[lwes-ruby.git] / test / unit / test_event.rb
blob63fe76c9f6b97a3922b46bae7d7e2ac3fd0e7b8e
1 require "#{File.expand_path(File.dirname(__FILE__))}/../test_helper"
2 require 'ipaddr'
4 class TestEvent < Test::Unit::TestCase
6   def setup
7     @options = LISTENER_DEFAULTS.dup
8   end
10   def test_inspect
11     ev = LWES::Event.new
12     assert_instance_of String, ev.inspect
13   end
15   def test_to_hash
16     ev = LWES::Event.new
17     assert_equal({}, ev.to_hash)
18   end
20   def test_emit_recieve_hash
21     receiver = UDPSocket.new
22     receiver.bind(nil, @options[:port])
23     emitter = LWES::Emitter.new(@options)
24     tmp = {
25       :str => 'hello',
26       :uint16 => [ :uint16, 6344 ],
27       :int16 => [ :int16, -6344 ],
28       :uint32 => [ :uint32, 6344445 ],
29       :int32 => [ :int32, -6344445 ],
30       :uint64 => [ :uint64, 6344445123123 ],
31       :int64 => [ :int64, -6344445123123 ],
32       :true => true,
33       :false => false,
34       :addr => [ :ip_addr, "127.0.0.1" ],
35     }
37     emitter.emit "Event", tmp
38     buf, addr = receiver.recvfrom(65536)
39     parsed = LWES::Event.parse(buf)
40     expect = {
41       :name => "Event",
42       :uint16 => 6344,
43       :str => "hello",
44       :int16 => -6344,
45       :uint32 => 6344445,
46       :int32 => -6344445,
47       :uint64 => 6344445123123,
48       :int64 => -6344445123123,
49       :true => true,
50       :false => false,
51       :addr => "127.0.0.1",
52     }
53     assert_instance_of LWES::Event, parsed
54     assert_equal expect, parsed.to_hash
56     # test for round tripping
57     emitter.emit parsed
58     buf, addr = receiver.recvfrom(65536)
59     assert_instance_of LWES::Event, parsed
60     assert_equal expect, parsed.to_hash
62     emitter << parsed
63     buf, addr = receiver.recvfrom(65536)
64     assert_instance_of LWES::Event, parsed
65     assert_equal expect, parsed.to_hash
66     ensure
67       receiver.close
68   end
70   def test_subclass_aset_aref
71     tdb = LWES::TypeDB.new("#{File.dirname(__FILE__)}/test1.esf")
72     tmp = LWES::Event.subclass "Event1", tdb
73     e = tmp.new
74     assert_equal({}, e.to_hash)
75     vals = {
76       :t_bool => true,
77       :t_int16 => -1000,
78       :t_uint16 => 1000,
79       :t_int32 => -64444,
80       :t_uint32 => 64444,
81       :t_int64 => 10_000_000_000,
82       :t_uint64 => 10_000_000_000,
83       :t_ip_addr => "192.168.0.1",
84       :t_string => "STRING",
85       :enc => 0,
86       :st => "ruby",
87     }
88     vals.each do |k,v|
89       assert_nothing_raised { e[k.to_s] = v }
90       assert_equal v, e[k.to_s], e.to_hash.inspect
91     end
93     e2 = tmp.new
94     vals.each do |k,v|
95       assert_nothing_raised { e2[k] = v }
96       assert_equal v, e2[k], e2.to_hash.inspect
97     end
98     assert_equal e2.to_hash, e.to_hash
99     e3 = tmp.new
100     vals.each do |k,v|
101       assert_nothing_raised { e3.__send__ "#{k}=", v }
102       assert_equal v, e3.__send__(k), e3.to_hash.inspect
103     end
104     assert_equal e3.to_hash, e.to_hash
105   end
107   def test_merge
108     tdb = LWES::TypeDB.new("#{File.dirname(__FILE__)}/test1.esf")
109     tmp = LWES::Event.subclass "Event1", tdb
110     e = tmp.new.merge :t_string => "merged"
111     assert_equal "merged", e.t_string
112   end
114   def test_init_copy
115     tdb = LWES::TypeDB.new("#{File.dirname(__FILE__)}/test1.esf")
116     tmp = LWES::Event.subclass "Event1", tdb
117     a = tmp.new
118     b = a.dup
119     assert_equal a.to_hash, b.to_hash
120     a.t_string = "HELLO"
121     assert_equal "HELLO", a.t_string
122     assert_nil b.t_string
123     c = a.dup
124     assert_equal "HELLO", c.t_string
125   end
127   def test_emit_receive_subclassed
128     receiver = UDPSocket.new
129     receiver.bind(nil, @options[:port])
130     emitter = LWES::Emitter.new(@options)
131     tmp = { :t_string => 'hello' }
133     tdb = LWES::TypeDB.new("#{File.dirname(__FILE__)}/test1.esf")
134     ev1 = LWES::Event.subclass "Event1", tdb
135     emitter.emit "Event1", tmp
136     buf, _ = receiver.recvfrom(65536)
137     parsed = LWES::Event.parse(buf)
138     assert_instance_of ev1, parsed
139     assert_equal parsed.to_hash, ev1.new(tmp).to_hash
140     ensure
141       receiver.close
142   end
144   def teardown
145     LWES::Event::CLASSES.clear
146   end