1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutStrings < EdgeCase::Koan
4 def test_double_quoted_strings_are_strings
5 string = "Hello, World"
6 assert_equal __, string.is_a?(String)
9 def test_single_quoted_strings_are_also_strings
10 string = 'Goodbye, World'
11 assert_equal __, string.is_a?(String)
14 def test_use_single_quotes_to_create_string_with_double_quotes
15 string = 'He said, "Go Away."'
16 assert_equal __, string
19 def test_use_double_quotes_to_create_strings_with_single_quotes
21 assert_equal __, string
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
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
38 def test_flexible_quotes_can_handle_multiple_lines
40 It was the best of times,
41 It was the worst of times.
43 assert_equal __, long_string.size
46 def test_here_documents_can_also_handle_multiple_lines
48 It was the best of times,
49 It was the worst of times.
51 assert_equal __, long_string.size
54 def test_plus_will_concatenate_two_strings
55 string = "Hello, " + "World"
56 assert_equal __, string
59 def test_plus_concatenation_will_leave_the_original_strings_unmodified
64 assert_equal __, there
67 def test_plus_equals_will_concatenate_to_the_end_of_a_string
74 def test_plus_equals_also_will_leave_the_original_string_unmodified
75 original_string = "Hello, "
79 assert_equal __, original_string
82 def test_the_shovel_operator_will_also_append_content_to_a_string
87 assert_equal __, there
90 def test_the_shovel_operator_modifies_the_original_string
91 original_string = "Hello, "
95 assert_equal __, original_string
99 # Ruby programmers tend to favor the shovel operator (<<) over the
100 # plus equals operator (+=) when building up strings. Why?
103 def test_double_quoted_string_interpret_escape_characters
105 assert_equal __, string.size
108 def test_single_quoted_string_do_not_interpret_escape_characters
110 assert_equal __, string.size
113 def test_single_quotes_sometimes_interpret_escape_characters
115 assert_equal __, string.size
116 assert_equal __, string
119 def test_double_quoted_strings_interpolate_variables
121 string = "The value is #{value}"
122 assert_equal __, string
125 def test_single_quoted_strings_do_not_interpolate
127 string = 'The value is #{value}'
128 assert_equal __, string
131 def test_any_ruby_expression_may_be_interpolated
132 string = "The square root of 5 is #{Math.sqrt(5)}"
133 assert_equal __, string
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]
142 in_ruby_version("1.8") do
143 def test_in_ruby_1_8_single_characters_are_represented_by_integers
145 assert_equal __, ?a == 97
147 assert_equal __, ?b == (?a + 1)
151 in_ruby_version("1.9") do
152 def test_in_ruby_1_9_single_characters_are_represented_by_strings
154 assert_equal __, ?a == 97
158 in_ruby_version("1.8") do
159 def test_in_ruby_1_8_you_can_get_a_single_character_from_a_string
160 string = "Bacon, lettuce and tomato"
161 assert_equal __, string[1]
167 in_ruby_version("1.9") do
169 def test_in_ruby_1_9_you_can_get_a_single_character_from_a_string
170 string = "Bacon, lettuce and tomato"
171 assert_equal "a", string[1]
178 def test_strings_can_be_split
179 string = "Sausage Egg Cheese"
181 assert_equal [__, __, __], words
184 def test_strings_can_be_split_with_different_patterns
185 string = "the:rain:in:spain"
186 words = string.split(/:/)
187 assert_equal [__, __, __, __], words
189 # NOTE: Patterns are formed from Regular Expressions. Ruby has a
190 # very powerful Regular Expression library. We will become
191 # enlightened about them soon.
194 def test_strings_can_be_joined
195 words = ["Now", "is", "the", "time"]
196 assert_equal __, words.join(" ")
199 def test_strings_are_unique_objects
203 assert_equal __, a == b
204 assert_equal __, a.object_id == b.object_id