apotomo 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,19 +11,13 @@ class ViewHelperTest < Apotomo::TestCase
11
11
  setup_test_states_in(subject)
12
12
  subject.invoke(:in_view, block)
13
13
  end
14
- def mouse_mock(id='mum', opts={}, &block)
15
- mouse = MouseWidget.new(parent_controller, id, opts)
16
- mouse.instance_eval &block if block_given?
17
- mouse
18
- end
19
-
20
14
 
21
- # describe "A widget state view" do
22
- ### DISCUSS: what is this for?
23
- teardown do
24
- Apotomo.js_framework = :prototype
25
- end
15
+ ### DISCUSS: what is this for?
16
+ teardown do
17
+ Apotomo.js_framework = :prototype
18
+ end
26
19
 
20
+ # describe "Rails::ViewHelper" do
27
21
  ### DISCUSS: needed?
28
22
  ### FIXME: could somebody get that working?
29
23
  test "respond to #multipart_form_to_event" do
@@ -36,25 +30,27 @@ class ViewHelperTest < Apotomo::TestCase
36
30
  end
37
31
 
38
32
  test "respond to #url_for_event" do
39
- assert_equal("/barn/render_event_response?source=mum&amp;type=footsteps", in_view(MouseWidget) do
40
- url_for_event(:footsteps)
41
- end)
33
+ assert_equal "/barn/render_event_response?source=mum&amp;type=footsteps", in_view(MouseWidget) { url_for_event(:footsteps) }
42
34
  end
43
35
 
44
36
  test "respond to #url_for_event with a namespaced controller" do
45
37
  @controller = namespaced_controller
46
- assert_equal("/farm/barn/render_event_response?source=mum&amp;type=footsteps", in_view(MouseWidget) do
47
- url_for_event(:footsteps)
48
- end)
38
+ assert_equal "/farm/barn/render_event_response?source=mum&amp;type=footsteps", in_view(MouseWidget) { url_for_event(:footsteps) }
49
39
  end
50
40
 
51
41
  test "respond to #widget_tag" do
52
- assert_equal('<span id="mum">squeak!</span>', in_view(MouseWidget) do widget_tag(:span) { "squeak!" } end)
42
+ assert_equal('<span id="mum">squeak!</span>', in_view(MouseWidget) do
43
+ widget_tag(:span) do
44
+ "squeak!"
45
+ end
46
+ end)
53
47
  end
54
48
 
55
49
  test "respond to #widget_tag with options" do
56
50
  assert_equal('<span class="mouse" id="kid">squeak!</span>', in_view(MouseWidget) do
57
- widget_tag(:span, :id => 'kid', :class => "mouse") { "squeak!" }
51
+ widget_tag(:span, :id => 'kid', :class => "mouse") do
52
+ "squeak!"
53
+ end
58
54
  end)
59
55
  end
60
56
 
@@ -63,14 +59,16 @@ class ViewHelperTest < Apotomo::TestCase
63
59
  end
64
60
 
65
61
  test "respond to #widget_id" do
66
- assert_equal('mum', in_view(MouseWidget){ widget_id })
62
+ assert_equal 'mum', in_view(MouseWidget) { widget_id }
67
63
  end
68
64
 
69
65
  test "respond to #render_widget" do
70
66
  mum = mouse
71
67
  MouseWidget.new(mum, :kid)
72
68
 
73
- assert_equal("<div id=\"kid\">burp!</div>\n", in_view(mum){ render_widget 'kid', :eat })
69
+ assert_equal("<div id=\"kid\">burp!</div>\n", in_view(mum) do
70
+ render_widget('kid', :eat)
71
+ end)
74
72
  end
75
73
 
76
74
  test "respond to #children" do
@@ -78,8 +76,15 @@ class ViewHelperTest < Apotomo::TestCase
78
76
  MouseWidget.new(mum, :kid)
79
77
 
80
78
  assert_equal("<div id=\"kid\">burp!</div>\n", in_view(mum) do
81
- children.inject("") { |html, child| html += render_widget(child, :eat) }.html_safe
79
+ children.collect do |child|
80
+ render_widget(child, :eat)
81
+ end.join.html_safe
82
82
  end)
83
83
  end
84
+
85
+ # TODO: test #js_generator
86
+
87
+ # TODO: test instance variables access
88
+
84
89
  # end
85
90
  end
@@ -3,131 +3,139 @@ require 'test_helper'
3
3
  class RequestProcessorTest < MiniTest::Spec
4
4
  include Apotomo::TestCaseMethods::TestController
5
5
 
6
- def root_mum_and_kid!
7
- mum_and_kid!
8
-
9
- @root = Apotomo::Widget.new(parent_controller, 'root', :display)
10
- @root << @mum
11
- end
12
-
13
-
14
6
  describe "RequestProcessor" do
15
7
  before do
16
8
  @processor = Apotomo::RequestProcessor.new(parent_controller)
17
- root = @processor.root
18
- root << mouse_mock
19
- end
20
-
21
- it "allow external modification of the tree" do
22
- root = @processor.root
23
- assert_equal 2, @processor.root.size
24
- end
25
-
26
- it "delegate #render_widget_for to #root" do
27
- assert_equal 'squeak!', @processor.render_widget_for('mouse', :squeak)
28
- end
29
- end
30
-
31
- describe "#attach_stateless_blocks_for" do
32
- before do
33
- @processor = Apotomo::RequestProcessor.new(parent_controller)
34
- @root = @processor.root
35
- assert_equal @root.size, 1
9
+ @processor.root << mouse_mock
36
10
  end
37
11
 
38
- it "allow has_widgets blocks with root parameter" do
39
- @processor.send(:attach_stateless_blocks_for, [Proc.new{ |root|
40
- root << widget(:mouse, 'mouse')
41
- }], @root, parent_controller)
12
+ describe "constructor, #parent_controller, #root" do
13
+ it "provide #parent_controller and a single root-node for #root" do
14
+ assert_kind_of Apotomo::Widget, @processor.root
15
+ assert_equal 2, @processor.root.size # because we added a child
16
+ assert_equal :root, @processor.root.name
42
17
 
43
- assert_equal 'mouse', @processor.root['mouse'].name
44
- end
45
- end
46
-
47
- describe "option processing at construction time" do
48
- describe "with empty options" do
49
- before do
50
- @processor = Apotomo::RequestProcessor.new(parent_controller)
18
+ assert_equal parent_controller, @processor.root.parent_controller
51
19
  end
52
20
 
53
- it "provide a single root-node for #root" do
54
- assert_equal 1, @processor.root.size
55
- end
56
- end
57
-
58
- describe "with controller" do
59
- it "attach the passed parent_controller to root" do
60
- assert_equal parent_controller, Apotomo::RequestProcessor.new(parent_controller, {}, []).root.parent_controller
61
- end
62
- end
63
- end
21
+ # TODO: test options argument
64
22
 
23
+ # TODO: test has_widgets_blocks argument
65
24
 
66
- describe "#process_for" do
67
- before do
68
- class KidWidget < Apotomo::Widget
69
- responds_to_event :doorSlam, :with => :flight
70
- responds_to_event :doorSlam, :with => :squeak
71
- def flight; render :text => "away from here!"; end
72
- def squeak; render :text => "squeak!"; end
73
- end
74
-
75
- procs = [Proc.new{ |root|
76
- root << widget(:mouse, 'mum')
77
- KidWidget.new(root['mum'], 'kid')
78
- }]
79
-
80
- @processor = Apotomo::RequestProcessor.new(parent_controller, {:js_framework => :prototype}, procs)
25
+ # TODO: test if after_initialize hook has been run
81
26
  end
82
27
 
83
- it "return an empty array if nothing was triggered" do
84
- assert_equal [], @processor.process_for({:type => :mouseClick, :source => 'kid'})
28
+ it "allow external modification of the tree" do # DISCUSS: needed?
29
+ assert_equal 2, @processor.root.size
85
30
  end
86
31
 
87
- it "return 2 page updates when @kid squeaks" do
88
- assert_equal ["away from here!", "squeak!"], @processor.process_for({:type => :doorSlam, :source => 'kid'})
32
+ it "delegate #render_widget_for to #root" do
33
+ # TODO: @processor.root should expect #render_widget_for
34
+ assert_equal 'squeak!', @processor.render_widget_for('mouse', :squeak)
89
35
  end
90
36
 
91
- it "append the params hash to the triggered event" do
92
- KidWidget.class_eval do
93
- def squeak(evt); render :text => evt.data.inspect; end
94
- end
37
+ describe "#attach_stateless_blocks_for" do
38
+ it "allow has_widgets blocks with root parameter" do
39
+ @processor.send(:attach_stateless_blocks_for, [
40
+ Proc.new{ |root|
41
+ root << widget(:mouse, 'mouse_sister')
42
+ },
43
+ Proc.new{ |root|
44
+ root << widget(:mouse, 'mouse_brother')
45
+ }], @processor.root, parent_controller)
95
46
 
96
- assert_equal ["away from here!", "{:type=>:doorSlam, :source=>\"kid\"}"], @processor.process_for({:type => :doorSlam, :source => 'kid'})
97
- end
47
+ # TODO: test if blocks are yielded
48
+ # TODO: test what blocks gets
98
49
 
99
- it "raise an exception when :source is unknown" do
100
- assert_raises Apotomo::RequestProcessor::InvalidSourceWidget do
101
- @processor.process_for({:type => :squeak, :source => 'tom'})
50
+ assert_kind_of MouseWidget, @processor.root['mouse_sister']
51
+ assert_equal 'mouse_sister', @processor.root['mouse_sister'].name
52
+ assert_kind_of MouseWidget, @processor.root['mouse_brother']
53
+ assert_equal 'mouse_brother', @processor.root['mouse_brother'].name
102
54
  end
103
55
  end
104
- end
105
-
106
-
107
-
108
56
 
109
- describe "invoking #address_for" do
110
- before do
111
- @processor = Apotomo::RequestProcessor.new(parent_controller)
112
- end
57
+ describe "#process_for" do
58
+ before do
59
+ class KidWidget < Apotomo::Widget
60
+ responds_to_event :doorSlam, :with => :flight
61
+ responds_to_event :doorSlam, :with => :squeak
113
62
 
114
- it "accept an event :type" do
115
- assert_equal({:type => :squeak, :source => 'mum'}, @processor.address_for(:type => :squeak, :source => 'mum'))
116
- end
63
+ def flight
64
+ render :text => "away from here!"
65
+ end
117
66
 
118
- it "accept arbitrary options" do
119
- assert_equal({:type => :squeak, :volume => 'loud', :source => 'mum'}, @processor.address_for(:type => :squeak, :volume => 'loud', :source => 'mum'))
120
- end
67
+ def squeak
68
+ render :text => "squeak!"
69
+ end
70
+ end
71
+
72
+ procs = [Proc.new{ |root|
73
+ root << widget(:mouse, 'mum')
74
+ KidWidget.new(root['mum'], 'kid')
75
+ }]
76
+
77
+ @processor = Apotomo::RequestProcessor.new(parent_controller, {:js_framework => :prototype}, procs)
78
+ end
79
+
80
+ it "return an empty array if nothing was triggered" do
81
+ assert_equal [], @processor.process_for(:type => :mouseClick, :source => 'kid')
82
+ end
83
+
84
+ it "return ordered results if something was triggered" do
85
+ assert_equal ["away from here!", "squeak!"], @processor.process_for(:type => :doorSlam, :source => 'kid')
86
+ end
87
+
88
+ # TODO: test a situation: root.page_updates is not empty before #process_for call
89
+
90
+ # TODO: widget instance should expect #fire
91
+
92
+ # TODO: make this test without #inspect
93
+ # TODO: widget instance should expect responder method (replace this test with)
94
+ it "append the params hash to the triggered event" do
95
+ KidWidget.class_eval do
96
+ def squeak(evt)
97
+ render :text => evt.data.inspect
98
+ end
99
+ end
100
+
101
+ assert_equal ["away from here!", %Q({:type=>:doorSlam, :param=>:value, :source=>"kid"})], @processor.process_for(:type => :doorSlam, :param => :value, :source => 'kid')
102
+ end
103
+
104
+ # TODO: test if after_fire hook has been run
121
105
 
122
- it "complain if no type given" do
123
- assert_raises RuntimeError do
124
- @processor.address_for(:source => 'mum')
106
+ it "raise an exception when :source is unknown" do
107
+ e = assert_raises Apotomo::RequestProcessor::InvalidSourceWidget do
108
+ @processor.process_for(:type => :squeak, :source => 'tom')
109
+ end
110
+ assert_match "Source \"tom\" non-existent", e.message
125
111
  end
126
112
  end
127
-
128
- it "complain if no source given" do
129
- assert_raises RuntimeError do
130
- @processor.address_for(:type => :footsteps)
113
+
114
+ describe "#address_for" do
115
+ before do
116
+ @processor = Apotomo::RequestProcessor.new(parent_controller)
117
+ end
118
+
119
+ it "accept an event :type and :source" do
120
+ assert_equal({:type => :squeak, :source => 'mum'}, @processor.address_for(:type => :squeak, :source => 'mum'))
121
+ end
122
+
123
+ it "accept arbitrary options" do
124
+ assert_equal({:type => :squeak, :volume => 'loud', :source => 'mum'}, @processor.address_for(:type => :squeak, :volume => 'loud', :source => 'mum'))
125
+ end
126
+
127
+ it "complain if no :type given" do
128
+ e = assert_raises RuntimeError do
129
+ @processor.address_for(:source => 'mum')
130
+ end
131
+ assert_equal "You forgot to provide :source or :type", e.message
132
+ end
133
+
134
+ it "complain if no :source given" do
135
+ e = assert_raises RuntimeError do
136
+ @processor.address_for(:type => :footsteps)
137
+ end
138
+ assert_equal "You forgot to provide :source or :type", e.message
131
139
  end
132
140
  end
133
141
  end
@@ -137,18 +145,22 @@ class RequestProcessorHooksTest < MiniTest::Spec
137
145
  include Apotomo::TestCaseMethods::TestController
138
146
  include Apotomo::TestCaseMethods
139
147
 
140
- describe "Hooks in RequestProcessor" do
148
+ describe "RequestProcessor' hooks" do
141
149
  before do
142
150
  @kid = mouse_mock(:kid)
143
151
  @class = Class.new(Apotomo::RequestProcessor)
144
152
  @class.instance_eval do
145
- def kid=(kid); @kid=kid end
146
- def kid; @kid end
153
+ def kid=(kid); @kid = kid; end
154
+ def kid; @kid; end
147
155
  end
148
156
  @class.kid = @kid
149
157
  end
150
158
 
151
159
  describe ":after_initialize hook" do
160
+ # TODO: test when hooks are called
161
+ # TODO: test if block is yielded
162
+ # TODO: test what blocks gets
163
+
152
164
  it "be called after the has_widgets blocks invokation" do
153
165
  @class.after_initialize do |r|
154
166
  r.root[:mum] << self.class.kid # requires that :mum is there, yet.
@@ -170,7 +182,7 @@ class RequestProcessorHooksTest < MiniTest::Spec
170
182
  # DISCUSS: maybe add a trigger test here?
171
183
  @r = @class.new(parent_controller, {},
172
184
  [Proc.new { |root| root << widget(:mouse, :mum) }])
173
- @r.process_for(:source => "root", :type => :noop) # calls ~after_fire.
185
+ @r.process_for(:source => "root", :type => :noop) # calls after_fire hook
174
186
 
175
187
  assert @r.root[:mum][:kid]
176
188
  end
@@ -10,13 +10,8 @@ module Apotomo
10
10
  end
11
11
  end
12
12
 
13
-
14
- # Provides a ready-to-use mouse widget instance.
15
13
  def mouse_mock(id='mouse', opts={}, &block)
16
- #mouse = MouseWidget.new(parent_controller, id, opts)
17
- #mouse.instance_eval &block if block_given?
18
14
  widget(:mouse, id, opts)
19
- #mouse
20
15
  end
21
16
 
22
17
  def mouse_class_mock(&block)
@@ -29,17 +24,14 @@ module Apotomo
29
24
  @mum = mouse('mum')
30
25
  @kid = MouseWidget.new(@mum, 'kid')
31
26
 
32
-
33
27
  @mum.respond_to_event :squeak, :with => :answer_squeak
34
28
  @mum.respond_to_event :squeak, :from => 'kid', :with => :alert
35
29
  @mum.respond_to_event :footsteps, :with => :escape
36
30
 
37
31
  @kid.respond_to_event :footsteps, :with => :peek
38
32
 
39
-
40
33
  @mum.instance_eval do
41
34
  def list; @list ||= []; end
42
-
43
35
  def answer_squeak; self.list << 'answer squeak'; render :text => "squeak", :render_children => false; end
44
36
  def alert; self.list << 'be alerted'; render :text => "alert!", :render_children => false; end
45
37
  def escape; self.list << 'escape'; render :text => "escape", :render_children => false; end
@@ -52,16 +44,23 @@ module Apotomo
52
44
  @mum
53
45
  end
54
46
 
47
+ def root_mum_and_kid!
48
+ mum_and_kid!
49
+
50
+ @root = Apotomo::Widget.new(parent_controller, 'root', :display)
51
+ @root << @mum
52
+ end
53
+
55
54
  def barn_controller!
56
55
  @controller = Class.new(ActionController::Base) do
57
- def self.default_url_options; {:controller => :barn}; end
56
+ def self.default_url_options
57
+ { :controller => :barn }
58
+ end
58
59
  end.new
59
- @controller.extend ActionController::UrlWriter
60
- @controller.params = {}
60
+ @controller.extend(ActionController::UrlWriter)
61
+ @controller.params = {}
61
62
  end
62
-
63
-
64
-
63
+
65
64
  module TestController
66
65
  def setup
67
66
  barn_controller!
@@ -76,9 +75,13 @@ module Apotomo
76
75
  self.request = ActionController::TestRequest.new
77
76
  end
78
77
 
79
- def self.name; "BarnController"; end
78
+ def self.name
79
+ "BarnController"
80
+ end
80
81
 
81
- def self.default_url_options; {:controller => :barn}; end
82
+ def self.default_url_options
83
+ { :controller => :barn }
84
+ end
82
85
  end.new
83
86
  end
84
87
 
@@ -90,8 +93,7 @@ module Apotomo
90
93
  controller = Farm::BarnController.new
91
94
  controller.request = ActionController::TestRequest.new
92
95
  controller
93
- end
94
-
96
+ end
95
97
  end
96
98
  end
97
99
  end
@@ -1,41 +1,41 @@
1
1
  require 'test_helper'
2
2
  require 'apotomo/test_case'
3
3
 
4
- class TestCaseTest < MiniTest::Spec
4
+ class CommentsWidget < Apotomo::Widget
5
+ end
5
6
 
6
- class CommentsWidgetTest < Apotomo::TestCase
7
- end
7
+ class CommentsWidgetTest < Apotomo::TestCase
8
+ end
8
9
 
9
- class CommentsWidget < Apotomo::Widget
10
- end
10
+ class MouseWidgetTest < Apotomo::TestCase
11
+ end
11
12
 
13
+ class TestCaseTest < MiniTest::Spec
12
14
  describe "TestCase" do
13
-
14
15
  describe "responding to #root" do
15
- class MouseWidgetTest < Apotomo::TestCase
16
- end
17
-
18
16
  before do
19
17
  @klass = MouseWidgetTest
20
- @test = @klass.new(:widget).tap{ |t| t.setup }
21
- @klass.has_widgets { |r| r << widget(:mouse, 'mum', :eating) }
18
+ @test = @klass.new(:widget).tap { |t| t.setup }
19
+ @klass.has_widgets do |root|
20
+ root << widget(:mouse, 'mum', :eating)
21
+ end
22
22
  end
23
23
 
24
24
  it "respond to #root" do
25
- assert_equal ['root', 'mum'], @test.root.collect { |w| w.name }
25
+ assert_equal ['root', 'mum'], @test.root.collect(&:name)
26
26
  end
27
27
 
28
28
  it "raise an error if no has_widgets block given" do
29
29
  exc = assert_raises RuntimeError do
30
- @test = Class.new(Apotomo::TestCase).new(:widget).tap{ |t| t.setup }
30
+ @test = Class.new(Apotomo::TestCase).new(:widget).tap { |t| t.setup }
31
31
  @test.root
32
32
  end
33
-
34
33
  assert_equal "Please setup a widget tree using has_widgets()", exc.message
35
34
  end
36
35
 
36
+ # TODO: needed? why root but not self?
37
37
  it "memorize root" do
38
- @test.root.visible=false
38
+ @test.root.visible = false
39
39
  assert_equal false, @test.root.visible?
40
40
  end
41
41
 
@@ -48,9 +48,10 @@ class TestCaseTest < MiniTest::Spec
48
48
  @test.render_widget('mum', :eat)
49
49
 
50
50
  @test.assert_select("div#mum", "burp!")
51
-
52
- exc = assert_raises( MiniTest::Assertion){ @test.assert_select("div#mummy", "burp!"); }
53
- assert_match /Expected at least 1 element matching "div#mummy", found 0/, exc.message
51
+ exc = assert_raises MiniTest::Assertion do
52
+ @test.assert_select("div#mummy", "burp!")
53
+ end
54
+ assert_match "Expected at least 1 element matching \"div#mummy\", found 0", exc.message
54
55
  end
55
56
 
56
57
  describe "using events" do
@@ -59,7 +60,7 @@ class TestCaseTest < MiniTest::Spec
59
60
  @mum.respond_to_event :footsteps, :with => :squeak
60
61
  @mum.instance_eval do
61
62
  def squeak(evt)
62
- render :text => evt.data # this usually leads to "{}".
63
+ render :text => evt.data
63
64
  end
64
65
  end
65
66
  end
@@ -74,6 +75,7 @@ class TestCaseTest < MiniTest::Spec
74
75
 
75
76
  it "respond to #assert_response" do
76
77
  @test.trigger(:footsteps, 'mum')
78
+
77
79
  assert @test.assert_response("{}")
78
80
  end
79
81
  end
@@ -81,6 +83,7 @@ class TestCaseTest < MiniTest::Spec
81
83
  describe "#view_assigns" do
82
84
  it "be emtpy when nothing was set" do
83
85
  @test.render_widget('mum')
86
+
84
87
  assert_equal({}, @test.view_assigns)
85
88
  end
86
89
 
@@ -92,23 +95,17 @@ class TestCaseTest < MiniTest::Spec
92
95
  end
93
96
  end
94
97
  @test.render_widget('mum', :sleep)
98
+
95
99
  assert_equal({:duration => "8h"}, @test.view_assigns)
96
100
  end
97
101
  end
98
102
  end
99
103
 
100
- describe "responding to parent_controller" do
101
- before do
102
- @test = Apotomo::TestCase.new(:widget).tap{ |t| t.setup }
103
- end
104
+ it "respond to #parent_controller and return a controller with correct #controller_path" do
105
+ @test = Apotomo::TestCase.new(:widget).tap { |t| t.setup }
104
106
 
105
- it "provide a test controller" do
106
- assert_kind_of ActionController::Base, @test.parent_controller
107
- end
108
-
109
- it "respond to #controller_path" do
110
- assert_equal "barn", @test.parent_controller.controller_path
111
- end
107
+ assert_kind_of ActionController::Base, @test.parent_controller
108
+ assert_equal "barn", @test.parent_controller.controller_path
112
109
  end
113
110
  end
114
111
  end
data/test/test_helper.rb CHANGED
@@ -9,9 +9,7 @@ require 'apotomo'
9
9
 
10
10
  Apotomo::Widget.append_view_path(File.expand_path(File.dirname(__FILE__) + "/widgets"))
11
11
 
12
- # Load test support files.
13
- require "test_case_methods"
14
-
12
+ require "test_case_methods" # Load test support files.
15
13
 
16
14
  MiniTest::Spec.class_eval do
17
15
  include Apotomo::WidgetShortcuts
@@ -48,9 +46,11 @@ class MouseWidget < Apotomo::Widget
48
46
  def squeak
49
47
  render :text => "squeak!"
50
48
  end
49
+
51
50
  def eating
52
51
  render
53
52
  end
53
+
54
54
  def eat
55
55
  render
56
56
  end