aa427d48f4712c667e405364b4d37b667c881281
[ruby_koans.git] / koans / about_strings.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutStrings < EdgeCase::Koan
4   def test_double_quoted_strings_are_strings
5     string = "Hello, World"
6     assert_equal __, string.is_a?(String)
7   end
8
9   def test_single_quoted_strings_are_also_strings
10     string = 'Goodbye, World'
11     assert_equal __, string.is_a?(String)
12   end
13
14   def test_use_single_quotes_to_create_string_with_double_quotes
15     string = 'He said, "Go Away."'
16     assert_equal __, string
17   end
18
19   def test_use_double_quotes_to_create_strings_with_single_quotes
20     string = "Don't"
21     assert_equal __, string
22   end
23
24   def test_use_backslash_for_those_hard_cases
25     a = "He said, \"Don't\""
26     b = 'He said, "Don\'t"'
27     assert_equal __, a == b
28   end
29
30   def test_use_flexible_quoting_to_handle_really_hard_cases
31     a = %(flexible quotes can handle both ' and " characters)
32     b = %!flexible quotes can handle both ' and " characters!
33     c = %{flexible quotes can handle both ' and " characters}
34     assert_equal __, a == b
35     assert_equal __, a == c
36   end
37
38   def test_flexible_quotes_can_handle_multiple_lines
39     long_string = %{
40 It was the best of times,
41 It was the worst of times.
42 }
43     assert_equal __, long_string.length
44     assert_equal __, long_string.lines.count
45   end
46
47   def test_here_documents_can_also_handle_multiple_lines
48     long_string = <<EOS
49 It was the best of times,
50 It was the worst of times.
51 EOS
52     assert_equal __, long_string.length
53     assert_equal __, long_string.lines.count
54   end
55
56   def test_plus_will_concatenate_two_strings
57     string = "Hello, " + "World"
58     assert_equal __, string
59   end
60
61   def test_plus_concatenation_will_leave_the_original_strings_unmodified
62     hi = "Hello, "
63     there = "World"
64     string = hi + there
65     assert_equal __, hi
66     assert_equal __, there
67   end
68
69   def test_plus_equals_will_concatenate_to_the_end_of_a_string
70     hi = "Hello, "
71     there = "World"
72     hi += there
73     assert_equal __, hi
74   end
75
76   def test_plus_equals_also_will_leave_the_original_string_unmodified
77     original_string = "Hello, "
78     hi = original_string
79     there = "World"
80     hi += there
81     assert_equal __, original_string
82   end
83
84   def test_the_shovel_operator_will_also_append_content_to_a_string
85     hi = "Hello, "
86     there = "World"
87     hi << there
88     assert_equal __, hi
89     assert_equal __, there
90   end
91
92   def test_the_shovel_operator_modifies_the_original_string
93     original_string = "Hello, "
94     hi = original_string
95     there = "World"
96     hi << there
97     assert_equal __, original_string
98
99     # THINK ABOUT IT:
100     #
101     # Ruby programmers tend to favor the shovel operator (<<) over the
102     # plus equals operator (+=) when building up strings.  Why?
103   end
104
105   def test_double_quoted_string_interpret_escape_characters
106     string = "\n"
107     assert_equal __, string.size
108   end
109
110   def test_single_quoted_string_do_not_interpret_escape_characters
111     string = '\n'
112     assert_equal __, string.size
113   end
114
115   def test_single_quotes_sometimes_interpret_escape_characters
116     string = '\\\''
117     assert_equal __, string.size
118     assert_equal __, string
119   end
120
121   def test_double_quoted_strings_interpolate_variables
122     value = 123
123     string = "The value is #{value}"
124     assert_equal __, string
125   end
126
127   def test_single_quoted_strings_do_not_interpolate
128     value = 123
129     string = 'The value is #{value}'
130     assert_equal __, string
131   end
132
133   def test_any_ruby_expression_may_be_interpolated
134     string = "The square root of 5 is #{Math.sqrt(5)}"
135     assert_equal __, string
136   end
137
138   def test_you_can_get_a_substring_from_a_string
139     string = "Bacon, lettuce and tomato"
140     assert_equal __, string[7,3]
141     assert_equal __, string[7..9]
142   end
143
144   in_ruby_version("1.8") do
145     def test_in_ruby_1_8_single_characters_are_represented_by_integers
146       assert_equal __, ?a
147       assert_equal __, ?a == 97
148
149       assert_equal __, ?b == (?a + 1)
150     end
151   end
152
153   in_ruby_version("1.9") do
154     def test_in_ruby_1_9_single_characters_are_represented_by_strings
155       assert_equal __, ?a
156       assert_equal __, ?a == 97
157     end
158   end
159
160 in_ruby_version("1.8") do
161     def test_in_ruby_1_8_you_can_get_a_single_character_from_a_string
162       string = "Bacon, lettuce and tomato"
163       assert_equal __, string[1]
164
165       # Surprised?
166     end
167   end
168   
169   in_ruby_version("1.9") do
170
171     def test_in_ruby_1_9_you_can_get_a_single_character_from_a_string
172       string = "Bacon, lettuce and tomato"
173       assert_equal "__", string[1]
174
175       # Surprised?
176     end
177   end
178
179
180   def test_strings_can_be_split
181     string = "Sausage Egg Cheese"
182     words = string.split
183     assert_equal [__, __, __], words
184   end
185
186   def test_strings_can_be_split_with_different_patterns
187     string = "the:rain:in:spain"
188     words = string.split(/:/)
189     assert_equal [__, __, __, __], words
190
191     # NOTE: Patterns are formed from Regular Expressions.  Ruby has a
192     # very powerful Regular Expression library.  We will become
193     # enlightened about them soon.
194   end
195
196   def test_strings_can_be_joined
197     words = ["Now", "is", "the", "time"]
198     assert_equal __, words.join(" ")
199   end
200
201   def test_strings_are_unique_objects
202     a = "a string"
203     b = "a string"
204
205     assert_equal __, a           == b
206     assert_equal __, a.object_id == b.object_id
207   end
208 end