27ccf72039d9ccda630ce4183176f9063cd39eb7
[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.size
44   end
45
46   def test_here_documents_can_also_handle_multiple_lines
47     long_string = <<EOS
48 It was the best of times,
49 It was the worst of times.
50 EOS
51     assert_equal __, long_string.size
52   end
53
54   def test_plus_will_concatenate_two_strings
55     string = "Hello, " + "World"
56     assert_equal __, string
57   end
58
59   def test_plus_concatenation_will_leave_the_original_strings_unmodified
60     hi = "Hello, "
61     there = "World"
62     string = hi + there
63     assert_equal __, hi
64     assert_equal __, there
65   end
66
67   def test_plus_equals_will_concatenate_to_the_end_of_a_string
68     hi = "Hello, "
69     there = "World"
70     hi += there
71     assert_equal __, hi
72   end
73
74   def test_plus_equals_also_will_leave_the_original_string_unmodified
75     original_string = "Hello, "
76     hi = original_string
77     there = "World"
78     hi += there
79     assert_equal __, original_string
80   end
81
82   def test_the_shovel_operator_will_also_append_content_to_a_string
83     hi = "Hello, "
84     there = "World"
85     hi << there
86     assert_equal __, hi
87     assert_equal __, there
88   end
89
90   def test_the_shovel_operator_modifies_the_original_string
91     original_string = "Hello, "
92     hi = original_string
93     there = "World"
94     hi << there
95     assert_equal __, original_string
96
97     # THINK ABOUT IT:
98     #
99     # Ruby programmers tend to favor the shovel operator (<<) over the
100     # plus equals operator (+=) when building up strings.  Why?
101   end
102
103   def test_double_quoted_string_interpret_escape_characters
104     string = "\n"
105     assert_equal __, string.size
106   end
107
108   def test_single_quoted_string_do_not_interpret_escape_characters
109     string = '\n'
110     assert_equal __, string.size
111   end
112
113   def test_single_quotes_sometimes_interpret_escape_characters
114     string = '\\\''
115     assert_equal __, string.size
116     assert_equal __, string
117   end
118
119   def test_double_quoted_strings_interpolate_variables
120     value = 123
121     string = "The value is #{value}"
122     assert_equal __, string
123   end
124
125   def test_single_quoted_strings_do_not_interpolate
126     value = 123
127     string = 'The value is #{value}'
128     assert_equal __, string
129   end
130
131   def test_any_ruby_expression_may_be_interpolated
132     string = "The square root of 5 is #{Math.sqrt(5)}"
133     assert_equal __, string
134   end
135
136   def test_you_can_get_a_substring_from_a_string
137     string = "Bacon, lettuce and tomato"
138     assert_equal __, string[7,3]
139     assert_equal __, string[7..9]
140   end
141
142   def test_you_can_get_a_single_character_from_a_string
143     string = "Bacon, lettuce and tomato"
144     assert_equal __, string[1]
145
146     # Surprised?
147   end
148
149   in_ruby_version("1.8") do
150     def test_in_ruby_1_8_single_characters_are_represented_by_integers
151       assert_equal __, ?a
152       assert_equal __, ?a == 97
153
154       assert_equal __, ?b == (?a + 1)
155     end
156   end
157
158   in_ruby_version("1.9") do
159     def test_in_ruby_1_9_single_characters_are_represented_by_strings
160       assert_equal __, ?a
161       assert_equal __, ?a == 97
162     end
163   end
164
165   def test_strings_can_be_split
166     string = "Sausage Egg Cheese"
167     words = string.split
168     assert_equal [__, __, __], words
169   end
170
171   def test_strings_can_be_split_with_different_patterns
172     string = "the:rain:in:spain"
173     words = string.split(/:/)
174     assert_equal [__, __, __, __], words
175
176     # NOTE: Patterns are formed from Regular Expressions.  Ruby has a
177     # very powerful Regular Expression library.  We will become
178     # enlightened about them soon.
179   end
180
181   def test_strings_can_be_joined
182     words = ["Now", "is", "the", "time"]
183     assert_equal __, words.join(" ")
184   end
185
186   def test_strings_are_unique_objects
187     a = "a string"
188     b = "a string"
189
190     assert_equal __, a           == b
191     assert_equal __, a.object_id == b.object_id
192   end
193 end