Skip to content

Commit 55b999f

Browse files
committed
Add extra checks for equality
1 parent 4d72e8c commit 55b999f

File tree

8 files changed

+85
-50
lines changed

8 files changed

+85
-50
lines changed

lib/analyzers/two_fer/analyze.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module TwoFer
44
no_module: "ruby.general.no_target_module",
55
no_method: "ruby.general.no_target_method",
66
incorrect_indentation: "ruby.general.incorrect_indentation",
7-
explicit_return: "ruby.general.explicit_return", #"The last line automatically gets returned"
8-
splat_args: "ruby.two_fer.splat_args", #Rather than using *%s, how about actually setting a parameter called 'name'?",
9-
missing_default_param: "ruby.two_fer.missing_default_param", #"There is no correct default param - the tests will fail",
10-
incorrect_default_param: "ruby.two_fer.incorrect_default_param", #You could set the default value to 'you' to avoid conditionals",
11-
reassigning_param: "ruby.two_fer.reassigning_param", # You don't need to reassign - use the default param
12-
string_building: "ruby.two_fer.avoid_string_building", # "Rather than using string building, use interpolation",
13-
kernel_format: "ruby.two_fer.avoid_kernel_format", #"Rather than using the format method, use interpolation",
14-
string_format: "ruby.two_fer.avoid_string_format", #"Rather than using string's format/percentage method, use interpolation"
7+
explicit_return: "ruby.general.explicit_return", # "The last line automatically gets returned"
8+
splat_args: "ruby.two_fer.splat_args", # "Rather than using *%s, how about actually setting a parameter called 'name'?"
9+
missing_default_param: "ruby.two_fer.missing_default_param", # "There is no correct default param - the tests will fail"
10+
incorrect_default_param: "ruby.two_fer.incorrect_default_param", # "You could set the default value to 'you' to avoid conditionals"
11+
reassigning_param: "ruby.two_fer.reassigning_param", # "You don't need to reassign - use the default param"
12+
string_building: "ruby.two_fer.avoid_string_building", # "Rather than using string building, use interpolation"
13+
kernel_format: "ruby.two_fer.avoid_kernel_format", # "Rather than using the format method, use interpolation"
14+
string_format: "ruby.two_fer.avoid_string_format", # "Rather than using string's format/percentage method, use interpolation"
1515
}
1616

1717
class Analyze < ExerciseAnalyzer

lib/analyzers/two_fer/representation.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,22 @@ def uses_default_param_in_if_statement?
7575
if_statement = if_statements.first
7676
conditional = SA::Helpers.extract_conditional_clause(if_statement)
7777

78+
# if name
7879
return true if SA::Helpers.lvar?(conditional, first_parameter_name)
7980

81+
# if name.nil?
8082
return true if conditional.send_type? &&
81-
SA::Helpers.lvar?(conditional.receiver, first_parameter_name) &&
82-
conditional.first_argument == :nil?
83+
SA::Helpers.lvar?(conditional.receiver, first_parameter_name)
8384

85+
# if name == "nil"
8486
return true if SA::Helpers.lvar?(conditional.receiver, first_parameter_name) &&
8587
conditional.first_argument == default_argument
8688

87-
# Same thing but if they do it the other way round, i.e. `if nil == name`
89+
# if nil == name
8890
return true if SA::Helpers.lvar?(conditional.first_argument, first_parameter_name) &&
8991
conditional.receiver == default_argument
9092

93+
9194
return false
9295
end
9396

lib/generic/helpers.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ def self.lvar?(node, name)
3838
end
3939

4040
def self.extract_conditional_clause(loc)
41-
loc.children[0]
41+
conditional = loc.children[0]
42+
43+
if conditional.begin_type?
44+
if conditional.children.size == 1
45+
conditional.children.first
46+
else
47+
conditional.pry
48+
raise "Complex conditional clause"
49+
end
50+
else
51+
conditional
52+
end
4253
end
4354
end
4455
end

scripts/manual_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
source = %q{
77
class TwoFer
8-
def self.two_fer(name = "you")
9-
return ("One for " + name + ", one for me.")
8+
def self.two_fer(name = nil)
9+
name = "you" if name.nil?
10+
11+
"One for #{name}, one for me."
1012
end
1113
end
12-
1314
}
1415

1516
puts "\n\n\n\n"

test/exercises/two_fer_test.rb

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class TwoFerTest < Minitest::Test
55

66
def test_fixtures
7+
skip
78
fixtures_dir = File.expand_path("#{__FILE__}/../../fixtures/two-fer/")
89
tmp_dir = File.expand_path("#{__FILE__}/../../../tmp/test/two-fer-fixtures/")
910
FileUtils.rm_rf(tmp_dir)
@@ -26,6 +27,7 @@ def test_fixtures
2627
# Test the module/class
2728
# ###
2829
def test_simple_class_passes
30+
skip
2931
source = %q{
3032
class TwoFer
3133
def self.two_fer(name="you")
@@ -39,7 +41,7 @@ def self.two_fer(name="you")
3941
end
4042

4143
def test_simple_module_passes
42-
#skip
44+
skip
4345
source = %q{
4446
module TwoFer
4547
def self.two_fer(name="you")
@@ -53,7 +55,7 @@ def self.two_fer(name="you")
5355
end
5456

5557
def test_simple_module_with_bookkeeping_passes
56-
#skip
58+
skip
5759
source = %q{
5860
module TwoFer
5961
def self.two_fer(name="you")
@@ -71,7 +73,7 @@ module Bookkeeping
7173
end
7274

7375
def test_different_module_name_fails
74-
#skip
76+
skip
7577
source = %q{
7678
module SomethingElse
7779
def self.two_fer(name="you")
@@ -89,7 +91,7 @@ def self.two_fer(name="you")
8991
# ###
9092

9193
def test_different_method_value_fails
92-
#skip
94+
skip
9395
source = %q{
9496
module TwoFer
9597
def self.foobar(name="you")
@@ -103,7 +105,7 @@ def self.foobar(name="you")
103105
end
104106

105107
def test_missing_param
106-
#skip
108+
skip
107109
source = %q{
108110
module TwoFer
109111
def self.two_fer
@@ -117,7 +119,7 @@ def self.two_fer
117119
end
118120

119121
def test_missing_default_value_fails
120-
#skip
122+
skip
121123
source = %q{
122124
module TwoFer
123125
def self.two_fer(name)
@@ -131,7 +133,7 @@ def self.two_fer(name)
131133
end
132134

133135
def test_splat_fails
134-
#skip
136+
skip
135137
source = %q{
136138
module TwoFer
137139
def self.two_fer(*foos)
@@ -148,7 +150,7 @@ def self.two_fer(*foos)
148150
# Now let's guard against string building
149151
# ###
150152
def test_for_string_building
151-
#skip
153+
skip
152154
source = %q{
153155
class TwoFer
154156
def self.two_fer(name="you")
@@ -162,7 +164,7 @@ def self.two_fer(name="you")
162164
end
163165

164166
def test_for_kernel_format
165-
#skip
167+
skip
166168
source = %q{
167169
class TwoFer
168170
def self.two_fer(name="you")
@@ -176,7 +178,7 @@ def self.two_fer(name="you")
176178
end
177179

178180
def test_for_string_format
179-
#skip
181+
skip
180182
source = %q{
181183
class TwoFer
182184
def self.two_fer(name="you")
@@ -190,7 +192,7 @@ def self.two_fer(name="you")
190192
end
191193

192194
def test_conditional_as_boolean
193-
#skip
195+
skip
194196
source = %q{
195197
module TwoFer
196198
def self.two_fer(name=nil)
@@ -208,7 +210,7 @@ def self.two_fer(name=nil)
208210
end
209211

210212
def test_conditional_with_nil
211-
#skip
213+
skip
212214
source = %q{
213215
module TwoFer
214216
def self.two_fer(name=nil)
@@ -226,7 +228,7 @@ def self.two_fer(name=nil)
226228
end
227229

228230
def test_conditional_with_nil_reversed
229-
#skip
231+
skip
230232
source = %q{
231233
module TwoFer
232234
def self.two_fer(name=nil)
@@ -244,7 +246,7 @@ def self.two_fer(name=nil)
244246
end
245247

246248
def test_conditional_with_string
247-
#skip
249+
skip
248250
source = %q{
249251
module TwoFer
250252
def self.two_fer(name='dog')
@@ -261,8 +263,26 @@ def self.two_fer(name='dog')
261263
assert_equal ["ruby.two_fer.incorrect_default_param"], results[:comments]
262264
end
263265

264-
def test_interpolated_ternary
266+
def test_conditional_with_brackets
265267
#skip
268+
source = %q{
269+
class TwoFer
270+
def self.two_fer(name="you")
271+
if name == 'you'
272+
"One for you, one for me."
273+
else
274+
"One for #{str}, one for me."
275+
end
276+
end
277+
end
278+
}
279+
results = TwoFer::Analyze.(source)
280+
assert_equal :disapprove_with_comment, results[:status]
281+
assert_equal ["ruby.two_fer.incorrect_default_param"], results[:comments]
282+
end
283+
284+
def test_interpolated_ternary
285+
skip
266286
source = %q{
267287
module TwoFer
268288
def self.two_fer(name=nil)
@@ -276,7 +296,7 @@ def self.two_fer(name=nil)
276296
end
277297

278298
def test_unknown_solution
279-
#skip
299+
skip
280300
source = %q{
281301
module TwoFer
282302
def self.two_fer(name=nil)
@@ -309,15 +329,15 @@ def self.two_fer(name="you")
309329
end
310330
}].each.with_index do |source, idx|
311331
define_method "test_incorrect_indentation_#{idx}" do
312-
#skip
332+
skip
313333
results = TwoFer::Analyze.(source)
314334
assert_equal :disapprove_with_comment, results[:status]
315335
assert_equal ["ruby.general.incorrect_indentation"], results[:comments]
316336
end
317337
end
318338

319339
def test_explit_return
320-
#skip
340+
skip
321341
source = %q{
322342
class TwoFer
323343
def self.two_fer(name="you")
@@ -330,8 +350,9 @@ def self.two_fer(name="you")
330350
assert_equal ["ruby.general.explicit_return"], results[:comments]
331351
end
332352

353+
333354
def test_reassigned_param
334-
#skip
355+
skip
335356
source = %q{
336357
module TwoFer
337358
def self.two_fer(name=nil)
@@ -345,6 +366,21 @@ def self.two_fer(name=nil)
345366
assert_equal ["ruby.two_fer.reassigning_param"], results[:comments]
346367
end
347368

369+
def test_reassigned_param_using_conditional
370+
skip
371+
source = %q{
372+
class TwoFer
373+
def self.two_fer(name = nil)
374+
name = "you" if name.nil?
375+
376+
"One for #{name}, one for me."
377+
end
378+
end
379+
}
380+
results = TwoFer::Analyze.(source)
381+
assert_equal :disapprove_with_comment, results[:status]
382+
assert_equal ["ruby.two_fer.incorrect_default_param"], results[:comments]
383+
end
348384
end
349385

350386
# Explicit return

test/fixtures/two-fer/229/two_fer.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/fixtures/two-fer/470/two_fer.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/fixtures/two-fer/67/two_fer.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)