|
4 | 4 |
|
5 | 5 | RSpec::Matchers.define :run_number_operations do |expected| |
6 | 6 | match do |actual| |
7 | | - CodeBreaker.parse(actual).include?(expected) |
| 7 | + actual_code = replace_explicit_numbers(actual) |
| 8 | + CodeBreaker.parse(actual_code).include?(expected) |
8 | 9 | end |
9 | 10 |
|
10 | 11 | failure_message do |actual| |
11 | | - expected_operants = expected.values.first |
12 | | - variable_name = expected_operants[0] |
| 12 | + expected_operants = expected.values.first |
| 13 | + variable_name = expected_operants[0] |
13 | 14 | expected_operation = "#{variable_name} = #{expected_operants[1].join(' ')}" |
14 | 15 |
|
15 | 16 | actual_line = actual.split("\n").select do |output| |
16 | | - output.match(/#{variable_name} =.+/) |
| 17 | + output.match(/#{variable_name}\s?=.+/) |
17 | 18 | end |
18 | 19 |
|
19 | | - actual_parsed = CodeBreaker.parse(actual_line.join) |
20 | | - actual_operants = actual_parsed.values.first |
21 | | - actual_operation = "#{variable_name} = #{actual_operants[1].join(' ')}" |
| 20 | + actual_code = replace_explicit_numbers(actual_line.join) |
| 21 | + actual_parsed = CodeBreaker.parse(actual_code) |
| 22 | + |
| 23 | + actual_operants = actual_parsed.values.first |
| 24 | + actual_operation = "#{variable_name} = #{Array(actual_operants[1]).join(' ')}" |
22 | 25 |
|
23 | 26 | %Q{Your code doesn't run the number operation "#{expected_operation}". |
24 | 27 | ------- Instead you calculated "#{actual_operation}".} |
25 | 28 | end |
| 29 | + |
| 30 | + def replace_explicit_numbers(actual) |
| 31 | + evaluate_match = lambda { |_| eval($1) } |
| 32 | + integer_or_float = /((Integer|Float)\(['"]?([-+]?\d*(_\d{1,})*(\.\d*)?)['"]?\))/ |
| 33 | + |
| 34 | + actual.gsub(integer_or_float, &evaluate_match) |
| 35 | + end |
26 | 36 | end |
27 | 37 |
|
28 | 38 | # code_breaker outputs |
|
39 | 49 |
|
40 | 50 | OPERATIONS.each do |operation| |
41 | 51 | variable_name = operation.first.last.first |
42 | | - values = operation.values.first.last |
| 52 | + values = operation.values.first.last |
43 | 53 |
|
44 | 54 | it "defines a variable with name \"#{variable_name}\"" do |
45 | 55 | expect(local_variables.include?(variable_name)).to be true |
46 | 56 | end |
47 | 57 |
|
48 | 58 | if local_variables.include?(variable_name) |
49 | | - operants = values.select.each_with_index { |_, i| i.even? } |
| 59 | + operants = values.select.each_with_index { |_, i| i.even? } |
50 | 60 | same_operants = (operants.uniq.count == 1) |
51 | 61 |
|
52 | 62 | numbers = if same_operants |
|
0 commit comments