|
1 | 1 | require 'rspec' |
| 2 | +require 'rspec/expectations' |
2 | 3 |
|
3 | | -describe "Your code" do |
4 | | - [['solution::code']] |
| 4 | +# requires code_breaker gem to be installed |
| 5 | +# (https://github.com/daigaku-ruby/code_breaker) |
| 6 | +require 'code_breaker' |
5 | 7 |
|
6 | | - sum = Rational(1, 2) + Rational(2, 4) |
7 | | - difference = 100_000_000_000_000 - 2500.25 |
8 | | - product = 1.578 + 2.7 + 0.42 |
9 | | - quotient = Complex(3.5, 5.25) / 8.75 |
10 | | - power = Rational(11/13) ** 11.13 |
| 8 | +RSpec::Matchers.define :run_number_operations do |expected| |
| 9 | + match do |actual| |
| 10 | + lines = actual.split("\n").compact.map(&:strip).reject(&:empty?) |
| 11 | + called_operations(lines).include?(expected) |
| 12 | + end |
11 | 13 |
|
| 14 | + def called_operations(lines) |
| 15 | + lines.map do |line| |
| 16 | + CodeBreaker.parse(line) |
| 17 | + end |
| 18 | + end |
12 | 19 |
|
13 | | - { |
14 | | - sum: [Rational, Rational, :+], |
15 | | - difference: [Bignum, Float, :-], |
16 | | - product: [Float, Float, Float, :*], |
17 | | - quotient: [Complex, Float, :/], |
18 | | - power: [Rational, Float, :**] |
19 | | - }.each do |operation, values| |
| 20 | + failure_message do |actual| |
| 21 | + %Q{Your code doesn't run the number operation "#{expected.join(' ')}".} |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 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 |
| 32 | + |
| 33 | +describe "Your code" do |
| 34 | + [['solution::code']] |
| 35 | + |
| 36 | + OPERATIONS.each do |operation, values| |
20 | 37 | it "defines a variable with name \"#{operation}\"" do |
21 | 38 | expect(local_variables.include?(operation)).to be true |
22 | 39 | end |
23 | 40 |
|
24 | 41 | if local_variables.include?(operation) |
25 | | - same_operants = (values[0..-2].uniq.count == 1) |
| 42 | + operants = values.select.each_with_index { |_, i| i.even? } |
| 43 | + same_operants = (operants.uniq.count == 1) |
26 | 44 |
|
27 | | - class_list = if same_operants |
28 | | - "#{values[0..-2].count} #{values.first} numbers" |
| 45 | + numbers = if same_operants |
| 46 | + "#{operants.count} #{values.first} numbers" |
29 | 47 | else |
30 | | - values[0..-2].join(' and a ') |
| 48 | + operants.join(' and a ') |
31 | 49 | end |
32 | 50 |
|
33 | | - it "calculates the #{operation} of #{class_list}" do |
34 | | - expect_any_instance_of(values.first) |
35 | | - .to receive(values.last).exactly(values.count - 2).times |
36 | | - |
37 | | - [['solution::code']] |
| 51 | + it "calculates the #{operation} of #{numbers}" do |
| 52 | + code_lines = %Q{ [['solution::code']] } |
| 53 | + expect(code_lines).to run_number_operations(values) |
38 | 54 | end |
39 | 55 | end |
40 | 56 | end |
|
0 commit comments