239.patch

Zachary Scott, 01/28/2013 02:19 AM

Download (3.42 KB)

View differences:

lib/observer.rb
124 124
  #          <tt>*arg</tt> is the value passed to #notify_observers by this
125 125
  #          Observable
126 126
  def add_observer(observer, func=:update)
127
    @observer_peers = {} unless defined? @observer_peers
127
    @observer_peers = {}.compare_by_identity unless defined? @observer_peers
128 128
    unless observer.respond_to? func
129 129
      raise NoMethodError, "observer does not respond to `#{func.to_s}'"
130 130
    end
test/test_observer.rb
1
require 'test/unit'
2
require 'observer'
3

  
4
class TestObserver < Test::Unit::TestCase
5

  
6
  class MockObservable
7
    include Observable
8
  end
9

  
10
  class MockObserver
11
    attr_reader :updates
12
    def initialize
13
      @updates = []
14
    end
15
    def update *args
16
      updates.push(args)
17
    end
18
  end
19

  
20
  def setup
21
    @observable = MockObservable.new
22
    @observer = MockObserver.new
23
  end
24

  
25
  def test_count_observers
26
    assert_equal(0, @observable.count_observers)
27
    @observable.add_observer(@observer)
28
    assert_equal(1, @observable.count_observers)
29
    9.times { @observable.add_observer(MockObserver.new) }
30
    assert_equal(10, @observable.count_observers)
31
    @observable.delete_observer(@observer)
32
    assert_equal(9, @observable.count_observers)
33
    @observable.delete_observers()
34
    assert_equal(0, @observable.count_observers)
35
  end
36

  
37
  def test_changed
38
    @observable = MockObservable.new
39
    assert(!@observable.changed?)
40
    @observable.changed
41
    assert(@observable.changed?)
42
    @observable.notify_observers
43
    assert(!@observable.changed?)
44
    @observable.changed(true)
45
    assert(@observable.changed?)
46
    @observable.changed(false)
47
    assert(!@observable.changed?)
48
  end
49

  
50
  def test_notify
51
    @observable.add_observer(@observer)
52
    @observable.changed
53
    @observable.notify_observers()
54
    @observable.changed
55
    @observable.notify_observers(1)
56
    @observable.notify_observers(:not_changed)
57
    @observable.changed
58
    @observable.notify_observers(2, 3)
59
    assert_equal([[], [1], [2, 3]], @observer.updates)
60
  end
61

  
62
  def test_add_delete_observer
63
    @observable.changed
64
    @observable.notify_observers(1)
65
    @observable.add_observer(@observer)
66
    @observable.changed
67
    @observable.notify_observers(2)
68
    @observable.delete_observer(@observer)
69
    @observable.changed
70
    @observable.notify_observers(3)
71
    assert_equal([[2]], @observer.updates)
72
  end
73

  
74
  def test_add_custom_observer
75
    custom_observer = []
76
    @observable.add_observer(custom_observer, :push)
77
    @observable.changed
78
    @observable.notify_observers(1,2,3)
79
    assert_equal([1, 2, 3], custom_observer)
80
    @observable.delete_observer(custom_observer)
81
    @observable.changed
82
    @observable.notify_observers(4,5)
83
    assert_equal([1, 2, 3], custom_observer)
84
  end
85

  
86
end
0
-