fixing typo
[ruby_koans.git] / src / about_proxy_object_project.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 # Project: Create a Proxy Class
4 #
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.
10 #
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.
14
15 class Proxy
16   def initialize(target_object)
17     @object = target_object
18     # ADD MORE CODE HERE
19     #--
20     @messages = []
21     #++
22   end
23
24   # WRITE CODE HERE
25   #--
26   attr_reader :messages
27
28   def method_missing(sym, *args, &block)
29     @messages << sym
30     @object.send(sym, *args, &block)
31   end
32   
33   def called?(method)
34     @messages.include?(method)
35   end
36
37   def number_of_times_called(method)
38     @messages.select { |m| m == method }.size
39   end
40   #++
41 end
42
43 # The proxy object should pass the following Koan:
44 #
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)
49     
50     assert tv.instance_of?(Proxy)
51   end
52   
53   def test_tv_methods_still_perform_their_function
54     tv = Proxy.new(Television.new)
55     
56     tv.channel = 10
57     tv.power
58     
59     assert_equal 10, tv.channel
60     assert tv.on?
61   end
62
63   def test_proxy_records_messages_sent_to_tv
64     tv = Proxy.new(Television.new)
65     
66     tv.power
67     tv.channel = 10
68     
69     assert_equal [:power, :channel=], tv.messages
70   end
71   
72   def test_proxy_handles_invalid_messages
73     tv = Proxy.new(Television.new)
74     
75     assert_raise(NoMethodError) do
76       tv.no_such_method
77     end
78   end
79   
80   def test_proxy_reports_methods_have_been_called
81     tv = Proxy.new(Television.new)
82     
83     tv.power
84     tv.power
85     
86     assert tv.called?(:power)
87     assert ! tv.called?(:channel)
88   end
89   
90   def test_proxy_counts_method_calls
91     tv = Proxy.new(Television.new)
92     
93     tv.power
94     tv.channel = 48
95     tv.power
96
97     assert_equal 2, tv.number_of_times_called(:power)
98     assert_equal 1, tv.number_of_times_called(:channel=)
99     assert_equal 0, tv.number_of_times_called(:on?)
100   end
101
102   def test_proxy_can_record_more_than_just_tv_objects
103     proxy = Proxy.new("Code Mash 2009")
104
105     proxy.upcase!
106     result = proxy.split
107
108     assert_equal ["CODE", "MASH", "2009"], result
109     assert_equal [:upcase!, :split], proxy.messages
110   end
111 end
112
113
114 # ====================================================================
115 # The following code is to support the testing of the Proxy class.  No
116 # changes should be necessary to anything below this comment.
117
118 # Example class using in the proxy testing above.
119 class Television
120   attr_accessor :channel
121   
122   def power
123     if @power == :on
124       @power = :off
125     else
126       @power = :on
127     end
128   end
129   
130   def on?
131     @power == :on
132   end
133 end
134
135 # Tests for the Television class.  All of theses tests should pass.
136 class TelevisionTest < EdgeCase::Koan
137   def test_it_turns_on
138     tv = Television.new
139     
140     tv.power
141     assert tv.on?
142   end
143   
144   def test_it_also_turns_off
145     tv = Television.new
146     
147     tv.power
148     tv.power
149     
150     assert ! tv.on?
151   end
152   
153   def test_edge_case_on_off
154     tv = Television.new
155     
156     tv.power
157     tv.power
158     tv.power
159         
160     assert tv.on?
161     
162     tv.power
163     
164     assert ! tv.on?
165   end
166
167   def test_can_set_the_channel
168     tv = Television.new
169
170     tv.channel = 11
171     assert_equal 11, tv.channel
172   end
173 end