OSDN Git Service

refactored spec for complex_number_arithmetic generator
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 18:16:06 +0000 (03:16 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 18:16:06 +0000 (03:16 +0900)
spec/generator/complex_number_arithmetic_spec.rb
spec/generator/fractional_arithmetic_spec.rb

index 9823c26..191f593 100644 (file)
@@ -9,6 +9,8 @@ module Mint::Generator
 
     before(:all) { @opt_pattern = %r![+\-]! }
 
+    it_should_behave_like 'Arithmetic'
+
     context 'create complex number' do
 
       before(:all) { @defaults = { :min => 1 } }
index 6ce20ed..9c1ae37 100644 (file)
@@ -10,44 +10,42 @@ module Mint::Generator
 
     before(:all) { @opt_pattern = %r! [+\-*]|div ! }
 
-    before do
-      settings = {
-        :minus => true,
-        :numerator_min   => 1, :numerator_max   => 100,
-        :denominator_min => 2, :denominator_max => 100
-      }
-      @problems = subject.generate(settings)
-    end
-    it "size 1" do
-      @problems.should have(1).problem
-    end
-    it "have 2 operands" do
-      @problems.first.split(@opt_pattern).should have(2).operands
-    end
-    it "have a operator (+, -, *, /, div)" do
-      @problems.first.scan(@opt_pattern).should have(1).operator
+    it_should_behave_like 'Arithmetic'
+
+    it do
+      subject.should_receive(:fraction)
+      subject.__send__(:operand)
     end
-    it "have 2 fraction operands" do
-      @problems.first.split(@opt_pattern).each do |operand|
-        operand.should match(%r!/!)
+
+    context 'create expression' do
+      (1..100).to_a.shuffle[1, 10].each do |n|
+        context n do
+          before do
+            settings = {
+              :minus => true,
+              :numerator_min   => 1, :numerator_max   => 100,
+              :denominator_min => 2, :denominator_max => 100,
+              :term_number => n,
+            }
+            @problems = subject.generate(settings)
+          end
+          it { @problems.should have(1).problem }
+          it { @problems.first.split(@opt_pattern).should have(n).operands }
+          it { @problems.first.scan(@opt_pattern).should have(n-1).operator }
+          it { @problems.first.split(@opt_pattern).each {|operand| operand.should match(%r!/!) } }
+        end
       end
     end
 
     context 'need parenthesis' do
-
-      before do
-      end
-
-      it 'multiplication' do
-        settings = { :operators => ['*'], :minus => false }
-        problems = subject.generate(settings)
-        problems.first.split(/\*/)[1].should match(/\(.*\)/)
-      end
-
-      it 'division' do
-        settings = { :operators => ['div'], :minus => false }
-        problems = subject.generate(settings)
-        problems.first.split(/div/)[1].should match(/\(.*\)/)
+      %w[ * div ].each do |operator|
+        context operator do
+          before do
+            settings = { :operators => [operator], :minus => false }
+            @problems = subject.generate(settings)
+          end
+          it { @problems.first.split(operator)[1].should match(/\(.*\)/) }
+        end
       end
     end
   end