1 # frozen_string_literal: true
5 class TestObserver < Test::Unit::TestCase
11 notify_observers(*args)
16 def initialize(observable)
18 observable.add_observer(self)
21 attr_reader :notifications
24 @notifications << args
29 observable = TestObservable.new
31 assert_equal(0, observable.count_observers)
33 watcher1 = TestWatcher.new(observable)
35 assert_equal(1, observable.count_observers)
37 observable.notify("test", 123)
39 watcher2 = TestWatcher.new(observable)
41 assert_equal(2, observable.count_observers)
45 assert_equal([["test", 123], [42]], watcher1.notifications)
46 assert_equal([[42]], watcher2.notifications)
48 observable.delete_observer(watcher1)
50 assert_equal(1, observable.count_observers)
52 observable.notify(:cats)
54 assert_equal([["test", 123], [42]], watcher1.notifications)
55 assert_equal([[42], [:cats]], watcher2.notifications)
57 observable.delete_observers
59 assert_equal(0, observable.count_observers)
61 observable.notify("nope")
63 assert_equal([["test", 123], [42]], watcher1.notifications)
64 assert_equal([[42], [:cats]], watcher2.notifications)