1 require "#{File.dirname(__FILE__)}/../test_helper"
3 class InvalidStruct1 < Struct.new(:invalid)
7 class TestEmitStruct < Test::Unit::TestCase
10 assert_kind_of Class, self.class.const_get(:Event1)
11 # assert self.class.const_get(:Event1).kind_of?(Struct)
12 @options = LISTENER_DEFAULTS.dup
15 def test_emit_non_lwes_struct
16 emitter = LWES::Emitter.new(@options)
17 assert_raise(ArgumentError) { emitter << InvalidStruct1.new }
21 emitter = LWES::Emitter.new(@options)
22 assert_raise(ArgumentError) { emitter << "HHI" }
23 assert_raise(ArgumentError) { emitter << [] }
24 assert_raise(ArgumentError) { emitter << {} }
27 def test_emit_struct_full
29 out = lwes_listener do
30 assert_nothing_raised do
31 emitter = LWES::Emitter.new(@options)
38 s.t_int64 = 10_000_000_000
39 s.t_uint64 = 10_000_000_000
40 s.t_ip_addr = '192.168.0.1'
47 value = s[m.to_sym] or next
48 regex = /\b#{m} = #{value};/
49 assert_equal 1, out.grep(regex).size,
50 "#{regex.inspect} didn't match #{out.inspect}"
54 def test_emit_struct_cplusplus
56 out = lwes_listener do
57 assert_nothing_raised do
58 emitter = LWES::Emitter.new(@options)
65 s.t_int64 = 10_000_000_000
66 s.t_uint64 = 10_000_000_000
67 s.t_ip_addr = '192.168.0.1'
74 value = s[m.to_sym] or next
75 regex = /\b#{m} = #{value};/
76 assert_equal 1, out.grep(regex).size,
77 "#{regex.inspect} didn't match #{out.inspect}"
81 def test_emit_from_class
88 :t_int64 => 10_000_000_000,
89 :t_uint64 => 10_000_000_000,
90 :t_ip_addr => '192.168.0.1',
91 :t_string => "STRING",
93 out = lwes_listener do
94 assert_nothing_raised do
95 emitter = LWES::Emitter.new(@options)
96 emitter.emit(Event1, opt)
100 opt.each do |m, value|
101 regex = /\b#{m} = #{value};/
102 assert_equal 1, out.grep(regex).size,
103 "#{regex.inspect} didn't match #{out.inspect}"
107 def test_emit_from_class_bad_type
108 out = lwes_listener do
109 e = assert_raises(TypeError) do
110 emitter = LWES::Emitter.new(@options)
116 :t_int64 => 10_000_000_000,
117 :t_uint64 => 10_000_000_000,
118 :t_ip_addr => '192.168.0.1',
119 :t_string => true, #"STRING",
121 emitter.emit(Event1, opt)
124 assert out.readlines.empty?
127 def test_emit_alt_class_name
128 out = lwes_listener do
129 emitter = LWES::Emitter.new(@options)
130 emitter.emit(Event, :t_uint32 => 16384)
133 assert_match %r{^Event1\[\d+\]}, out.first
138 ESF_FILE = "#{File.dirname(__FILE__)}/test1.esf"
139 LWES::Struct.new(:file=>ESF_FILE,
140 :parent => TestEmitStruct)
141 LWES::Struct.new(:file=>ESF_FILE,
142 :parent => TestEmitStruct,