apotomo 1.0.5 → 1.1.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. data/.gitignore +5 -0
  2. data/CHANGES.textile +35 -7
  3. data/Gemfile +0 -2
  4. data/README.rdoc +9 -9
  5. data/apotomo.gemspec +3 -3
  6. data/lib/apotomo.rb +4 -47
  7. data/lib/apotomo/event.rb +2 -0
  8. data/lib/apotomo/event_handler.rb +0 -3
  9. data/lib/apotomo/event_methods.rb +6 -2
  10. data/lib/apotomo/invoke_event_handler.rb +5 -3
  11. data/lib/apotomo/javascript_generator.rb +19 -16
  12. data/lib/apotomo/rails/controller_methods.rb +84 -131
  13. data/lib/apotomo/rails/view_helper.rb +15 -31
  14. data/lib/apotomo/railtie.rb +24 -0
  15. data/lib/apotomo/request_processor.rb +17 -48
  16. data/lib/apotomo/test_case.rb +5 -5
  17. data/lib/apotomo/tree_node.rb +52 -61
  18. data/lib/apotomo/version.rb +1 -1
  19. data/lib/apotomo/widget.rb +70 -146
  20. data/lib/apotomo/widget/javascript_methods.rb +39 -0
  21. data/lib/apotomo/widget_shortcuts.rb +14 -40
  22. data/lib/generators/apotomo/widget_generator.rb +8 -9
  23. data/lib/generators/erb/widget_generator.rb +17 -0
  24. data/lib/generators/haml/widget_generator.rb +20 -0
  25. data/lib/generators/{apotomo/templates → templates}/view.erb +1 -1
  26. data/lib/generators/templates/view.haml +4 -0
  27. data/lib/generators/{apotomo/templates → templates}/widget.rb +1 -1
  28. data/lib/generators/{apotomo/templates → templates}/widget_test.rb +1 -1
  29. data/lib/generators/test_unit/widget_generator.rb +14 -0
  30. data/test/rails/caching_test.rb +10 -17
  31. data/test/rails/controller_methods_test.rb +9 -81
  32. data/test/rails/rails_integration_test.rb +76 -60
  33. data/test/rails/view_helper_test.rb +17 -28
  34. data/test/rails/widget_generator_test.rb +19 -31
  35. data/test/support/test_case_methods.rb +6 -20
  36. data/test/test_helper.rb +15 -25
  37. data/test/unit/event_handler_test.rb +1 -0
  38. data/test/unit/event_methods_test.rb +20 -8
  39. data/test/unit/event_test.rb +5 -0
  40. data/test/unit/javascript_generator_test.rb +19 -19
  41. data/test/unit/render_test.rb +17 -112
  42. data/test/unit/request_processor_test.rb +73 -111
  43. data/test/unit/test_case_test.rb +13 -7
  44. data/test/unit/widget_shortcuts_test.rb +24 -53
  45. data/test/unit/widget_test.rb +76 -36
  46. data/test/widgets/mouse/eat.erb +1 -0
  47. data/test/{fixtures → widgets}/mouse/eating.html.erb +0 -0
  48. data/test/{fixtures → widgets}/mouse/educate.html.erb +0 -0
  49. data/test/{fixtures → widgets}/mouse/feed.html.erb +0 -0
  50. data/test/{fixtures → widgets}/mouse/make_me_squeak.html.erb +0 -0
  51. data/test/{fixtures → widgets}/mouse/posing.html.erb +0 -0
  52. data/test/widgets/mouse/snuggle.html.erb +1 -0
  53. metadata +32 -50
  54. data/lib/apotomo/container_widget.rb +0 -10
  55. data/lib/apotomo/persistence.rb +0 -112
  56. data/lib/apotomo/rails/view_methods.rb +0 -7
  57. data/lib/apotomo/stateful_widget.rb +0 -29
  58. data/lib/apotomo/transition.rb +0 -46
  59. data/lib/generators/apotomo/templates/view.haml +0 -4
  60. data/test/dummy/log/production.log +0 -0
  61. data/test/dummy/log/server.log +0 -0
  62. data/test/dummy/public/javascripts/application.js +0 -2
  63. data/test/dummy/public/javascripts/controls.js +0 -965
  64. data/test/dummy/public/javascripts/dragdrop.js +0 -974
  65. data/test/dummy/public/javascripts/effects.js +0 -1123
  66. data/test/dummy/public/javascripts/prototype.js +0 -6001
  67. data/test/dummy/public/javascripts/rails.js +0 -175
  68. data/test/dummy/script/rails +0 -6
  69. data/test/dummy/tmp/app/cells/mouse_widget.rb +0 -11
  70. data/test/dummy/tmp/app/cells/mouse_widget/snuggle.html.erb +0 -7
  71. data/test/dummy/tmp/app/cells/mouse_widget/squeak.html.erb +0 -7
  72. data/test/dummy/tmp/test/widgets/mouse_widget_test.rb +0 -12
  73. data/test/fixtures/application_widget_tree.rb +0 -2
  74. data/test/fixtures/mouse/snuggle.html.erb +0 -1
  75. data/test/rails/view_methods_test.rb +0 -38
  76. data/test/unit/container_test.rb +0 -21
  77. data/test/unit/invoke_test.rb +0 -126
  78. data/test/unit/persistence_test.rb +0 -201
  79. data/test/unit/stateful_widget_test.rb +0 -58
  80. data/test/unit/test_addressing.rb +0 -110
  81. data/test/unit/test_jump_to_state.rb +0 -89
  82. data/test/unit/test_tab_panel.rb +0 -71
  83. data/test/unit/transition_test.rb +0 -34
@@ -4,42 +4,40 @@ class RequestProcessorTest < ActiveSupport::TestCase
4
4
  include Apotomo::TestCaseMethods::TestController
5
5
 
6
6
  def root_mum_and_kid!
7
+ mum_and_kid!
7
8
 
8
- mum_and_kid!
9
-
10
- @root = Apotomo::Widget.new(parent_controller, 'root', :display)
11
- @root << @mum
12
-
13
-
14
- @session = {}
15
- #freeze!
16
- end
17
-
18
- # Call SW.freeze_for on @session, "freezing" stateful widgets below @root there.
19
- def freeze!
20
- Apotomo::StatefulWidget.freeze_for(@session, @root)
9
+ @root = Apotomo::Widget.new(parent_controller, 'root', :display)
10
+ @root << @mum
21
11
  end
22
12
 
23
13
 
24
- context "#root" do
25
- should "allow external modification of the tree" do
26
- @processor = Apotomo::RequestProcessor.new(parent_controller, {})
14
+ context "RequestProcessor" do
15
+ setup do
16
+ @processor = Apotomo::RequestProcessor.new(parent_controller)
27
17
  root = @processor.root
28
18
  root << mouse_mock
19
+ end
20
+
21
+ should "allow external modification of the tree" do
22
+ root = @processor.root
29
23
  assert_equal 2, @processor.root.size
30
24
  end
25
+
26
+ should "delegate #render_widget_for to #root" do
27
+ assert_equal 'squeak!', @processor.render_widget_for('mouse', :squeak)
28
+ end
31
29
  end
32
30
 
33
31
  context "#attach_stateless_blocks_for" do
34
32
  setup do
35
- @processor = Apotomo::RequestProcessor.new(parent_controller, {})
33
+ @processor = Apotomo::RequestProcessor.new(parent_controller)
36
34
  @root = @processor.root
37
35
  assert_equal @root.size, 1
38
36
  end
39
37
 
40
38
  should "allow has_widgets blocks with root parameter" do
41
39
  @processor.send(:attach_stateless_blocks_for, [Proc.new{ |root|
42
- root.add widget('mouse_cell', 'mouse')
40
+ root << widget(:mouse, 'mouse')
43
41
  }], @root, parent_controller)
44
42
 
45
43
  assert_equal 'mouse', @processor.root['mouse'].name
@@ -47,13 +45,9 @@ class RequestProcessorTest < ActiveSupport::TestCase
47
45
  end
48
46
 
49
47
  context "option processing at construction time" do
50
- context "with empty session and options" do
48
+ context "with empty options" do
51
49
  setup do
52
- @processor = Apotomo::RequestProcessor.new(parent_controller, {})
53
- end
54
-
55
- should "mark the tree as flushed" do
56
- assert @processor.widgets_flushed?
50
+ @processor = Apotomo::RequestProcessor.new(parent_controller)
57
51
  end
58
52
 
59
53
  should "provide a single root-node for #root" do
@@ -63,60 +57,15 @@ class RequestProcessorTest < ActiveSupport::TestCase
63
57
 
64
58
  context "with controller" do
65
59
  should "attach the passed parent_controller to root" do
66
- assert_equal parent_controller, Apotomo::RequestProcessor.new(parent_controller, {}, {}, []).root.parent_controller
60
+ assert_equal parent_controller, Apotomo::RequestProcessor.new(parent_controller, {}, []).root.parent_controller
67
61
  end
68
62
  end
69
-
70
- context "with session" do
71
- setup do
72
- root_mum_and_kid!
73
- @mum.version = 1
74
- freeze!
75
-
76
- @processor = Apotomo::RequestProcessor.new(parent_controller, @session)
77
- end
78
-
79
- should "provide a widget family for #root" do
80
- assert_equal 3, @processor.root.size
81
- assert_equal 1, @processor.root['mum'].version
82
- assert_not @processor.widgets_flushed?
83
- end
84
-
85
- context "having a flush flag set" do
86
- setup do
87
- @processor = Apotomo::RequestProcessor.new(parent_controller, @session, :flush_widgets => true)
88
- end
89
-
90
- should "provide a single root for #root when :flush_widgets is set" do
91
- assert_equal 1, @processor.root.size
92
- assert @processor.widgets_flushed?
93
- end
94
-
95
- should "wipe-out our session variables" do
96
- assert_nil @session[:apotomo_stateful_branches]
97
- assert_nil @session[:apotomo_widget_ivars]
98
- end
99
-
100
- end
101
-
102
- context "and with stateless widgets" do
103
- setup do
104
- root_mum_and_kid!
105
- freeze!
106
- @processor = Apotomo::RequestProcessor.new(parent_controller, @session, {}, [Proc.new { |root| root << Apotomo::Widget.new(parent_controller, 'grandma', :eating) }])
107
- end
108
-
109
- should "first attach passed stateless, then stateful widgets to root" do
110
- assert_equal 4, @processor.root.size
111
- end
112
- end
113
- end
114
-
115
63
  end
116
64
 
65
+
117
66
  context "#process_for" do
118
67
  setup do
119
- class KidCell < Apotomo::Widget
68
+ class KidWidget < Apotomo::Widget
120
69
  responds_to_event :doorSlam, :with => :flight
121
70
  responds_to_event :doorSlam, :with => :squeak
122
71
  def flight; render :text => "away from here!"; end
@@ -124,10 +73,10 @@ class RequestProcessorTest < ActiveSupport::TestCase
124
73
  end
125
74
 
126
75
  procs = [Proc.new{ |root,controller|
127
- root << mum = MouseCell.new(parent_controller, 'mum', :squeak) << KidCell.new(parent_controller, 'kid', :squeak)
76
+ root << widget(:mouse, 'mum') << KidWidget.new(parent_controller, 'kid', :squeak)
128
77
  }]
129
78
 
130
- @processor = Apotomo::RequestProcessor.new(parent_controller, {}, {:js_framework => :prototype}, procs)
79
+ @processor = Apotomo::RequestProcessor.new(parent_controller, {:js_framework => :prototype}, procs)
131
80
  end
132
81
 
133
82
  should "return an empty array if nothing was triggered" do
@@ -138,6 +87,14 @@ class RequestProcessorTest < ActiveSupport::TestCase
138
87
  assert_equal ["away from here!", "squeak!"], @processor.process_for({:type => :doorSlam, :source => 'kid'})
139
88
  end
140
89
 
90
+ should "append the params hash to the triggered event" do
91
+ KidWidget.class_eval do
92
+ def squeak(evt); render :text => evt.data.inspect; end
93
+ end
94
+
95
+ assert_equal ["away from here!", "{:type=>:doorSlam, :source=>\"kid\"}"], @processor.process_for({:type => :doorSlam, :source => 'kid'})
96
+ end
97
+
141
98
  should "raise an exception when :source is unknown" do
142
99
  assert_raises RuntimeError do
143
100
  @processor.process_for({:type => :squeak, :source => 'tom'})
@@ -146,47 +103,11 @@ class RequestProcessorTest < ActiveSupport::TestCase
146
103
  end
147
104
 
148
105
 
149
-
150
- context "#freeze!" do
151
- should "serialize stateful branches to @session" do
152
- @processor = Apotomo::RequestProcessor.new(parent_controller, {})
153
- @processor.root << mum_and_kid!
154
- assert_equal 3, @processor.root.size
155
- @processor.freeze!
156
-
157
- @processor = Apotomo::RequestProcessor.new(parent_controller, @processor.session)
158
- assert_equal 3, @processor.root.size
159
- end
160
- end
161
-
162
- context "#render_widget_for" do
163
- setup do
164
- class MouseCell < Apotomo::Widget
165
- def squeak; render :text => "squeak!"; end
166
- end
167
-
168
- @processor = Apotomo::RequestProcessor.new(parent_controller, {}, {},
169
- [Proc.new { |root| root << MouseCell.new(parent_controller, 'mum', :squeak) }])
170
- end
171
-
172
- should "render the widget when passing an existing widget id" do
173
- assert_equal 'squeak!', @processor.render_widget_for('mum', {})
174
- end
175
-
176
- should "render the widget when passing an existing widget instance" do
177
- assert_equal 'squeak!', @processor.render_widget_for(@processor.root['mum'], {})
178
- end
179
106
 
180
- should "raise an exception when a non-existent widget id id passed" do
181
- assert_raises RuntimeError do
182
- @processor.render_widget_for('mummy', {})
183
- end
184
- end
185
- end
186
107
 
187
108
  context "invoking #address_for" do
188
109
  setup do
189
- @processor = Apotomo::RequestProcessor.new(parent_controller, {})
110
+ @processor = Apotomo::RequestProcessor.new(parent_controller)
190
111
  end
191
112
 
192
113
  should "accept an event :type" do
@@ -210,3 +131,44 @@ class RequestProcessorTest < ActiveSupport::TestCase
210
131
  end
211
132
  end
212
133
  end
134
+
135
+ class RequestProcessorHooksTest < ActiveSupport::TestCase
136
+ include Apotomo::TestCaseMethods::TestController
137
+ include Apotomo::TestCaseMethods
138
+
139
+ context "Hooks in RequestProcessor" do
140
+ setup do
141
+ @class = Class.new(Apotomo::RequestProcessor)
142
+ end
143
+
144
+ context ":after_initialize hook" do
145
+ should "be called after the has_widgets blocks invokation" do
146
+ @k = mouse_mock("kid")
147
+ @class.after_initialize do |r|
148
+ r.root["mum"] << @k
149
+ end
150
+
151
+ @r = @class.new(parent_controller, {},
152
+ [Proc.new { |root| root << widget(:mouse, 'mum') }])
153
+
154
+ assert_equal @r.root["mum"]["kid"], @k
155
+ end
156
+ end
157
+
158
+ context ":after_fire hook" do
159
+ should "be called in #process_for after fire" do
160
+ @k = mouse_mock("kid")
161
+ @class.after_fire do |r|
162
+ r.root["mum"] << @k = mouse_mock("kid")
163
+ end
164
+
165
+ # DISCUSS: maybe add a trigger test here?
166
+ @r = @class.new(parent_controller, {},
167
+ [Proc.new { |root| root << widget(:mouse, 'mum') }])
168
+ @r.process_for(:source => "root", :type => :noop) # calls ~after_fire.
169
+
170
+ assert_equal @k, @r.root["mum"]["kid"]
171
+ end
172
+ end
173
+ end
174
+ end
@@ -18,7 +18,7 @@ class TestCaseTest < Test::Unit::TestCase
18
18
  setup do
19
19
  @klass = MouseWidgetTest
20
20
  @test = @klass.new(:widget).tap{ |t| t.setup }
21
- @klass.has_widgets { |r| r << widget("mouse_cell", 'mum', :eating) }
21
+ @klass.has_widgets { |r| r << widget(:mouse, 'mum', :eating) }
22
22
  end
23
23
 
24
24
  should "respond to #root" do
@@ -40,12 +40,12 @@ class TestCaseTest < Test::Unit::TestCase
40
40
  end
41
41
 
42
42
  should "respond to #render_widget" do
43
- assert_equal "<div id=\"mum\">burp!</div>", @test.render_widget('mum')
44
- assert_equal "<div id=\"mum\">burp!</div>", @test.last_invoke
43
+ assert_equal "<div id=\"mum\">burp!</div>\n", @test.render_widget('mum', :eat)
44
+ assert_equal "<div id=\"mum\">burp!</div>\n", @test.last_invoke
45
45
  end
46
46
 
47
47
  should "respond to #assert_select" do
48
- @test.render_widget('mum')
48
+ @test.render_widget('mum', :eat)
49
49
 
50
50
  assert_nothing_raised { @test.assert_select("div#mum", "burp!") }
51
51
 
@@ -68,9 +68,15 @@ class TestCaseTest < Test::Unit::TestCase
68
68
 
69
69
  should "provide options from #trigger to the widget" do
70
70
  @test.trigger(:footsteps, :source => 'mum', :direction => :kitchen)
71
- assert_equal :kitchen, @mum.param(:direction)
71
+ assert_equal :kitchen, @mum.options[:direction]
72
72
  end
73
73
 
74
+ #should "merge options from #trigger and constructor" do
75
+ # @test.root << @test.widget("mouse_cell", 'kid', :location => :hallway)
76
+ # @test.trigger(:footsteps, :source => 'kid', :direction => :kitchen)
77
+ # assert_equal({:direction => :kitchen, :location => :hallway}, @mum.param(:direction))
78
+ #end
79
+
74
80
  should "respond to #assert_response" do
75
81
  @test.trigger(:footsteps, :source => 'mum')
76
82
  assert @test.assert_response("squeak!")
@@ -87,8 +93,8 @@ class TestCaseTest < Test::Unit::TestCase
87
93
  assert_kind_of ActionController::Base, @test.parent_controller
88
94
  end
89
95
 
90
- should "respond to #controller_name" do
91
- assert_equal "barn", @test.parent_controller.controller_name
96
+ should "respond to #controller_path" do
97
+ assert_equal "barn", @test.parent_controller.controller_path
92
98
  end
93
99
  end
94
100
  end
@@ -1,105 +1,76 @@
1
1
  require 'test_helper'
2
2
 
3
- class MumWidget < MouseCell; end
4
- class MouseTabs;end
3
+ class MumWidget < MouseWidget; end
4
+ class MouseTabsWidget;end
5
5
 
6
6
  class WidgetShortcutsTest < Test::Unit::TestCase
7
7
  include Apotomo::TestCaseMethods::TestController
8
8
 
9
9
  context "#constant_for" do
10
10
  should "constantize symbols" do
11
- assert_equal MumWidget, constant_for(:mum_widget)
11
+ assert_equal MumWidget, constant_for(:mum)
12
12
  end
13
13
 
14
14
  should "not try to singularize the widget class" do
15
- assert_equal MouseTabs, constant_for(:mouse_tabs)
16
- end
17
- end
18
-
19
- context "#cell" do
20
- should "create a MouseCell instance for backward-compatibility" do
21
- assert_kind_of MouseCell, cell(:mouse, :eating, 'mum')
15
+ assert_equal MouseTabsWidget, constant_for(:mouse_tabs)
22
16
  end
23
17
  end
24
18
 
25
19
  context "#widget" do
26
20
  context "with all arguments" do
27
21
  setup do
28
- @mum = widget(:mum_widget, 'mum', :eating, :color => 'grey', :type => :hungry)
22
+ @mum = widget(:mum, 'mum', :eating, :color => 'grey', :type => :hungry)
29
23
  end
30
24
 
31
25
  should "create a MumWidget instance" do
32
26
  assert_kind_of MumWidget, @mum
33
- assert_equal :eating, @mum.instance_variable_get(:@start_state)
34
27
  assert_equal 'mum', @mum.name
35
28
  end
36
29
 
37
30
  should "accept options" do
38
- assert_equal({:color => "grey", :type => :hungry}, @mum.opts)
31
+ assert_equal({:color => "grey", :type => :hungry}, @mum.options)
39
32
  end
40
33
  end
41
34
 
42
- context "with 3 arguments and no start_state" do
43
- should "set a default start_state" do
44
- @mum = widget(:mum_widget, 'mum', :color => 'grey', :type => :hungry)
35
+ context "with 3 arguments and no options" do
36
+ should "not set options" do
37
+ @mum = widget(:mum, 'mum', :squeak)
45
38
  assert_kind_of MumWidget, @mum
46
- assert_equal :display, @mum.instance_variable_get(:@start_state)
47
39
  assert_equal 'mum', @mum.name
48
- assert_equal({:color => "grey", :type => :hungry}, @mum.opts)
40
+ assert_equal({}, @mum.options)
49
41
  end
50
42
  end
51
43
 
52
- context "with 3 arguments and no options" do
53
- should "not set options" do
54
- @mum = widget(:mum_widget, 'mum', :squeak)
44
+ context "with class and id" do
45
+ setup do
46
+ @mum = widget(:mum, 'mummy')
47
+ end
48
+
49
+ should "create a MumWidget instance" do
55
50
  assert_kind_of MumWidget, @mum
56
- assert_equal :squeak, @mum.instance_variable_get(:@start_state)
57
- assert_equal 'mum', @mum.name
58
- assert_equal({}, @mum.opts)
51
+ assert_equal 'mummy', @mum.name
59
52
  end
60
53
  end
61
54
 
62
- context "with id only" do
55
+ context "with class, only" do
63
56
  setup do
64
- @mum = widget(:mum_widget, 'mum')
57
+ @mum = widget(:mum)
65
58
  end
66
59
 
67
- should "create a MumWidget instance with :display start state" do
60
+ should "create a MumWidget instance named :mum" do
68
61
  assert_kind_of MumWidget, @mum
69
- assert_equal :display, @mum.instance_variable_get(:@start_state)
70
- assert_equal 'mum', @mum.name
62
+ assert_equal :mum, @mum.name
71
63
  end
72
64
  end
73
65
 
66
+
74
67
  should "yield itself" do
75
- @mum = widget(:mum_widget, :snuggle, 'mum') do |mum|
68
+ @mum = widget(:mum, 'mum') do |mum|
76
69
  assert_kind_of MumWidget, mum
77
- mum << widget(:mum_widget, 'kid', :sleep)
70
+ mum << widget(:mum, 'kid')
78
71
  end
79
72
  assert_equal 2, @mum.size
80
73
  assert_kind_of MumWidget, @mum['kid']
81
74
  end
82
75
  end
83
-
84
- context "#container" do
85
- setup do
86
- @family = container('family')
87
- end
88
-
89
- should "create a ContainerWidget instance" do
90
- assert_kind_of ::Apotomo::ContainerWidget, @family
91
- assert_equal 'family', @family.name
92
- end
93
-
94
- should "yield itself" do
95
- @container = container(:family) do |family|
96
- family << widget(:mum_widget, 'mum')
97
- end
98
- assert_equal 2, @container.size
99
- end
100
-
101
- should "be aliased to #section for backward-compatibility" do
102
- assert_kind_of ::Apotomo::ContainerWidget, section('family')
103
- end
104
- end
105
76
  end
@@ -9,34 +9,34 @@ class WidgetTest < ActiveSupport::TestCase
9
9
  @mum = Apotomo::Widget.new(@controller, 'mum', :squeak)
10
10
  end
11
11
  end
12
-
12
+
13
13
  context "Widget.has_widgets" do
14
14
  setup do
15
- @mum = Class.new(MouseCell) do
15
+ @mum = Class.new(MouseWidget) do
16
16
  has_widgets do |me|
17
- me << widget('mouse_cell', 'baby', :squeak)
17
+ me << widget(:mouse, 'baby')
18
18
  end
19
- end.new(@controller, 'mum', :squeak)
19
+ end.new(@controller, 'mum')
20
20
 
21
21
  @kid = Class.new(@mum.class).new(@controller, 'mum', :squeak)
22
22
  end
23
23
 
24
24
  should "setup the widget family at creation time" do
25
25
  assert_equal 1, @mum.children.size
26
- assert_kind_of Apotomo::StatefulWidget, @mum['baby']
26
+ assert_kind_of Apotomo::Widget, @mum['baby']
27
27
  end
28
28
 
29
29
  should "inherit trees for now" do
30
30
  assert_equal 1, @mum.children.size
31
- assert_kind_of Apotomo::StatefulWidget, @mum['baby']
31
+ assert_kind_of Apotomo::Widget, @mum['baby']
32
32
  end
33
33
  end
34
34
 
35
35
  context "Widget.after_add" do
36
36
  setup do
37
- @mum = Class.new(MouseCell) do
37
+ @mum = Class.new(MouseWidget) do
38
38
  after_add do |me, parent|
39
- parent << widget('mouse_cell', 'kid', :squeak)
39
+ parent << widget(:mouse, 'kid', :squeak)
40
40
  end
41
41
  end.new(@controller, 'mum', :squeak)
42
42
 
@@ -57,7 +57,7 @@ class WidgetTest < ActiveSupport::TestCase
57
57
  assert_equal ['berry', 'kid'], @root.children.collect { |w| w.name }
58
58
  end
59
59
  end
60
-
60
+
61
61
  context "A stateless widget" do
62
62
  setup do
63
63
  @mum = Apotomo::Widget.new(@controller, 'mum', :squeak)
@@ -91,23 +91,6 @@ class WidgetTest < ActiveSupport::TestCase
91
91
  @mum.visible = false
92
92
  assert_not @mum.visible?
93
93
  end
94
-
95
- context "in a widget family" do
96
- setup do
97
- @mum << @jerry = mouse_mock('jerry')
98
- @mum << @berry = mouse_mock('berry')
99
- end
100
-
101
- should "per default return all #visible_children" do
102
- assert_equal [@jerry, @berry], @mum.visible_children
103
- assert_equal [], @jerry.visible_children
104
- end
105
-
106
- should "hide berry in #visible_children if he's invisible" do
107
- @berry.visible = false
108
- assert_equal [@jerry], @mum.visible_children
109
- end
110
- end
111
94
  end
112
95
 
113
96
  should "respond to #find_widget" do
@@ -128,16 +111,23 @@ class WidgetTest < ActiveSupport::TestCase
128
111
  assert_equal @mum.name, @mum.widget_id
129
112
  end
130
113
 
131
- should "provide #param" do
132
- @controller.params = HashWithIndifferentAccess.new('type' => 'Wireless mouse', :brand => "Logitech")
133
- @mum = widget(:mouse_cell, 'mum', :display, :color => 'grey', :type => 'shrew')
134
- assert_equal nil, @mum.param(:whatever)
135
- assert_equal 'grey', @mum.param(:color)
136
- assert_equal 'grey', @mum.param('color')
137
- assert_equal 'shrew', @mum.param(:type)
138
- assert_equal 'Logitech', @mum.param(:brand)
114
+ should "mark #param as deprecated" do
115
+ assert_raises RuntimeError do
116
+ @mum.param(:volume)
117
+ end
139
118
  end
140
119
 
120
+ should "respond to DEFAULT_VIEW_PATHS" do
121
+ assert_equal ["app/widgets", "app/widgets/layouts"], Apotomo::Widget::DEFAULT_VIEW_PATHS
122
+ end
123
+
124
+ should "respond to .view_paths" do
125
+ assert_equal ActionView::PathSet.new(Apotomo::Widget::DEFAULT_VIEW_PATHS + ["test/widgets"]), Apotomo::Widget.view_paths
126
+ end
127
+
128
+ should "respond to .controller_path" do
129
+ assert_equal "mouse", MouseWidget.controller_path
130
+ end
141
131
 
142
132
  # internal_methods:
143
133
  should "not list internal methods in action_methods" do
@@ -145,8 +135,8 @@ class WidgetTest < ActiveSupport::TestCase
145
135
  end
146
136
 
147
137
  should "list both local and inherited states in Widget.action_methods" do
148
- assert MouseCell.action_methods.collect{ |m| m.to_s }.include?("eating")
149
- assert Class.new(MouseCell).action_methods.collect{ |m| m.to_s }.include?("eating")
138
+ assert MouseWidget.action_methods.collect{ |m| m.to_s }.include?("squeak")
139
+ assert Class.new(MouseWidget).action_methods.collect{ |m| m.to_s }.include?("squeak")
150
140
  end
151
141
 
152
142
  should "not list #display in internal_methods although it's defined in Object" do
@@ -154,3 +144,53 @@ class WidgetTest < ActiveSupport::TestCase
154
144
  end
155
145
  end
156
146
  end
147
+
148
+
149
+ class RenderWidgetTest < ActiveSupport::TestCase
150
+ include Apotomo::TestCaseMethods::TestController
151
+
152
+ context "#render_widget" do
153
+ should "allow passing widget id" do
154
+ assert_equal "squeak!", mouse_mock.render_widget('mouse', :squeak)
155
+ end
156
+
157
+ should "allow passing widget instance" do
158
+ assert_equal 'squeak!', mouse_mock.render_widget(mouse_mock('mum'), :squeak)
159
+ end
160
+
161
+ should "use :display as standard state" do
162
+ mum = mouse_mock('Mum') do
163
+ def display
164
+ render :text => "#{widget_id}, that's me!"
165
+ end
166
+ end
167
+
168
+ assert_equal "Mum, that's me!", mouse_mock.render_widget(mum)
169
+ end
170
+
171
+ should "raise an exception when a non-existent widget id is passed" do
172
+ e = assert_raises RuntimeError do
173
+ mouse_mock.render_widget('mummy')
174
+ end
175
+
176
+ assert_equal "Couldn't render non-existent widget `mummy`", e.message
177
+ end
178
+
179
+ should "pass options as state-args" do
180
+ mum = mouse_mock do
181
+ def display(color="grey")
182
+ render :text => "I'm #{color}"
183
+ end
184
+ end
185
+
186
+ assert_equal("I'm grey", mouse_mock.render_widget(mum), "default value in state-arg didn't work")
187
+ assert_equal("I'm black", mouse_mock.render_widget(mum, :display, "black"))
188
+ end
189
+
190
+ should "use #find_widget from self to find the passed widget id" do
191
+ mum = mouse_mock('mum') << mouse_mock(:kid)
192
+
193
+ assert_equal "<div id=\"kid\">burp!</div>\n", mum.render_widget(:kid, :eat)
194
+ end
195
+ end
196
+ end