forked from bitmakerlabs/learn_ruby_rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
172 lines (145 loc) · 6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<html>
<head>
<title>Test-First Teaching: learn_ruby: temperature_object</title>
<link href="../assets/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="header">
<h1><a href="http://testfirst.org">TestFirst.org</a></h1>
<h2>the home of test-first teaching</h2>
</div>
<div class="nav">
<h2><a href="../index.html">learn_ruby</a></h2>
<b>Labs:</b>
<ul>
<li><a href="../00_hello/index.html">00 Hello</a></li>
<li><a href="../01_temperature/index.html">01 Temperature</a></li>
<li><a href="../02_calculator/index.html">02 Calculator</a></li>
<li><a href="../03_simon_says/index.html">03 Simon Says</a></li>
<li><a href="../04_pig_latin/index.html">04 Pig Latin</a></li>
<li><a href="../05_silly_blocks/index.html">05 Silly Blocks</a></li>
<li><a href="../06_performance_monitor/index.html">06 Performance Monitor</a></li>
<li><a href="../07_hello_friend/index.html">07 Hello Friend</a></li>
<li><a href="../08_book_titles/index.html">08 Book Titles</a></li>
<li><a href="../09_timer/index.html">09 Timer</a></li>
<li>10 Temperature Object</li>
<li><a href="../11_dictionary/index.html">11 Dictionary</a></li>
<li><a href="../12_rpn_calculator/index.html">12 Rpn Calculator</a></li>
<li><a href="../13_xml_document/index.html">13 Xml Document</a></li>
<li><a href="../14_array_extensions/index.html">14 Array Extensions</a></li>
<li><a href="../15_in_words/index.html">15 In Words</a></li>
</ul>
</div>
<h1>temperature_object</h1>
<div class="content"><div class="rspec_file"> <div class="intro"><h1>Topics:</h1>
<ul>
<li>floating-point math</li>
<li>objects</li>
<li>constructors</li>
<li>class methods</li>
<li>factory methods</li>
<li>options hashes</li>
</ul>
<h1>Hints</h1>
<p>Remember that one degree fahrenheit is 5/9 of one degree celsius, and that the freezing point of water is 0 degrees celsius but 32 degrees fahrenheit.</p>
<p>Remember to define the <code>from_celsius</code> factory method as a <em>class</em> method, not an <em>instance</em> method.</p>
<p>The temperature object's constructor should accept an <em>options hash</em> which contains either a <code>:celcius</code> entry or a <code>:fahrenheit</code> entry.</p>
</div>
<div class="tests">
<h1>Tests</h1>
<a class="raw_file" href="temperature_object_spec.rb">temperature_object_spec.rb</a>
<pre>
require "temperature"
describe Temperature do
describe "can be constructed with an options hash" do
describe "in degrees fahrenheit" do
it "at 50 degrees" do
Temperature.new(:f => 50).in_fahrenheit.should == 50
end
describe "and correctly convert to celsius" do
it "at freezing" do
Temperature.new(:f => 32).in_celsius.should == 0
end
it "at boiling" do
Temperature.new(:f => 212).in_celsius.should == 100
end
it "at body temperature" do
Temperature.new(:f => 98.6).in_celsius.should == 37
end
it "at an arbitrary temperature" do
Temperature.new(:f => 68).in_celsius.should == 20
end
end
end
describe "in degrees celsius" do
it "at 50 degrees" do
Temperature.new(:c => 50).in_celsius.should == 50
end
describe "and correctly convert to fahrenheit" do
it "at freezing" do
Temperature.new(:c => 0).in_fahrenheit.should == 32
end
it "at boiling" do
Temperature.new(:c => 100).in_fahrenheit.should == 212
end
it "at body temperature" do
Temperature.new(:c => 37).in_fahrenheit.should be_within(0.1).of(98.6)
# Why do we need to use be_within here?
# See http://www.ruby-forum.com/topic/169330
# and http://groups.google.com/group/rspec/browse_thread/thread/f3ebbe3c313202bb
# Also, try "puts 0.5 - 0.4 - 0.1" -- pretty crazy, right?
end
end
end
end
# Factory Method is a design pattern, not a Ruby language feature.
# One way to implement this pattern in Ruby is via class methods --
# that is, methods defined on the class (Temperature) rather than
# on individual instances of the class.
describe "can be constructed via factory methods" do
it "in degrees celsius" do
Temperature.from_celsius(50).in_celsius.should == 50
Temperature.from_celsius(50).in_fahrenheit.should == 122
end
it "in degrees fahrenheit" do
Temperature.from_fahrenheit(50).in_fahrenheit.should == 50
Temperature.from_fahrenheit(50).in_celsius.should == 10
end
end
# test-driving bonus:
#
# 1. make two class methods -- ftoc and ctof
# 2. refactor to call those methods from the rest of the object
#
# run *all* the tests during your refactoring, to make sure you did it right
#
describe "utility class methods" do
end
# Here's another way to solve the problem!
describe "Temperature subclasses" do
describe "Celsius subclass" do
it "is constructed in degrees celsius" do
Celsius.new(50).in_celsius.should == 50
Celsius.new(50).in_fahrenheit.should == 122
end
it "is a Temperature subclass" do
Celsius.new(0).should be_a(Temperature)
end
end
describe "Fahrenheit subclass" do
it "is constructed in degrees fahrenheit" do
Fahrenheit.new(50).in_fahrenheit.should == 50
Fahrenheit.new(50).in_celsius.should == 10
end
it "is a Temperature subclass" do
Fahrenheit.new(0).should be_a(Temperature)
end
end
end
end</pre>
</div>
</div>
</div>
<div class="footer"><a href="http://testfirst.org">TestFirst.org</a></div>
</body>
</html>