root/lang/ruby/ruwin/test/ruwin/test_component.rb @ 28955

Revision 28955, 6.0 kB (checked in by isaisstillalive, 6 years ago)
  • プロパティと同名のアクセッサがすでに定義されている場合の処理が甘かったので修正。
Line 
1require File.expand_path(File.join(File.dirname(__FILE__), "..", "helper"))
2require "ruwin/component"
3
4class TestRuwinComponent < Test::Unit::TestCase
5  include Ruwin::Const::Window
6 
7  def test_component_property_accessor
8    klass = Class.new(Ruwin::Component)
9    klass.class_eval "property_accessor :test_pro => 1"
10   
11    assert_equal 1, klass.instance_variable_get(:@proparty)[:test_pro]
12    assert_respond_to klass, :test_pro
13    assert_equal 1, klass.test_pro
14    klass.test_pro 2
15    assert_equal 2, klass.test_pro
16   
17    instance = klass.new
18    assert_equal 2, instance.instance_variable_get(:@proparty)[:test_pro]
19    assert_respond_to instance, :test_pro
20    assert_respond_to instance, :test_pro=
21    assert_equal 2, instance.test_pro
22    instance.test_pro = 3
23    assert_equal 3, instance.test_pro
24  end
25 
26  def test_component_property_accessor_reader_defined
27    klass = Class.new(Ruwin::Component)
28    klass.class_eval "def test_pro;:defined;end; property_accessor :test_pro => 1"
29   
30    instance = klass.new
31    assert_respond_to instance, :test_pro
32    assert_respond_to instance, :test_pro=
33    assert_equal :defined, instance.test_pro
34  end
35 
36  def test_component_property_accessor_writer_defined
37    klass = Class.new(Ruwin::Component)
38    klass.class_eval "def test_pro= v;:defined;end; property_accessor :test_pro => 1"
39   
40    instance = klass.new
41    assert_respond_to instance, :test_pro
42    assert_respond_to instance, :test_pro=
43    assert_equal :defined, instance.__send__(:test_pro=, nil)
44  end
45 
46  def test_copy_property
47    klass = Class.new(Ruwin::Component)
48    klass.instance_variable_get(:@proparty).replace :caption => "caption", :style => 0
49   
50    copied_proparty = klass.copy_proparty
51    assert_not_same klass.instance_variable_get(:@proparty), copied_proparty
52    assert_equal    klass.instance_variable_get(:@proparty), copied_proparty
53   
54    assert_not_same klass.instance_variable_get(:@proparty)[:caption], copied_proparty[:caption]
55    assert_equal    klass.instance_variable_get(:@proparty)[:caption], copied_proparty[:caption]
56   
57    assert_same     klass.instance_variable_get(:@proparty)[:style], copied_proparty[:style]
58    assert_equal    klass.instance_variable_get(:@proparty)[:style], copied_proparty[:style]
59  end
60 
61  def test_component_should_have_proparty
62    assert Ruwin::Component.instance_variable_get(:@proparty)
63  end
64 
65  def test_component_subclass_should_have_proparty
66    klass = Class.new(Ruwin::Component)
67    assert klass.instance_variable_get(:@proparty)
68  end
69 
70  def test_component_subclass_proparty_should_have_unduplicated_immutable_value
71    symbol = :test
72    baseklass = Class.new(Ruwin::Component)
73    baseklass.instance_variable_get(:@proparty)[:test] = symbol
74    klass = Class.new(baseklass)
75    assert_equal symbol, klass.instance_variable_get(:@proparty)[:test]
76    assert_same  symbol, klass.instance_variable_get(:@proparty)[:test]
77  end
78 
79  def test_component_subclass_proparty_should_have_duplicated_mutable_value
80    string = "test"
81    baseklass = Class.new(Ruwin::Component)
82    baseklass.instance_variable_get(:@proparty)[:test] = string
83    klass = Class.new(baseklass)
84    assert_equal    string, klass.instance_variable_get(:@proparty)[:test]
85    assert_not_same string, klass.instance_variable_get(:@proparty)[:test]
86  end
87 
88 
89  %w{style exstyle caption}.each do |name|
90    class_eval <<-"END"
91      def test_proparty_#{name}_setter
92        klass = Class.new(Ruwin::Component)
93        klass.class_eval do
94          #{name} 1
95        end
96        assert_equal 1, klass.instance_variable_get(:@proparty)[:#{name}]
97      end
98     
99      def test_proparty_#{name}_getter
100        klass = Class.new(Ruwin::Component)
101        klass.class_eval do
102          #{name} 1
103        end
104        assert_equal 1, klass.#{name}
105      end
106    END
107  end
108 
109 
110  def test_new
111    klass = Class.new(Ruwin::Component)
112    instance = klass.new
113    assert_equal WS_CLIPSIBLINGS,     instance.style&WS_CLIPSIBLINGS
114    assert_equal WS_OVERLAPPEDWINDOW, instance.style&WS_OVERLAPPEDWINDOW
115  end
116 
117  def test_new_with_block
118    klass = Class.new(Ruwin::Component)
119    instance = klass.new do
120      self.caption = "block"
121    end
122    assert_equal "block", instance.caption
123  end
124 
125  def test_new_defined_const
126    klass = Class.new(Ruwin::Component)
127    klass.class_eval <<-'END'
128      CLASS_NAME = "BUTTON"
129      STYLE   = 1
130      EXSTYLE = 2
131    END
132    instance = klass.new
133    assert_equal "BUTTON", instance.classname
134    assert_equal 1, instance.style&1
135    assert_equal 2, instance.exstyle&2
136  end
137 
138  def test_factoy_defined_proparties
139    klass = Class.new(Ruwin::Component)
140    klass.class_eval <<-'END'
141      STYLE   = 1
142      EXSTYLE = 2
143      caption   "caption"
144      style     4
145      exstyle   8
146    END
147    instance = klass.new
148    assert_equal "caption", instance.caption
149    assert_equal 4, instance.style&4
150    assert_equal 8, instance.exstyle&8
151  end
152 
153 
154 
155  def test_send_message_with_error_code
156    instance = Ruwin::Component.new
157    def instance.sendMessage *args
158      :returned
159    end
160    assert_equal :returned, instance.__send__(:send_message_with_error_code, 0, 1, 2, :error_code)
161  end
162 
163  def test_send_message_with_error_code_returned_error_code
164    instance = Ruwin::Component.new
165    def instance.sendMessage *args
166      :error_code
167    end
168    assert_equal :alternate, instance.__send__(:send_message_with_error_code, 0, 1, 2, :error_code){ :alternate }
169  end
170 
171  def test_draw_bundling
172    instance = Ruwin::Component.new
173    instance.instance_variable_set :@messages, []
174    class << instance
175      attr_reader :messages
176      def sendMessage *args
177        @messages << args
178      end
179    end
180    instance.__send__ :draw_bundling do
181      instance.sendMessage :inter
182    end
183    assert_equal [[WM_SETREDRAW, 0, 0], [:inter], [WM_SETREDRAW, 1, 0]], instance.messages
184  end
185end
Note: See TracBrowser for help on using the browser.