Sha256: 3d9f4ed35dc4806588f38d7c334a8b8589c4e932957eba4f09b38c3c4c008048

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# Copyright (c) 2010 Paolo Capriotti <p.capriotti@gmail.com>
# 
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.

require 'test/unit'
require 'rui/observer_utils'

class TestObserverUtils < Test::Unit::TestCase
  class FakeObservable
    include Observable
  end
  
  def setup
    @object = FakeObservable.new
  end
  
  def test_simple_observer
    ok = false
    @object.on(:something) { ok = true }
    @object.fire :something
    assert ok
  end
  
  def test_observer
    obs = Object.new
    class << obs
      include Observer
      attr_reader :arg
      
      def on_something(arg)
        @arg = arg
      end
    end
    
    @object.add_observer(obs)
    @object.changed
    @object.notify_observers :something => 37
    
    assert_equal 37, obs.arg
  end
  
  def test_multiple_observer
    obs = Object.new
    class << obs
      include Observer
      attr_reader :arg1, :arg2
      
      def on_whatever(arg1)
        @arg1 = arg1
      end
      
      def on_something(arg2)
        @arg2 = arg2
      end
    end
    
    @object.add_observer(obs)
    @object.changed
    @object.notify_observers :something => 2, :whatever => 1, :nothing => 3
    
    assert_equal 1, obs.arg1
    assert_equal 2, obs.arg2
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rui-0.1.2 test/test_observer_utils.rb
rui-0.1.0 test/test_observer_utils.rb