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.length
44 assert_equal __, long_string.lines.count
47 def test_here_documents_can_also_handle_multiple_lines
49 It was the best of times,
50 It was the worst of times.
52 assert_equal __, long_string.length
53 assert_equal __, long_string.lines.count
56 def test_plus_will_concatenate_two_strings
57 string = "Hello, " + "World"
58 assert_equal __, string
61 def test_plus_concatenation_will_leave_the_original_strings_unmodified
66 assert_equal __, there
69 def test_plus_equals_will_concatenate_to_the_end_of_a_string
76 def test_plus_equals_also_will_leave_the_original_string_unmodified
77 original_string = "Hello, "
81 assert_equal __, original_string
84 def test_the_shovel_operator_will_also_append_content_to_a_string
89 assert_equal __, there
92 def test_the_shovel_operator_modifies_the_original_string
93 original_string = "Hello, "
97 assert_equal __, original_string
101 # Ruby programmers tend to favor the shovel operator (<<) over the
102 # plus equals operator (+=) when building up strings. Why?
105 def test_double_quoted_string_interpret_escape_characters
107 assert_equal __, string.size
110 def test_single_quoted_string_do_not_interpret_escape_characters
112 assert_equal __, string.size
115 def test_single_quotes_sometimes_interpret_escape_characters
117 assert_equal __, string.size
118 assert_equal __, string
121 def test_double_quoted_strings_interpolate_variables
123 string = "The value is #{value}"
124 assert_equal __, string
127 def test_single_quoted_strings_do_not_interpolate
129 string = 'The value is #{value}'
130 assert_equal __, string
133 def test_any_ruby_expression_may_be_interpolated
134 string = "The square root of 5 is #{Math.sqrt(5)}"
135 assert_equal __, string
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]
144 def test_you_can_get_a_single_character_from_a_string
145 string = "Bacon, lettuce and tomato"
146 assert_equal __, string[1]
151 in_ruby_version("1.8") do
152 def test_in_ruby_1_8_single_characters_are_represented_by_integers
154 assert_equal __, ?a == 97
156 assert_equal __, ?b == (?a + 1)
160 in_ruby_version("1.9") do
161 def test_in_ruby_1_9_single_characters_are_represented_by_strings
163 assert_equal __, ?a == 97
167 def test_strings_can_be_split
168 string = "Sausage Egg Cheese"
170 assert_equal [__, __, __], words
173 def test_strings_can_be_split_with_different_patterns
174 string = "the:rain:in:spain"
175 words = string.split(/:/)
176 assert_equal [__, __, __, __], words
178 # NOTE: Patterns are formed from Regular Expressions. Ruby has a
179 # very powerful Regular Expression library. We will become
180 # enlightened about them soon.
183 def test_strings_can_be_joined
184 words = ["Now", "is", "the", "time"]
185 assert_equal __, words.join(" ")
188 def test_strings_are_unique_objects
192 assert_equal __, a == b
193 assert_equal __, a.object_id == b.object_id