Fix some typos.
[ruby_koans.git] / koans / 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   end
20
21   # WRITE CODE HERE
22 end
23
24 # The proxy object should pass the following Koan:
25 #
26 class AboutProxyObjectProject < EdgeCase::Koan
27   def test_proxy_method_returns_wrapped_object
28     # NOTE: The Television class is defined below
29     tv = Proxy.new(Television.new)
30     
31     assert tv.instance_of?(Proxy)
32   end
33   
34   def test_tv_methods_still_perform_their_function
35     tv = Proxy.new(Television.new)
36     
37     tv.channel = 10
38     tv.power
39     
40     assert_equal 10, tv.channel
41     assert tv.on?
42   end
43
44   def test_proxy_records_messages_sent_to_tv
45     tv = Proxy.new(Television.new)
46     
47     tv.power
48     tv.channel = 10
49     
50     assert_equal [:power, :channel=], tv.messages
51   end
52   
53   def test_proxy_handles_invalid_messages
54     tv = Proxy.new(Television.new)
55     
56     assert_raise(NoMethodError) do
57       tv.no_such_method
58     end
59   end
60   
61   def test_proxy_reports_methods_have_been_called
62     tv = Proxy.new(Television.new)
63     
64     tv.power
65     tv.power
66     
67     assert tv.called?(:power)
68     assert ! tv.called?(:channel)
69   end
70   
71   def test_proxy_counts_method_calls
72     tv = Proxy.new(Television.new)
73     
74     tv.power
75     tv.channel = 48
76     tv.power
77
78     assert_equal 2, tv.number_of_times_called(:power)
79     assert_equal 1, tv.number_of_times_called(:channel=)
80     assert_equal 0, tv.number_of_times_called(:on?)
81   end
82
83   def test_proxy_can_record_more_than_just_tv_objects
84     proxy = Proxy.new("Code Mash 2009")
85
86     proxy.upcase!
87     result = proxy.split
88
89     assert_equal ["CODE", "MASH", "2009"], result
90     assert_equal [:upcase!, :split], proxy.messages
91   end
92 end
93
94
95 # ====================================================================
96 # The following code is to support the testing of the Proxy class.  No
97 # changes should be necessary to anything below this comment.
98
99 # Example class using in the proxy testing above.
100 class Television
101   attr_accessor :channel
102   
103   def power
104     if @power == :on
105       @power = :off
106     else
107       @power = :on
108     end
109   end
110   
111   def on?
112     @power == :on
113   end
114 end
115
116 # Tests for the Television class.  All of theses tests should pass.
117 class TelevisionTest < EdgeCase::Koan
118   def test_it_turns_on
119     tv = Television.new
120     
121     tv.power
122     assert tv.on?
123   end
124   
125   def test_it_also_turns_off
126     tv = Television.new
127     
128     tv.power
129     tv.power
130     
131     assert ! tv.on?
132   end
133   
134   def test_edge_case_on_off
135     tv = Television.new
136     
137     tv.power
138     tv.power
139     tv.power
140         
141     assert tv.on?
142     
143     tv.power
144     
145     assert ! tv.on?
146   end
147
148   def test_can_set_the_channel
149     tv = Television.new
150
151     tv.channel = 11
152     assert_equal 11, tv.channel
153   end
154 end