1 # -*- coding: utf-8 -*-
2 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
4 class AboutRegularExpressions < EdgeCase::Koan
5 def test_a_pattern_is_a_regular_expression
6 assert_equal __(Regexp), /pattern/.class
9 def test_a_regexp_can_search_a_string_for_matching_content
10 assert_equal __("match"), "some matching content"[/match/]
13 def test_a_failed_match_returns_nil
14 assert_equal __(nil), "some matching content"[/missing/]
17 # ------------------------------------------------------------------
19 def test_question_mark_means_optional
20 assert_equal __("ab"), "abbcccddddeeeee"[/ab?/]
21 assert_equal __("a"), "abbcccddddeeeee"[/az?/]
24 def test_plus_means_one_or_more
25 assert_equal __("bccc"), "abbcccddddeeeee"[/bc+/]
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*/]
35 # When would * fail to match?
40 # We say that the repetition operators above are "greedy."
44 # ------------------------------------------------------------------
46 def test_the_left_most_match_wins
47 assert_equal __("a"), "abbccc az"[/az*/]
50 # ------------------------------------------------------------------
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/] }
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+/]
62 def test_character_classes_can_include_ranges
63 assert_equal __("42"), "the number is 42"[/[0-9]+/]
66 def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
67 assert_equal __(" \t\n"), "space: \t\n"[/\s+/]
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+/]
76 def test_period_is_a_shortcut_for_any_non_newline_character
77 assert_equal __("abc"), "abc\n123"[/a.+/]
80 def test_a_character_class_can_be_negated
81 assert_equal __("the number is "), "the number is 42"[/[^0-9]+/]
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 # ... a programmer would most likely do
88 assert_equal __(" = "), "variable_1 = 42"[/[^a-zA-Z0-9_]+/]
89 assert_equal __(" = "), "variable_1 = 42"[/\W+/]
92 # ------------------------------------------------------------------
94 def test_slash_a_anchors_to_the_start_of_the_string
95 assert_equal __("start"), "start end"[/\Astart/]
96 assert_equal __(nil), "start end"[/\Aend/]
99 def test_slash_z_anchors_to_the_end_of_the_string
100 assert_equal __("end"), "start end"[/end\z/]
101 assert_equal __(nil), "start end"[/start\z/]
104 def test_caret_anchors_to_the_start_of_lines
105 assert_equal __("2"), "num 42\n2 lines"[/^\d+/]
108 def test_dollar_sign_anchors_to_the_end_of_lines
109 assert_equal __("42"), "2 lines\nnum 42"[/\d+$/]
112 def test_slash_b_anchors_to_a_word_boundary
113 assert_equal __("vines"), "bovine vines"[/\bvine./]
116 # ------------------------------------------------------------------
118 def test_parentheses_group_contents
119 assert_equal __("hahaha"), "ahahaha"[/(ha)+/]
122 # ------------------------------------------------------------------
124 def test_parentheses_also_capture_matched_content_by_number
125 assert_equal __("Gray"), "Gray, James"[/(\w+), (\w+)/, 1]
126 assert_equal __("James"), "Gray, James"[/(\w+), (\w+)/, 2]
129 def test_variables_can_also_be_used_to_access_captures
130 assert_equal __("Gray, James"), "Name: Gray, James"[/(\w+), (\w+)/]
131 assert_equal __("Gray"), $1
132 assert_equal __("James"), $2
135 # ------------------------------------------------------------------
137 def test_a_vertical_pipe_means_or
138 grays = /(James|Dana|Summer) Gray/
139 assert_equal __("James Gray"), "James Gray"[grays]
140 assert_equal __("Summer"), "Summer Gray"[grays, 1]
141 assert_equal __(nil), "Jim Gray"[grays, 1]
146 # Explain the difference between a character class ([...]) and alternation (|).
148 # ------------------------------------------------------------------
150 def test_scan_is_like_find_all
151 assert_equal __(["one", "two", "three"]), "one two-three".scan(/\w+/)
154 def test_sub_is_like_find_and_replace
155 assert_equal __("one t-three"), "one two-three".sub(/(t\w*)/) { $1[0, 1] }
158 def test_gsub_is_like_find_and_replace_all
159 assert_equal __("one t-t"), "one two-three".gsub(/(t\w*)/) { $1[0, 1] }