OSDN Git Service

refactored spec for complex_fractional_arithmetic generator
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 19:32:02 +0000 (04:32 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 19:32:02 +0000 (04:32 +0900)
lib/mint/generator/complex_fractional_arithmetic.rb
spec/generator/complex_fractional_arithmetic_spec.rb

index d7daccc..3528b04 100644 (file)
@@ -46,11 +46,15 @@ module Mint::Generator
       result.join(' / ')
     end
 
+    def operand
+      fraction
+    end
+
     [:numerator, :denominator].each do |name|
       define_method(name) do
         result = []
         term_number(name).times do
-          result << fraction
+          result << operand
           result << options[:operators].sample
         end
         result[0..-2].join(' ')
index dad8516..cb7dd86 100644 (file)
@@ -8,30 +8,30 @@ module Mint::Generator
 
     subject{ ComplexFractionalArithmetic.new }
 
-    context 'each 2 terms' do
-      before do
-        settings = {
-          :numerator_term_min   => 2, :numerator_term_max   => 2,
-          :denominator_term_min => 2, :denominator_term_max => 2
-        }
-        @problems = subject.generate(settings)
-      end
-      it { @problems.should have(1).problem }
-      it { @problems.first.split(/ \/ /).should have(2).operands }
-      it { @problems.first.scan(%r| [\-\+\*] |).should have(2).operator }
+    before(:all) { @opt_pattern = %r| [\-\+\*] | }
+
+    it_should_behave_like 'Arithmetic'
+
+    it do
+      subject.should_receive(:fraction)
+      subject.__send__(:operand)
     end
 
-    context '1 term / 2 terms' do
-      before do
-        settings = {
-          :numerator_term_min   => 1, :numerator_term_max   => 1,
-          :denominator_term_min => 2, :denominator_term_max => 2
-        }
-        @problems = subject.generate(settings)
+    context 'create expressoin' do
+      ([1] + (2..100).to_a.shuffle[0, 9]).combination(2) do |i, j|
+        context "#{i}/#{j}" do
+          before do
+            settings = {
+              :numerator_term_min   => i, :numerator_term_max   => i,
+              :denominator_term_min => j, :denominator_term_max => j,
+            }
+            @problems = subject.generate(settings)
+          end
+          it { @problems.should have(1).problem }
+          it { @problems.first.split(/\//).should have((i + 1) + (j + 1)).operands }
+          it { @problems.first.scan(@opt_pattern).should have((i - 1) + (j - 1)).operator }
+        end
       end
-      it { @problems.should have(1).problem }
-      it { @problems.first.split(/ \/ /).should have(2).operands }
-      it { @problems.first.scan(%r| [\-\+\*] |).should have(1).operator }
     end
   end
 end