Skip to content

Commit 5bcf49e

Browse files
committed
Approve acronym when using scan method
1 parent 9ec22d3 commit 5bcf49e

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/analyzers/acronym/analyze.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Analyze < ExerciseAnalyzer
33
include Mandate
44

55
def analyze!
6-
if solution.uses_method_chain? || solution.uses_scan?
6+
if solution.uses_method_chain? || solution.uses_scan? || solution.uses_split?
77
self.status = :approve
88

99
raise FinishedFlowControlException

lib/analyzers/acronym/representation.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ def uses_scan?
3939
all? { |node, i| node_matches?(node, matchers[i]) }
4040
end
4141

42+
def uses_split?
43+
matchers = [
44+
{
45+
method_name: :upcase,
46+
},
47+
{
48+
method_name: :join,
49+
chained?: true
50+
},
51+
{
52+
method_name: :map,
53+
chained?: true,
54+
arguments: [{ to_ast: s(:block_pass, s(:sym, :chr)) }]
55+
},
56+
{
57+
method_name: :split,
58+
receiver: s(:lvar, :words),
59+
arguments: [{ type: :regexp }]
60+
}
61+
]
62+
63+
target_method.
64+
body.
65+
each_node(:send).
66+
with_index.
67+
all? { |node, i| node_matches?(node, matchers[i]) }
68+
end
69+
4270
private
4371
memoize
4472
def target_method

test/exercises/acronym_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,17 @@ def self.abbreviate(words)
2727
assert_equal :approve, results[:status]
2828
assert_equal [], results[:comments]
2929
end
30+
31+
def test_split_with_any_regex_passes
32+
source = %q{
33+
class Acronym
34+
def self.abbreviate(words)
35+
words.split(/[ -]/).map(&:chr).join.upcase
36+
end
37+
end
38+
}
39+
results = Acronym::Analyze.(source)
40+
assert_equal :approve, results[:status]
41+
assert_equal [], results[:comments]
42+
end
3043
end

0 commit comments

Comments
 (0)