1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 # Project: Create a Proxy Class
5 # In this assignment, create a proxy class (one is started for you
6 # below). You should be able to initialize the proxy object with any
7 # object. Any messages sent to the proxy object should be forwarded
8 # to the target object. As each message is sent, the proxy should
9 # record the name of the method sent.
11 # The proxy class is started for you. You will need to add a method
12 # missing handler and any other supporting methods. The specification
13 # of the Proxy class is given in the AboutProxyObjectProject koan.
16 def initialize(target_object)
17 @object = target_object
28 def method_missing(sym, *args, &block)
30 @object.send(sym, *args, &block)
34 @messages.include?(method)
37 def number_of_times_called(method)
38 @messages.select { |m| m == method }.size
43 # The proxy object should pass the following Koan:
45 class AboutProxyObjectProject < EdgeCase::Koan
46 def test_proxy_method_returns_wrapped_object
47 # NOTE: The Television class is defined below
48 tv = Proxy.new(Television.new)
50 # HINT: Proxy class is defined above, may need tweaking...
52 assert tv.instance_of?(Proxy)
55 def test_tv_methods_still_perform_their_function
56 tv = Proxy.new(Television.new)
61 assert_equal 10, tv.channel
65 def test_proxy_records_messages_sent_to_tv
66 tv = Proxy.new(Television.new)
71 assert_equal [:power, :channel=], tv.messages
74 def test_proxy_handles_invalid_messages
75 tv = Proxy.new(Television.new)
77 assert_raise(NoMethodError) do
82 def test_proxy_reports_methods_have_been_called
83 tv = Proxy.new(Television.new)
88 assert tv.called?(:power)
89 assert ! tv.called?(:channel)
92 def test_proxy_counts_method_calls
93 tv = Proxy.new(Television.new)
99 assert_equal 2, tv.number_of_times_called(:power)
100 assert_equal 1, tv.number_of_times_called(:channel=)
101 assert_equal 0, tv.number_of_times_called(:on?)
104 def test_proxy_can_record_more_than_just_tv_objects
105 proxy = Proxy.new("Code Mash 2009")
110 assert_equal ["CODE", "MASH", "2009"], result
111 assert_equal [:upcase!, :split], proxy.messages
116 # ====================================================================
117 # The following code is to support the testing of the Proxy class. No
118 # changes should be necessary to anything below this comment.
120 # Example class using in the proxy testing above.
122 attr_accessor :channel
137 # Tests for the Television class. All of theses tests should pass.
138 class TelevisionTest < EdgeCase::Koan
146 def test_it_also_turns_off
155 def test_edge_case_on_off
169 def test_can_set_the_channel
173 assert_equal 11, tv.channel