Skip to content

Commit 9ec22d3

Browse files
committed
Approve acronym when using scan without any regexp
1 parent c161a01 commit 9ec22d3

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

lib/analyzers/acronym/representation.rb

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Acronym
22
class Representation < SolutionRepresentation
3-
43
def uses_method_chain?
54
target_method.body == s(:send,
65
s(:send,
@@ -17,15 +16,27 @@ def uses_method_chain?
1716
end
1817

1918
def uses_scan?
20-
target_method.body == s(:send,
21-
s(:send,
22-
s(:send,
23-
s(:lvar, :words),
24-
:scan,
25-
s(:regexp, s(:str, "\\b[[:alpha:]]"), s(:regopt))
26-
),
27-
:join),
28-
:upcase)
19+
matchers = [
20+
{
21+
method_name: :upcase,
22+
},
23+
{
24+
method_name: :join,
25+
chained?: true
26+
},
27+
{
28+
method_name: :scan,
29+
receiver: s(:lvar, :words),
30+
chained?: true,
31+
arguments: [{ type: :regexp }]
32+
},
33+
]
34+
35+
target_method.
36+
body.
37+
each_node(:send).
38+
with_index.
39+
all? { |node, i| node_matches?(node, matchers[i]) }
2940
end
3041

3142
private
@@ -38,5 +49,23 @@ def target_method
3849
def target_module
3950
SA::Helpers.extract_module_or_class(root_node, "Acronym")
4051
end
52+
53+
def node_matches?(node, matcher)
54+
matcher.
55+
map do |criteria|
56+
key, expected_value = *criteria
57+
criteria_value = node.send(key)
58+
59+
case criteria_value
60+
when Array
61+
criteria_value.each_with_index.all? do |n, i|
62+
node_matches?(n, expected_value[i])
63+
end
64+
else
65+
criteria_value == expected_value
66+
end
67+
end.
68+
all?
69+
end
4170
end
4271
end

test/exercises/acronym_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def self.abbreviate(words)
1515
assert_equal [], results[:comments]
1616
end
1717

18-
def test_scan_passes
18+
def test_scan_with_any_regex_passes
1919
source = %q{
2020
class Acronym
2121
def self.abbreviate(words)
22-
words.scan(/\b[[:alpha:]]/).join.upcase
22+
words.scan(/any/).join.upcase
2323
end
2424
end
2525
}

0 commit comments

Comments
 (0)