Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moving on 110/274

  • Loading branch information...
commit 53a6853a6f8a59974e3812cf665cef252480ac6e 1 parent b747cc7
SK authored

Showing 2 changed files with 37 additions and 37 deletions. Show diff stats Hide diff stats

  1. +5 5 about_methods.rb
  2. +32 32 about_regular_expressions.rb
10 about_methods.rb
@@ -7,18 +7,18 @@ def my_global_method(a,b)
7 7 class AboutMethods < EdgeCase::Koan
8 8
9 9 def test_calling_global_methods
10   - assert_equal __, my_global_method(2,3)
  10 + assert_equal 5, my_global_method(2,3)
11 11 end
12 12
13 13 def test_calling_global_methods_without_parentheses
14 14 result = my_global_method 2, 3
15   - assert_equal __, result
  15 + assert_equal 5, result
16 16 end
17 17
18 18 # (NOTE: We are Using eval below because the example code is
19 19 # considered to be syntactically invalid).
20 20 def test_sometimes_missing_parentheses_are_ambiguous
21   - eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
  21 + eval "assert_equal 5, my_global_method (2, 3)" # ENABLE CHECK
22 22 #
23 23 # Ruby doesn't know if you mean:
24 24 #
@@ -33,10 +33,10 @@ def test_sometimes_missing_parentheses_are_ambiguous
33 33 # NOTE: wrong number of argument is not a SYNTAX error, but a
34 34 # runtime error.
35 35 def test_calling_global_methods_with_wrong_number_of_arguments
36   - exception = assert_raise(___) do
  36 + exception = assert_raise(ArgumentError) do
37 37 my_global_method
38 38 end
39   - assert_match(/__/, exception.message)
  39 + assert_match(/"wrong number of arguments"/, exception.message)
40 40
41 41 exception = assert_raise(___) do
42 42 my_global_method(1,2,3)
64 about_regular_expressions.rb
@@ -44,99 +44,99 @@ def test_asterisk_means_zero_or_more
44 44 # ------------------------------------------------------------------
45 45
46 46 def test_the_left_most_match_wins
47   - assert_equal __, "abbccc az"[/az*/]
  47 + assert_equal "a", "abbccc az"[/az*/]
48 48 end
49 49
50 50 # ------------------------------------------------------------------
51 51
52 52 def test_character_classes_give_options_for_a_character
53 53 animals = ["cat", "bat", "rat", "zat"]
54   - assert_equal __, animals.select { |a| a[/[cbr]at/] }
  54 + assert_equal ["cat", "bat", "rat"], animals.select { |a| a[/[cbr]at/] }
55 55 end
56 56
57 57 def test_slash_d_is_a_shortcut_for_a_digit_character_class
58   - assert_equal __, "the number is 42"[/[0123456789]+/]
59   - assert_equal __, "the number is 42"[/\d+/]
  58 + assert_equal "42", "the number is 42"[/[0123456789]+/]
  59 + assert_equal "42", "the number is 42"[/\d+/]
60 60 end
61 61
62 62 def test_character_classes_can_include_ranges
63   - assert_equal __, "the number is 42"[/[0-9]+/]
  63 + assert_equal "42", "the number is 42"[/[0-9]+/]
64 64 end
65 65
66 66 def test_slash_s_is_a_shortcut_for_a_whitespace_character_class
67   - assert_equal __, "space: \t\n"[/\s+/]
  67 + assert_equal " \t\n", "space: \t\n"[/\s+/]
68 68 end
69 69
70 70 def test_slash_w_is_a_shortcut_for_a_word_character_class
71 71 # NOTE: This is more like how a programmer might define a word.
72   - assert_equal __, "variable_1 = 42"[/[a-zA-Z0-9_]+/]
73   - assert_equal __, "variable_1 = 42"[/\w+/]
  72 + assert_equal "variable_1", "variable_1 = 42"[/[a-zA-Z0-9_]+/]
  73 + assert_equal "variable_1", "variable_1 = 42"[/\w+/]
74 74 end
75 75
76 76 def test_period_is_a_shortcut_for_any_non_newline_character
77   - assert_equal __, "abc\n123"[/a.+/]
  77 + assert_equal "abc", "abc\n123"[/a.+/]
78 78 end
79 79
80 80 def test_a_character_class_can_be_negated
81   - assert_equal __, "the number is 42"[/[^0-9]+/]
  81 + assert_equal "the number is ", "the number is 42"[/[^0-9]+/]
82 82 end
83 83
84 84 def test_shortcut_character_classes_are_negated_with_capitals
85   - assert_equal __, "the number is 42"[/\D+/]
86   - assert_equal __, "space: \t\n"[/\S+/]
87   - assert_equal __, "variable_1 = 42"[/\W+/]
  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+/] ##Exclude words keep symbols
88 88 end
89 89
90 90 # ------------------------------------------------------------------
91 91
92 92 def test_slash_a_anchors_to_the_start_of_the_string
93   - assert_equal __, "start end"[/\Astart/]
94   - assert_equal __, "start end"[/\Aend/]
  93 + assert_equal "start", "start end"[/\Astart/]
  94 + assert_equal nil, "start end"[/\Aend/]
95 95 end
96 96
97 97 def test_slash_z_anchors_to_the_end_of_the_string
98   - assert_equal __, "start end"[/end\z/]
99   - assert_equal __, "start end"[/start\z/]
  98 + assert_equal "end", "start end"[/end\z/]
  99 + assert_equal nil, "start end"[/start\z/]
100 100 end
101 101
102 102 def test_caret_anchors_to_the_start_of_lines
103   - assert_equal __, "num 42\n2 lines"[/^\d+/]
  103 + assert_equal "2", "num 42\n2 lines"[/^\d+/]
104 104 end
105 105
106 106 def test_dollar_sign_anchors_to_the_end_of_lines
107   - assert_equal __, "2 lines\nnum 42"[/\d+$/]
  107 + assert_equal "42", "2 lines\nnum 42"[/\d+$/]
108 108 end
109 109
110 110 def test_slash_b_anchors_to_a_word_boundary
111   - assert_equal __, "bovine vines"[/\bvine./]
  111 + assert_equal "vines", "bovine vines"[/\bvine./]
112 112 end
113 113
114 114 # ------------------------------------------------------------------
115 115
116 116 def test_parentheses_group_contents
117   - assert_equal __, "ahahaha"[/(ha)+/]
  117 + assert_equal "hahaha", "ahahaha"[/(ha)+/]
118 118 end
119 119
120 120 # ------------------------------------------------------------------
121 121
122 122 def test_parentheses_also_capture_matched_content_by_number
123   - assert_equal __, "Gray, James"[/(\w+), (\w+)/, 1]
124   - assert_equal __, "Gray, James"[/(\w+), (\w+)/, 2]
  123 + assert_equal "Gray", "Gray, James"[/(\w+), (\w+)/, 1]
  124 + assert_equal "James", "Gray, James"[/(\w+), (\w+)/, 2]
125 125 end
126 126
127 127 def test_variables_can_also_be_used_to_access_captures
128   - assert_equal __, "Name: Gray, James"[/(\w+), (\w+)/]
129   - assert_equal __, $1
130   - assert_equal __, $2
  128 + assert_equal "Gray, James", "Name: Gray, James"[/(\w+), (\w+)/]
  129 + assert_equal "Gray", $1
  130 + assert_equal "James", $2
131 131 end
132 132
133 133 # ------------------------------------------------------------------
134 134
135 135 def test_a_vertical_pipe_means_or
136 136 grays = /(James|Dana|Summer) Gray/
137   - assert_equal __, "James Gray"[grays]
138   - assert_equal __, "Summer Gray"[grays, 1]
139   - assert_equal __, "Jim Gray"[grays, 1]
  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 140 end
141 141
142 142 # THINK ABOUT IT:
@@ -146,14 +146,14 @@ def test_a_vertical_pipe_means_or
146 146 # ------------------------------------------------------------------
147 147
148 148 def test_scan_is_like_find_all
149   - assert_equal __, "one two-three".scan(/\w+/)
  149 + assert_equal ["one", "two", "three"], "one two-three".scan(/\w+/)
150 150 end
151 151
152 152 def test_sub_is_like_find_and_replace
153   - assert_equal __, "one two-three".sub(/(t\w*)/) { $1[0, 1] }
  153 + assert_equal "one t-three", "one two-three".sub(/(t\w*)/) { $1[0, 1] }
154 154 end
155 155
156 156 def test_gsub_is_like_find_and_replace_all
157   - assert_equal __, "one two-three".gsub(/(t\w*)/) { $1[0, 1] }
  157 + assert_equal "one t-t", "one two-three".gsub(/(t\w*)/) { $1[0, 1] }
158 158 end
159 159 end

0 comments on commit 53a6853

Please sign in to comment.
Something went wrong with that request. Please try again.