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 __(true), string.is_a?(String)
9 def test_single_quoted_strings_are_also_strings
10 string = 'Goodbye, World'
11 assert_equal __(true), 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 __('He said, "Go Away."'), string
19 def test_use_double_quotes_to_create_strings_with_single_quotes
21 assert_equal __("Don't"), 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 __(true), 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 __(true), a == b
35 assert_equal __(true), 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 __(54), 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 __(53), long_string.size
54 def test_plus_will_concatenate_two_strings
55 string = "Hello, " + "World"
56 assert_equal __("Hello, World"), string
59 def test_plus_concatenation_will_leave_the_original_strings_unmodified
63 assert_equal __("Hello, "), hi
64 assert_equal __("World"), there
67 def test_plus_equals_will_concatenate_to_the_end_of_a_string
71 assert_equal __("Hello, World"), hi
74 def test_plus_equals_also_will_leave_the_original_string_unmodified
75 original_string = "Hello, "
79 assert_equal __("Hello, "), original_string
82 def test_the_shovel_operator_will_also_append_content_to_a_string
86 assert_equal __("Hello, World"), hi
87 assert_equal __("World"), there
90 def test_the_shovel_operator_modifies_the_original_string
91 original_string = "Hello, "
95 assert_equal __("Hello, World"), 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 __(1), string.size
108 def test_single_quoted_string_do_not_interpret_escape_characters
110 assert_equal __(2), string.size
113 def test_single_quotes_sometimes_interpret_escape_characters
115 assert_equal __(2), string.size
116 assert_equal __("\\'"), string
119 def test_double_quoted_strings_interpolate_variables
121 string = "The value is #{value}"
122 assert_equal __("The value is 123"), string
125 def test_single_quoted_strings_do_not_interpolate
127 string = 'The value is #{value}'
128 assert_equal __('The value is #{value}'), string
131 def test_any_ruby_expression_may_be_interpolated
132 string = "The square root of 5 is #{Math.sqrt(5)}"
133 assert_equal __("The square root of 5 is 2.23606797749979"), string
136 def test_you_can_get_a_substring_from_a_string
137 string = "Bacon, lettuce and tomato"
138 assert_equal __("let"), string[7,3]
139 assert_equal __("let"), string[7..9]
142 def test_you_can_get_a_single_character_from_a_string
143 string = "Bacon, lettuce and tomato"
144 assert_equal __(97, 'a'), string[1]
149 in_ruby_version("1.8") do
150 def test_in_ruby_1_8_single_characters_are_represented_by_integers
151 assert_equal __(97, 'a'), ?a
152 assert_equal __(true, false), ?a == 97
154 assert_equal __(true), ?b == (?a + 1)
158 in_ruby_version("1.9") do
159 def test_in_ruby_1_9_single_characters_are_represented_by_strings
160 assert_equal __('a'), ?a
161 assert_equal __(false), ?a == 97
165 def test_strings_can_be_split
166 string = "Sausage Egg Cheese"
168 assert_equal [__("Sausage"), __("Egg"), __("Cheese")], words
171 def test_strings_can_be_split_with_different_patterns
172 string = "the:rain:in:spain"
173 words = string.split(/:/)
174 assert_equal [__("the"), __("rain"), __("in"), __("spain")], words
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.
181 def test_strings_can_be_joined
182 words = ["Now", "is", "the", "time"]
183 assert_equal __("Now is the time"), words.join(" ")
186 def test_strings_are_unique_objects
190 assert_equal __(true), a == b
191 assert_equal __(false), a.object_id == b.object_id