Skip to content

Commit 19c3b60

Browse files
committed
Add fixtures and conform to interface
1 parent eef24b1 commit 19c3b60

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require "rake/testtask"
33
Rake::TestTask.new(:test) do |t|
44
t.libs << "test"
55
t.libs << "lib"
6-
t.test_files = FileList["test/**/*_test.rb"]
6+
t.test_files = FileList["test/**/*_test.rb"].exclude("test/fixtures/**/*")
77
end
88

99
task :default => :test

lib/analyzers/exercise_analyzer.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class ExerciseAnalyzer
22

3-
# This is just flow-control for quickly exiting the
4-
# analysis. We probably don't want to do things this
3+
# This is just flow-control for quickly exiting the
4+
# analysis. We probably don't want to do things this
55
# way eventually, but it helps remove noise for now.
66
class FinishedFlowControlException < RuntimeError
77
end
88

99
def initialize(code_to_analyze)
1010
@code_to_analyze = code_to_analyze
11-
@approve = false
12-
@refer_to_mentor = false
13-
@messages = []
11+
@status = nil
12+
@comments = []
1413
end
1514

1615
def call
@@ -23,17 +22,16 @@ def call
2322

2423
def results
2524
{
26-
approve: approve,
27-
refer_to_mentor: refer_to_mentor,
28-
messages: messages
25+
status: status,
26+
comments: comments
2927
}
3028
end
3129

3230
private
3331
attr_reader :code_to_analyze
3432

3533
protected
36-
attr_accessor :approve, :refer_to_mentor, :messages
34+
attr_accessor :status, :comments
3735

3836
def root_node
3937
@root_node ||= begin

lib/analyzers/two_fer/analyze.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,34 +214,32 @@ def default_argument_value
214214
default_argument.children[0]
215215
end
216216

217-
218217
# ###
219218
# Flow helpers
220219
#
221220
# These are totally generic to all exercises and
222221
# can probably be extracted to parent
223222
# ###
224223
def approve!(msg = nil)
225-
self.messages << MESSAGES[msg] if msg
226-
self.approve = true
224+
if msg
225+
self.status = :approve_with_comment
226+
self.comments << MESSAGES[msg]
227+
else
228+
self.status = :approve_as_optimal
229+
end
227230

228231
raise FinishedFlowControlException
229232
end
230233

231234
def refer_to_mentor!
232-
self.refer_to_mentor = true
233-
234-
# Refering to mentor is an explicit
235-
# command resulting from weirdness
236-
# and should override approving
237-
self.approve = false
235+
self.status = :refer_to_mentor
238236

239237
raise FinishedFlowControlException
240238
end
241239

242240
def disapprove!(msg, *msg_args)
243-
self.messages << (MESSAGES[msg] % msg_args)
244-
self.approve = false
241+
self.status = :disapprove_with_comment
242+
self.comments << (MESSAGES[msg] % msg_args)
245243

246244
raise FinishedFlowControlException
247245
end

test/exercises/two_fer_test.rb

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
require "test_helper"
22
require 'pry'
33

4-
class HelloCollectorTest < Minitest::Test
4+
class TwoFerTest < Minitest::Test
5+
6+
def test_fixtures
7+
dir = File.expand_path("#{__FILE__}/../../fixtures/two-fer/")
8+
Dir.foreach(dir).each do |id|
9+
next unless File.exist?("#{dir}/#{id}/analysis.json")
10+
11+
expected = TwoFer::Analyze.(File.read("#{dir}/#{id}/two_fer.rb"))
12+
actual = JSON.parse(File.read("#{dir}/#{id}/analysis.json"))
13+
14+
assert_equal expected[:status].to_s, actual['status']
15+
assert_equal expected[:comments], actual['comments']
16+
end
17+
end
18+
519
# ###
620
# Test the module/class
721
# ###
@@ -15,8 +29,8 @@ def self.two_fer(name="you")
1529
end
1630
}
1731
results = TwoFer::Analyze.(source)
18-
assert results[:approve]
19-
assert_equal [], results[:messages]
32+
assert_equal :approve_as_optimal, results[:status]
33+
assert_equal [], results[:comments]
2034
end
2135

2236
def test_simple_module_passes
@@ -29,8 +43,8 @@ def self.two_fer(name="you")
2943
end
3044
}
3145
results = TwoFer::Analyze.(source)
32-
assert results[:approve]
33-
assert_equal [], results[:messages]
46+
assert_equal :approve_as_optimal, results[:status]
47+
assert_equal [], results[:comments]
3448
end
3549

3650
def test_simple_module_with_bookkeeping_passes
@@ -47,8 +61,8 @@ module Bookkeeping
4761
end
4862
}
4963
results = TwoFer::Analyze.(source)
50-
assert results[:approve]
51-
assert_equal [], results[:messages]
64+
assert_equal :approve_as_optimal, results[:status]
65+
assert_equal [], results[:comments]
5266
end
5367

5468
def test_different_module_name_fails
@@ -61,8 +75,8 @@ def self.two_fer(name="you")
6175
end
6276
}
6377
results = TwoFer::Analyze.(source)
64-
refute results[:approve]
65-
assert_equal ["No module or class called TwoFer"], results[:messages]
78+
assert_equal :disapprove_with_comment, results[:status]
79+
assert_equal ["No module or class called TwoFer"], results[:comments]
6680
end
6781

6882
# ###
@@ -79,8 +93,8 @@ def self.foobar(name="you")
7993
end
8094
}
8195
results = TwoFer::Analyze.(source)
82-
refute results[:approve]
83-
assert_equal ["No method called two_fer"], results[:messages]
96+
assert_equal :disapprove_with_comment, results[:status]
97+
assert_equal ["No method called two_fer"], results[:comments]
8498
end
8599

86100
def test_missing_param
@@ -93,8 +107,8 @@ def self.two_fer
93107
end
94108
}
95109
results = TwoFer::Analyze.(source)
96-
refute results[:approve]
97-
assert_equal ["There is no correct default param - the tests will fail"], results[:messages]
110+
assert_equal :disapprove_with_comment, results[:status]
111+
assert_equal ["There is no correct default param - the tests will fail"], results[:comments]
98112
end
99113

100114
def test_missing_default_value_fails
@@ -107,8 +121,8 @@ def self.two_fer(name)
107121
end
108122
}
109123
results = TwoFer::Analyze.(source)
110-
refute results[:approve]
111-
assert_equal ["There is no correct default param - the tests will fail"], results[:messages]
124+
assert_equal :disapprove_with_comment, results[:status]
125+
assert_equal ["There is no correct default param - the tests will fail"], results[:comments]
112126
end
113127

114128
def test_splat_fails
@@ -121,8 +135,8 @@ def self.two_fer(*foos)
121135
end
122136
}
123137
results = TwoFer::Analyze.(source)
124-
refute results[:approve]
125-
assert_equal ["Rather than using *foos, how about actually setting a parameter called 'name'?"], results[:messages]
138+
assert_equal :disapprove_with_comment, results[:status]
139+
assert_equal ["Rather than using *foos, how about actually setting a parameter called 'name'?"], results[:comments]
126140
end
127141

128142
# ### 
@@ -138,8 +152,8 @@ def self.two_fer(name="you")
138152
end
139153
}
140154
results = TwoFer::Analyze.(source)
141-
assert results[:approve]
142-
assert_equal ["Rather than using string building, use interpolation"], results[:messages]
155+
assert_equal :approve_with_comment, results[:status]
156+
assert_equal ["Rather than using string building, use interpolation"], results[:comments]
143157
end
144158

145159
def test_for_kernel_format
@@ -152,8 +166,8 @@ def self.two_fer(name="you")
152166
end
153167
}
154168
results = TwoFer::Analyze.(source)
155-
assert results[:approve]
156-
assert_equal ["Rather than using the format method, use interpolation"], results[:messages]
169+
assert_equal :approve_with_comment, results[:status]
170+
assert_equal ["Rather than using the format method, use interpolation"], results[:comments]
157171
end
158172

159173
def test_for_string_format
@@ -166,8 +180,8 @@ def self.two_fer(name="you")
166180
end
167181
}
168182
results = TwoFer::Analyze.(source)
169-
assert results[:approve]
170-
assert_equal ["Rather than using string's format/percentage method, use interpolation"], results[:messages]
183+
assert_equal :approve_with_comment, results[:status]
184+
assert_equal ["Rather than using string's format/percentage method, use interpolation"], results[:comments]
171185
end
172186

173187
def test_conditional_with_nil
@@ -184,8 +198,8 @@ def self.two_fer(name=nil)
184198
end
185199
}
186200
results = TwoFer::Analyze.(source)
187-
refute results[:approve]
188-
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:messages]
201+
assert_equal :disapprove_with_comment, results[:status]
202+
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:comments]
189203
end
190204

191205
def test_conditional_with_nil_reversed
@@ -202,8 +216,8 @@ def self.two_fer(name=nil)
202216
end
203217
}
204218
results = TwoFer::Analyze.(source)
205-
refute results[:approve]
206-
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:messages]
219+
assert_equal :disapprove_with_comment, results[:status]
220+
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:comments]
207221
end
208222

209223
def test_conditional_with_string
@@ -220,8 +234,8 @@ def self.two_fer(name='dog')
220234
end
221235
}
222236
results = TwoFer::Analyze.(source)
223-
refute results[:approve]
224-
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:messages]
237+
assert_equal :disapprove_with_comment, results[:status]
238+
assert_equal ["You could set the default value to 'you' to avoid conditionals"], results[:comments]
225239
end
226240

227241
def test_unknown_solution
@@ -234,9 +248,8 @@ def self.two_fer(name=nil)
234248
end
235249
}
236250
results = TwoFer::Analyze.(source)
237-
refute results[:approve]
238-
assert results[:refer_to_mentor]
239-
assert_equal [], results[:messages]
251+
assert_equal :refer_to_mentor, results[:status]
252+
assert_equal [], results[:comments]
240253
end
241254
end
242255

0 commit comments

Comments
 (0)