Skip to content

Commit 42b64fa

Browse files
committed
Fix specs regarding code_breaker's lvasgn parsing
1 parent 4bd82c7 commit 42b64fa

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

2_Numbers/2_Basic_operators/solution_spec.rb

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,44 @@ def called_operations(lines)
1818
end
1919

2020
failure_message do |actual|
21-
%Q{Your code doesn't run the number operation "#{expected.join(' ')}".}
21+
expected_operants = expected.values.first
22+
variable_name = expected_operants[0]
23+
expected_operation = "#{variable_name} = #{expected_operants[1].join(' ')}"
24+
25+
actual_line = actual.split("\n").select do |output|
26+
output.match(/#{variable_name} =.+/)
27+
end
28+
29+
actual_parsed = CodeBreaker.parse(actual_line.join)
30+
actual_operants = actual_parsed.values.first
31+
actual_operation = "#{variable_name} = #{actual_operants[1].join(' ')}"
32+
33+
%Q{Your code doesn't run the number operation "#{expected_operation}".
34+
------- Instead you calculated "#{actual_operation}".}
2235
end
2336
end
2437

25-
OPERATIONS = {
26-
sum: [Rational, :+, Rational],
27-
difference: [Bignum, :-, Float],
28-
product: [Float, :*, Float, :*, Float],
29-
quotient: [Complex, :/, Float],
30-
power: [Rational, :**, Float]
31-
}.freeze
38+
# code_breaker outputs
39+
OPERATIONS = [
40+
{ lvasgn: [:sum, [Rational, :+, Rational]] },
41+
{ lvasgn: [:difference, [Bignum, :-, Float]] },
42+
{ lvasgn: [:product, [Float, :*, Float, :*, Float]] },
43+
{ lvasgn: [:quotient, [Complex, :/, Float]] },
44+
{ lvasgn: [:power, [Rational, :**, Float]] }
45+
].freeze
3246

3347
describe "Your code" do
3448
[['solution::code']]
3549

36-
OPERATIONS.each do |operation, values|
37-
it "defines a variable with name \"#{operation}\"" do
38-
expect(local_variables.include?(operation)).to be true
50+
OPERATIONS.each do |operation|
51+
variable_name = operation.first.last.first
52+
values = operation.values.first.last
53+
54+
it "defines a variable with name \"#{variable_name}\"" do
55+
expect(local_variables.include?(variable_name)).to be true
3956
end
4057

41-
if local_variables.include?(operation)
58+
if local_variables.include?(variable_name)
4259
operants = values.select.each_with_index { |_, i| i.even? }
4360
same_operants = (operants.uniq.count == 1)
4461

@@ -48,9 +65,9 @@ def called_operations(lines)
4865
operants.join(' and a ')
4966
end
5067

51-
it "calculates the #{operation} of #{numbers}" do
68+
it "calculates the #{variable_name} of #{numbers}" do
5269
code_lines = %Q{ [['solution::code']] }
53-
expect(code_lines).to run_number_operations(values)
70+
expect(code_lines).to run_number_operations(operation)
5471
end
5572
end
5673
end

0 commit comments

Comments
 (0)