root/lang/ruby/starframe/test/starframe/test_renderable.rb @ 28021

Revision 28021, 6.2 kB (checked in by isaisstillalive, 5 years ago)
  • Eventableモジュールを完全に削除
Line 
1require File.expand_path(File.join(File.dirname(__FILE__), "..", "helper"))
2require "starframe/renderable"
3
4class TestStarFrameRenderable < Test::Unit::TestCase
5  class ScreenObject
6    attr_reader :methods
7    def initialize
8      @methods = []
9    end
10    def method_missing name, *args
11      @methods << [name, args]
12    end
13  end
14 
15  class UnOverwriteRenderableObject
16    include StarFrame::Renderable
17  end
18 
19  def test_screen
20    object = UnOverwriteRenderableObject.new
21    screen = ScreenObject.new
22    object.screen = screen
23    assert_equal screen, object.screen
24  end
25 
26  def test_render_texture_forwarding_screen
27    object = UnOverwriteRenderableObject.new
28    screen = ScreenObject.new
29    object.screen = screen
30   
31    object.instance_eval do
32      render_texture :texture, :x, :y, :options
33    end
34    assert_equal [[:render_texture, [:texture, :x, :y, :options]]], screen.methods
35  end
36end
37
38class TestStarFrameRenderable < Test::Unit::TestCase
39  def test_render_without_block_raised_arg_error
40    assert_raise ArgumentError do
41      klass = Class.new do
42        include StarFrame::Renderable
43        render
44      end
45    end
46  end
47  def test_render_with_method_name_raised_arg_error
48    assert_raise ArgumentError do
49      klass = Class.new do
50        include StarFrame::Renderable
51        render nil, :method_name
52      end
53    end
54  end
55 
56  def test_render_hook_without_method_name_and_block_raised_arg_error
57    assert_raise ArgumentError do
58      klass = Class.new do
59        include StarFrame::Renderable
60        render :after
61      end
62    end
63  end
64  def test_render_hook_with_method_name_and_block_raised_arg_error
65    assert_raise ArgumentError do
66      klass = Class.new do
67        include StarFrame::Renderable
68        render :after, :name do
69        end
70      end
71    end
72  end
73  def test_render_hook_with_unknown_type_raised_type_error
74    assert_raise TypeError do
75      klass = Class.new do
76        include StarFrame::Renderable
77        render :no_type do
78        end
79      end
80    end
81  end
82 
83 
84  class UnOverwriteRenderableObject
85    include StarFrame::Renderable
86  end
87 
88  def test_renderable_object_should_respond_to_render
89    assert_respond_to UnOverwriteRenderableObject.new, :render
90  end
91 
92  def test_renderable_object_should_have_renderable_methods
93    actual = RenderableObject.instance_variable_get(:@_renderable_methods)
94    expected = {:before => [], :after => []}
95    assert_equal expected, actual
96  end
97 
98  def test_renderable_object_render_should_return_nil
99    assert_equal nil, UnOverwriteRenderableObject.new.render
100  end
101 
102 
103  class RenderableObject < UnOverwriteRenderableObject
104    render do
105      :rendered
106    end
107  end
108 
109  def test_rendered_event_should_return_block
110    assert_equal :rendered, RenderableObject.new.rendered
111  end
112  def test_render_should_send_rendered_event
113    assert_equal :rendered, RenderableObject.new.render
114  end
115 
116 
117 
118  class HookedRenderableObject
119    include StarFrame::Renderable
120   
121    attr_reader :called
122    def initialize
123      @called = []
124    end
125   
126    render do
127      @called << :main
128    end
129   
130    render :before do
131      @called << :before
132      :before
133    end
134    render :after do
135      @called << :after
136      :after
137    end
138   
139    render :before, :before_with_name
140    def before_with_name
141       @called << :before2
142      :before2
143    end
144    render :after, :after_with_name
145    def after_with_name
146      @called << :after2
147      :after2
148    end
149  end
150  class HookedRenderableObjectSub < HookedRenderableObject
151    render :before do
152      @called << :before3
153      :before3
154    end
155    render :after do
156      @called << :after3
157      :after3
158    end
159  end
160 
161  def test_rendered_hook_should_register_renderable_methods
162    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
163    assert_equal 2, actual[:before].size
164    assert_equal 2, actual[:after].size
165  end
166  def test_rendered_hook_should_define_methods
167    hooked_object = HookedRenderableObject.new
168    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
169    assert_equal true, actual[:before].all?{ |method_name| hooked_object.respond_to? method_name }
170    assert_equal true, actual[:after].all?{ |method_name| hooked_object.respond_to? method_name }
171  end
172 
173  def test_rendered_before_hook_should_return_block
174    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
175    assert_equal :before, HookedRenderableObject.new.__send__(actual[:before][-1])
176  end
177  def test_rendered_after_hook_should_return_block
178    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
179    assert_equal :after, HookedRenderableObject.new.__send__(actual[:after][0])
180  end
181 
182  def test_rendered_before_hook_with_method_name_should_return_method
183    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
184    assert_equal :before2, HookedRenderableObject.new.__send__(actual[:before][-2])
185  end
186  def test_rendered_after_hook_with_method_name_should_return_method
187    actual = HookedRenderableObject.instance_variable_get(:@_renderable_methods)
188    assert_equal :after2, HookedRenderableObject.new.__send__(actual[:after][1])
189  end
190 
191  def test_hooked_render_should_call_method_chain
192    hooked_object = HookedRenderableObject.new
193    hooked_object.render
194    assert_equal [:before2, :before, :main, :after, :after2], hooked_object.called
195  end
196 
197 
198  def test_hooked_sub_render_should_call_method_chain
199    hooked_object = HookedRenderableObjectSub.new
200    hooked_object.render
201    assert_equal [:before3, :before2, :before, :main, :after, :after2, :after3], hooked_object.called
202  end
203 
204 
205  def test_register_render_hook
206    klass = Class.new(HookedRenderableObject) do
207      def before3
208        @called << :before3
209        :before3
210      end
211      def after3
212        @called << :after3
213        :after3
214      end
215    end
216    klass.register_render_hook :before, :before3
217    klass.register_render_hook :after, :after3
218    hooked_object = klass.new
219    hooked_object.render
220    assert_equal [:before3, :before2, :before, :main, :after, :after2, :after3], hooked_object.called
221  end
222end
Note: See TracBrowser for help on using the browser.