55b85f678eb79a75f44e3fc645a3c5307b615b47
[ruby_koans.git] / src / about_regular_expressions.rb
1 # -*- coding: utf-8 -*-
2 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3
4 class AboutRegularExpressions < EdgeCase::Koan
5   def test_a_pattern_is_a_regular_expression
6     assert_equal __(Regexp), /pattern/.class
7   end
8
9   def test_a_regexp_can_search_a_string_for_matching_content
10     assert_equal __("match"), "some matching content"[/match/]
11   end
12
13   def test_a_failed_match_returns_nil
14     assert_equal __(nil), "some matching content"[/missing/]
15   end
16
17   # ------------------------------------------------------------------
18
19   def test_question_mark_means_optional
20     assert_equal __("ab"), "abbcccddddeeeee"[/ab?/]
21     assert_equal __("a"), "abbcccddddeeeee"[/az?/]
22   end
23
24   def test_plus_means_one_or_more
25     assert_equal __("bccc"), "abbcccddddeeeee"[/bc+/]
26   end
27
28   def test_asterisk_means_zero_or_more
29     assert_equal __("abb"), "abbcccddddeeeee"[/ab*/]
30     assert_equal __("a"), "abbcccddddeeeee"[/az*/]
31     assert_equal __(""), "abbcccddddeeeee"[/z*/]
32
33     # THINK ABOUT IT:
34     #
35     # When would * fail to match?
36   end
37
38   # THINK ABOUT IT:
39   #
40   # We say that the repetition operators above are "greedy."
41   #
42   # Why?
43
44   # ------------------------------------------------------------------
45
46   def test_the_left_most_match_wins
47     assert_equal __("a"), "abbccc az"[/az*/]
48   end
49
50   # ------------------------------------------------------------------
51
52   def test_character_classes_give_options_for_a_character
53     animals = ["cat", "bat", "rat", "zat"]
54     assert_equal __(["cat", "bat", "rat"]), animals.select { |a| a[/[cbr]at/] }
55   end
56
57   def test_slash_d_is_a_shortcut_for_a_digit_character_class
58     assert_equal __("42"), "the number is 42"[/[0123456789]+/]
59     assert_equal __("42"), "the number is 42"[/\d+/]
60   end
61
62   def test_character_classes_can_include_ranges
63     assert_equal __("42"), "the number is 42"[/[0-9]+/]
64   end
65
66   def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
67     assert_equal __(" \t\n"), "space: \t\n"[/\s+/]
68   end
69
70   def test_slash_w_is_a_shortcut_for_a_word_character_class
71     # NOTE:  This is more like how a programmer might define a word.
72     assert_equal __("variable_1"), "variable_1 = 42"[/[a-zA-Z0-9_]+/]
73     assert_equal __("variable_1"), "variable_1 = 42"[/\w+/]
74   end
75
76   def test_period_is_a_shortcut_for_any_non_newline_character
77     assert_equal __("abc"), "abc\n123"[/a.+/]
78   end
79
80   def test_a_character_class_can_be_negated
81     assert_equal __("the number is "), "the number is 42"[/[^0-9]+/]
82   end
83
84   def test_shortcut_character_classes_are_negated_with_capitals
85     assert_equal __("the number is "), "the number is 42"[/\D+/]
86     assert_equal __("space:"), "space: \t\n"[/\S+/]
87     assert_equal __(" = "), "variable_1 = 42"[/\W+/]
88   end
89
90   # ------------------------------------------------------------------
91
92   def test_slash_a_anchors_to_the_start_of_the_string
93     assert_equal __("start"), "start end"[/\Astart/]
94     assert_equal __(nil), "start end"[/\Aend/]
95   end
96
97   def test_slash_z_anchors_to_the_end_of_the_string
98     assert_equal __("end"), "start end"[/end\z/]
99     assert_equal __(nil), "start end"[/start\z/]
100   end
101
102   def test_caret_anchors_to_the_start_of_lines
103     assert_equal __("2"), "num 42\n2 lines"[/^\d+/]
104   end
105
106   def test_dollar_sign_anchors_to_the_end_of_lines
107     assert_equal __("42"), "2 lines\nnum 42"[/\d+$/]
108   end
109
110   def test_slash_b_anchors_to_a_word_boundary
111     assert_equal __("vines"), "bovine vines"[/\bvine./]
112   end
113
114   # ------------------------------------------------------------------
115
116   def test_parentheses_group_contents
117     assert_equal __("hahaha"), "ahahaha"[/(ha)+/]
118   end
119
120   # ------------------------------------------------------------------
121
122   def test_parentheses_also_capture_matched_content_by_number
123     assert_equal __("Gray"), "Gray, James"[/(\w+), (\w+)/, 1]
124     assert_equal __("James"), "Gray, James"[/(\w+), (\w+)/, 2]
125   end
126
127   def test_variables_can_also_be_used_to_access_captures
128     assert_equal __("Gray, James"), "Name:  Gray, James"[/(\w+), (\w+)/]
129     assert_equal __("Gray"), $1
130     assert_equal __("James"), $2
131   end
132
133   # ------------------------------------------------------------------
134
135   def test_a_vertical_pipe_means_or
136     grays = /(James|Dana|Summer) Gray/
137     assert_equal __("James Gray"), "James Gray"[grays]
138     assert_equal __("Summer"), "Summer Gray"[grays, 1]
139     assert_equal __(nil), "Jim Gray"[grays, 1]
140   end
141
142   # THINK ABOUT IT:
143   #
144   # Explain the difference between a character class ([...]) and alternation (|).
145
146   # ------------------------------------------------------------------
147
148   def test_scan_is_like_find_all
149     assert_equal __(["one", "two", "three"]), "one two-three".scan(/\w+/)
150   end
151
152   def test_sub_is_like_find_and_replace
153     assert_equal __("one t-three"), "one two-three".sub(/(t\w*)/) { $1[0, 1] }
154   end
155
156   def test_gsub_is_like_find_and_replace_all
157     assert_equal __("one t-t"), "one two-three".gsub(/(t\w*)/) { $1[0, 1] }
158   end
159 end