Skip to content

Commit a141dcb

Browse files
committed
Approve acronym solution when using method chaining
1 parent cc85304 commit a141dcb

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

lib/analyzer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
EXERCISES = %w{
22
two_fer
3+
acronym
34
}
45

56
FILENAMES = {

lib/analyzers/acronym/analyze.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Acronym
2+
class Analyze < ExerciseAnalyzer
3+
include Mandate
4+
5+
def analyze!
6+
if solution.uses_method_chain?
7+
self.status = :approve
8+
9+
raise FinishedFlowControlException
10+
end
11+
end
12+
end
13+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Acronym
2+
class Representation < SolutionRepresentation
3+
4+
def uses_method_chain?
5+
target_method.body == s(:send,
6+
s(:send,
7+
s(:send,
8+
s(:send,
9+
s(:send, s(:lvar, :words), :tr, s(:str, "-"), s(:str, " ")),
10+
:split
11+
),
12+
:map,
13+
s(:block_pass, s(:sym, :chr))
14+
),
15+
:join),
16+
:upcase)
17+
end
18+
19+
private
20+
memoize
21+
def target_method
22+
SA::Helpers.extract_module_method(target_module, "abbreviate")
23+
end
24+
25+
memoize
26+
def target_module
27+
SA::Helpers.extract_module_or_class(root_node, "Acronym")
28+
end
29+
end
30+
end

test/exercises/acronym_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test_helper"
2+
require 'pry'
3+
4+
class AcronymTest < Minitest::Test
5+
def test_method_chaining_passes
6+
source = %q{
7+
class Acronym
8+
def self.abbreviate(words)
9+
words.tr('-', ' ').split.map(&:chr).join.upcase
10+
end
11+
end
12+
}
13+
results = Acronym::Analyze.(source)
14+
assert_equal :approve, results[:status]
15+
assert_equal [], results[:comments]
16+
end
17+
end

0 commit comments

Comments
 (0)