Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 105 additions and 98 deletions.
  1. +37 −37 test/event_methods_test.rb
  2. +25 −23 test/test_case_methods.rb
  3. +43 −38 test/widget_test.rb
View
74 test/event_methods_test.rb
@@ -1,68 +1,68 @@
require 'test_helper'
-
+
class EventMethodsTest < Test::Unit::TestCase
include Apotomo::TestCaseMethods::TestController
-
+
def handler(id, state)
Apotomo::InvokeEventHandler.new(:widget_id => id, :state => state)
end
-
-
+
+
context "#respond_to_event and #fire" do
setup do
mum_and_kid!
end
-
+
should "alert @mum first, then make her squeak when @kid squeaks" do
@kid.fire :squeak
assert_equal ['be alerted', 'answer squeak'], @mum.list
end
-
+
should "make @mum just squeak back when jerry squeaks" do
@mum << mouse_mock(:jerry)
@mum[:jerry].fire :squeak
assert_equal ['answer squeak'], @mum.list
end
-
-
+
+
should "make @mum run away while @kid keeps watching" do
@kid.fire :footsteps
assert_equal ['peek', 'escape'], @mum.list
end
-
+
should "by default add a handler only once" do
@mum.respond_to_event :peep, :with => :answer_squeak
@mum.respond_to_event :peep, :with => :answer_squeak
@mum.fire :peep
assert_equal ['answer squeak'], @mum.list
end
-
+
should "squeak back twice when using the :once => false option" do
@mum.respond_to_event :peep, :with => :answer_squeak
@mum.respond_to_event :peep, :with => :answer_squeak, :once => false
@mum.fire :peep
assert_equal ['answer squeak', 'answer squeak'], @mum.list
end
-
+
should "also accept an event argument only" do
@mum.respond_to_event :answer_squeak
@mum.fire :answer_squeak
assert_equal ['answer squeak'], @mum.list
- end
-
+ end
+
should "make pass the event into the triggered state" do
@mum.instance_eval do
respond_to_event :footsteps
-
+
def footsteps(evt)
list << evt
end
end
-
+
@mum.trigger :footsteps, "near"
assert_kind_of Apotomo::Event, @mum.list.last
end
-
+
should "accept payload data for the event" do
@mum.respond_to_event :answer_squeak
@mum.instance_eval do
@@ -70,35 +70,35 @@ def answer_squeak(evt)
list << evt.data
end
end
-
+
@mum.fire :answer_squeak, :volume => 9
assert_equal [{:volume => 9}], @mum.list
end
-
-
+
+
context "#responds_to_event with :passing" do
setup do
class AdolescentMouse < MouseWidget
responds_to_event :squeak, :passing => :root
end
-
+
@root = mouse(:root)
end
-
+
should "add handlers to root when called with :passing" do
AdolescentMouse.new(@root, 'jerry')
-
+
assert_equal [handler('jerry', :squeak)], @root.event_table.all_handlers_for(:squeak, 'jerry')
end
-
+
should "inherit :passing handlers" do
Class.new(AdolescentMouse).new(@root, 'jerry')
-
+
assert_equal [handler('jerry', :squeak)], @root.event_table.all_handlers_for(:squeak, 'jerry')
end
-
+
end
-
+
context "#responds_to_event in class context" do
class AdultMouse < Apotomo::Widget
responds_to_event :peep, :with => :answer_squeak
@@ -107,46 +107,46 @@ class BabyMouse < AdultMouse
responds_to_event :peep
responds_to_event :footsteps, :with => :squeak
end
-
+
setup do
@mum = AdultMouse.new(parent_controller, 'mum')
end
-
+
should "add the handlers at creation time" do
assert_equal [handler('mum', :answer_squeak)], @mum.event_table.all_handlers_for(:peep, 'mum')
end
-
+
should "inherit handlers" do
assert_equal [[:peep, {:with=>:answer_squeak}]], AdultMouse.responds_to_event_options
assert_equal [[:peep, {:with=>:answer_squeak}], [:peep], [:footsteps, {:with=>:squeak}]], BabyMouse.responds_to_event_options
end
-
+
should "not share responds_to_event options between different instances" do
assert_equal [handler('mum', :answer_squeak)], @mum.event_table.all_handlers_for(:peep, 'mum')
-
+
assert_equal [handler('dad', :answer_squeak)], AdultMouse.new(parent_controller, 'dad', :show).event_table.all_handlers_for(:peep, 'dad')
end
end
-
+
context "#trigger" do
should "be an alias for #fire" do
@kid.trigger :footsteps
assert_equal ['peek', 'escape'], @mum.list
end
end
-
-
+
+
context "page_updates" do
should "expose a simple Array for now" do
assert_kind_of Array, @mum.page_updates
assert_equal 0, @mum.page_updates.size
end
-
+
should "be queued in root#page_updates after #fire" do
@mum.fire :footsteps
assert_equal ["escape"], @mum.page_updates
end
end
-
- end
+
+ end
end
View
48 test/test_case_methods.rb
@@ -3,14 +3,14 @@ module TestCaseMethods
def root_mock
MouseWidget.new(parent_controller, :root)
end
-
+
def mouse(id=nil, &block)
MouseWidget.new(parent_controller, id || :mouse).tap do |widget|
widget.instance_eval &block if block_given?
end
end
-
-
+
+
# Provides a ready-to-use mouse widget instance.
def mouse_mock(id='mouse', opts={}, &block)
#mouse = MouseWidget.new(parent_controller, id, opts)
@@ -18,40 +18,42 @@ def mouse_mock(id='mouse', opts={}, &block)
widget(:mouse, id, opts)
#mouse
end
-
+
def mouse_class_mock(&block)
klass = Class.new(MouseWidget)
klass.instance_eval &block if block_given?
klass
end
-
+
def mum_and_kid!
@mum = mouse('mum')
- @kid = MouseWidget.new(@mum, 'kid')
-
-
+ @kid = MouseWidget.new(@mum, 'kid')
+
+
@mum.respond_to_event :squeak, :with => :answer_squeak
@mum.respond_to_event :squeak, :from => 'kid', :with => :alert
@mum.respond_to_event :footsteps, :with => :escape
-
+ @mum.respond_to_event :load, :with => :load
+
@kid.respond_to_event :footsteps, :with => :peek
-
-
+
+
@mum.instance_eval do
def list; @list ||= []; end
-
+
def answer_squeak; self.list << 'answer squeak'; render :text => "squeak", :render_children => false; end
def alert; self.list << 'be alerted'; render :text => "alert!", :render_children => false; end
def escape; self.list << 'escape'; render :text => "escape", :render_children => false; end
+ def load; self.list << 'load'; render :text => "load", :render_children => false; end
end
-
+
@kid.instance_eval do
def peek; root.list << 'peek'; render :text => "" end
end
-
+
@mum
end
-
+
def barn_controller!
@controller = Class.new(ActionController::Base) do
def self.default_url_options; {:controller => :barn}; end
@@ -59,14 +61,14 @@ def self.default_url_options; {:controller => :barn}; end
@controller.extend ActionController::UrlWriter
@controller.params = {}
end
-
-
+
+
module TestController
def setup
barn_controller!
end
-
+
# Creates a mock controller instance. Currently, each widget needs a parent controller instance due to some
# sucky dependency in cells.
def barn_controller!
@@ -75,23 +77,23 @@ def initialize(*)
super
self.request = ActionController::TestRequest.new
end
-
+
def self.name; "BarnController"; end
-
+
def self.default_url_options; {:controller => :barn}; end
end.new
end
-
+
def parent_controller
@controller
end
-
+
def namespaced_controller
controller = Farm::BarnController.new
controller.request = ActionController::TestRequest.new
controller
end
-
+
end
end
end
View
81 test/widget_test.rb
@@ -2,7 +2,7 @@
class WidgetTest < ActiveSupport::TestCase
include Apotomo::TestCaseMethods::TestController
-
+
context "The constructor" do
should "accept the parent_controller as first arg" do
assert_kind_of ActionController::Base, @controller
@@ -18,105 +18,105 @@ class WidgetTest < ActiveSupport::TestCase
#MouseWidget.new(me, :baby) # this is also possible.
end
end.new(@controller, 'mum')
-
+
@kid = Class.new(@mum.class).new(@controller, 'mum')
end
-
+
should "setup the widget family at creation time" do
assert_equal 1, @mum.children.size
assert_kind_of MouseWidget, @mum[:baby]
end
-
+
should "inherit trees for now" do
assert_equal 1, @mum.children.size
assert_kind_of MouseWidget, @mum[:baby]
end
end
-
-
+
+
context "A widget" do
setup do
@mum = Apotomo::Widget.new(@controller, 'mum', :squeak)
end
-
+
context "responding to #address_for_event" do
should "accept an event :type" do
assert_equal({:source=>"mum", :type=>:squeak, :controller=>"barn"}, @mum.address_for_event(:squeak))
end
-
+
should "accept a :source" do
assert_equal({:source=>"kid", :type=>:squeak, :controller=>"barn"}, @mum.address_for_event(:squeak, :source => 'kid'))
end
-
+
should "accept arbitrary options" do
assert_equal({:volume=>"loud", :source=>"mum", :type=>:squeak, :controller=>"barn"}, @mum.address_for_event(:squeak, :volume => 'loud'))
end
-
+
should "work with controller namespaces" do
@mum = Apotomo::Widget.new(namespaced_controller, 'mum', :squeak)
assert_equal({:source=>"mum", :type=>:squeak, :controller=>"farm/barn"}, @mum.address_for_event(:squeak))
end
end
-
+
context "implementing visibility" do
should "per default respond to #visible?" do
assert @mum.visible?
end
-
+
should "expose a setter therefore" do
@mum.visible = false
assert_not @mum.visible?
end
end
-
+
context "#find_widget" do
setup do
mum_and_kid!
end
-
+
should "find itself" do
assert_equal @mum, @mum.find_widget('mum')
end
-
+
should "return nil for not-existant widgets" do
assert_nil @mum.find_widget('pet')
end
-
+
should "find children" do
assert_equal @kid, @mum.find_widget('kid')
end
-
+
should "find treat 'id' and :id the same" do
assert_equal @mum.find_widget(:kid), @mum.find_widget('kid')
end
end
-
+
should "respond to the WidgetShortcuts methods, like #widget" do
assert_respond_to @mum, :widget
end
-
+
should "respond to #parent_controller and return the AC in root" do
@mum << mouse_mock(:kid)
assert_equal @controller, @mum.parent_controller
assert_equal @controller, @mum[:kid].parent_controller
end
-
+
should "alias #widget_id to #name" do
assert_equal @mum.name, @mum.widget_id
end
-
+
should "respond to DEFAULT_VIEW_PATHS" do
assert_equal ["app/widgets"], Apotomo::Widget::DEFAULT_VIEW_PATHS
end
-
+
should "respond to .view_paths" do
assert_equal ActionView::PathSet.new(Apotomo::Widget::DEFAULT_VIEW_PATHS + ["test/widgets"]), Apotomo::Widget.view_paths
end
-
+
should "respond to .controller_path" do
assert_equal "mouse", MouseWidget.controller_path
end
-
+
# internal_methods:
should "not list internal methods in action_methods" do
# FIXME: puts "WTF is wrong again with AC.action_methods godamn, I HATE this magic shit!"
@@ -124,64 +124,69 @@ class WidgetTest < ActiveSupport::TestCase
assert_equal [], Class.new(Apotomo::Widget).action_methods
end
end
-
+
should "list both local and inherited states in Widget.action_methods" do
assert MouseWidget.action_methods.collect{ |m| m.to_s }.include?("squeak")
assert Class.new(MouseWidget).action_methods.collect{ |m| m.to_s }.include?("squeak")
end
-
+
+ should "list 'load' in local and inherited states in Widget.action_methods" do
+ assert MouseWidget.action_methods.collect{ |m| m.to_s }.include?("load")
+ assert Class.new(MouseWidget).action_methods.collect{ |m| m.to_s }.include?("load")
+ end
+
should "not list #display in internal_methods although it's defined in Object" do
assert_not Apotomo::Widget.internal_methods.include?(:display)
end
end
end
-
+
class RenderWidgetTest < ActiveSupport::TestCase
include Apotomo::TestCaseMethods::TestController
-
+
context "#render_widget" do
should "allow passing widget id" do
assert_equal "squeak!", mouse.render_widget('mouse', :squeak)
end
-
+
should "allow passing widget instance" do
assert_equal 'squeak!', mouse.render_widget(mouse(:mum), :squeak)
end
-
+
should "use :display as standard state" do
mum = mouse('Mum') do
def display
render :text => "#{widget_id}, that's me!"
end
end
-
+
assert_equal "Mum, that's me!", mouse.render_widget(mum)
end
-
+
should "raise an exception when a non-existent widget id is passed" do
e = assert_raises RuntimeError do
mouse.render_widget('mummy')
end
-
+
assert_equal "Couldn't render non-existent widget `mummy`", e.message
end
-
+
should "pass options as state-args" do
mum = mouse do
def display(color="grey")
render :text => "I'm #{color}"
end
end
-
+
assert_equal("I'm grey", mouse.render_widget(mum), "default value in state-arg didn't work")
assert_equal("I'm black", mouse.render_widget(mum, :display, "black"))
end
-
+
should "use #find_widget from self to find the passed widget id" do
mum = mouse << mouse_mock(:kid)
-
+
assert_equal "<div id=\"kid\">burp!</div>\n", mum.render_widget(:kid, :eat)
- end
+ end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.