OSDN Git Service

refactored spec for high_order_expression_base
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 20:43:53 +0000 (05:43 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 12 Mar 2010 20:46:26 +0000 (05:46 +0900)
spec/generator/expansion_spec.rb
spec/generator/high_order_expression_base_spec.rb
spec/spec_helper.rb

index 8106d3d..390c9be 100644 (file)
@@ -8,6 +8,8 @@ module Mint::Generator
 
     subject{ Expansion.new }
 
+    it_should_behave_like 'HighOrderExpression'
+
     before(:all) do
       @defaults = {
         :x => ['x', 'y', 'z'],
index 923e145..83c89ed 100644 (file)
@@ -4,133 +4,9 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
 
 module Mint::Generator
   describe HighOrderExpression do
-
     before(:all) { subject.instance_variable_set(:@options, {}) }
-
     subject { HighOrderExpression.new }
-
-    context 'discriminant' do
-      [
-        [1, 2, 1, 0],
-        [1, 4, 1, 12],
-        [2, 2, 2, -12],
-
-      ].each do |a, b, c, d|
-        it { subject.__send__(:discriminant, a, b, c).should == d }
-      end
-    end
-
-    context 'factorization' do
-
-      (2..3).each do |i|
-        it do
-          subject.stub(:create_integer => i)
-          subject.should_receive(:"easy_factorization#{i}")
-          subject.__send__(:factorization)
-        end
-      end
-
-      it do
-        subject.stub(:create_integer => 0)
-        expect { 
-          subject.__send__(:factorization)
-        }.to raise_error(NotImplementedError)
-      end
-
-      context 'easy' do
-
-        before do
-          subject.instance_variable_set(:@options, :x => ['x'])
-          subject.stub(:create_integer).and_return(2, 3, 4)
-        end
-
-        it do
-          subject.should_receive(:factorization2).with(1, 5, 6, 'x')
-          subject.__send__(:easy_factorization2)
-        end
-
-        it do
-          subject.should_receive(:factorization3).with(1, 9, 26, 24, 'x')
-          subject.__send__(:easy_factorization3)
-        end
-
-        context 'factorization2' do
-
-          values = [[2, 3, 4], [1, 3, 4], [-1, 3, 4], [2, 1, 4], [2, -1, 4]]
-
-          values.zip([
-            '2x^2 + 3x + 4',
-            'x^2 + 3x + 4',
-            '-x^2 + 3x + 4',
-            '2x^2 + x + 4',
-            '2x^2 - x + 4',
-          ]).each do |(a, b, c), ptn|
-            it ptn do
-              expression = subject.__send__(:factorization2, a, b, c, 'x')
-              expression.should == ptn
-            end
-          end
-
-          values.zip([
-            '2x^2 + 3xy + 4y^2',
-            'x^2 + 3xy + 4y^2',
-            '-x^2 + 3xy + 4y^2',
-            '2x^2 + xy + 4y^2',
-            '2x^2 - xy + 4y^2',
-          ]).each do |(a, b, c), ptn|
-            it ptn do
-              expression = subject.__send__(:factorization2, a, b, c, ['x', 'y'])
-              expression.should == ptn
-            end
-          end
-        end
-
-        context 'factorization3' do
-
-          values = [[2, 3, 4, 5], [1, 3, 4, 5], [-1, 3, 4, 5], [2, 1, 4, 5], [2, -1, 4, 5]]
-
-          values.zip([
-            '2x^3 + 3x^2y + 4xy^2 + 5y^3',
-            'x^3 + 3x^2y + 4xy^2 + 5y^3',
-            '-x^3 + 3x^2y + 4xy^2 + 5y^3',
-            '2x^3 + x^2y + 4xy^2 + 5y^3',
-            '2x^3 - x^2y + 4xy^2 + 5y^3',
-          ]).each do |(a, b, c, d), ptn|
-
-            it ptn do
-              expression = subject.__send__(:factorization3, a, b, c, d, ['x', 'y'])
-              expression.should == ptn
-            end
-          end
-        end
-      end
-
-      context 'expansion' do
-
-        context 'one variable' do
-          before do
-            @single = '(2x + 2)'
-            subject.stub(:create_integer => 2)
-            subject.stub(:single => @single)
-            subject.instance_variable_set(:@options, :x => ['x'])
-          end
-          it do
-            expression = subject.__send__(:expansion)
-            expression.should == "#{@single}#{@single}"
-          end
-          it do
-            subject.should_receive(:single).and_return('x', @single)
-            expression = subject.__send__(:expansion)
-            expression.should == "#{@single}x"
-          end
-          it do
-            subject.should_receive(:single).and_return(@single, 'x')
-            expression = subject.__send__(:expansion)
-            expression.should == "#{@single}x"
-          end
-        end
-      end
-    end
+    it_should_behave_like 'HighOrderExpression'
   end
 end
 
index fd3cb2b..e114b3b 100644 (file)
@@ -60,86 +60,216 @@ end
 
 shared_examples_for 'Arithmetic' do
 
-    def set_options(options = {})
-      defaults = {:term_number => 1, :operators => ['@']}
-      subject.__send__(:options=, defaults.merge(options))
+  def set_options(options = {})
+    defaults = {:term_number => 1, :operators => ['@']}
+    subject.__send__(:options=, defaults.merge(options))
+  end
+
+  before { set_options }
+
+  it_should_behave_like 'generator'
+
+  [
+    [' -25', ' (-25)'],
+    ['-25', '-25'],
+    [' 25', ' 25'],
+    ['       -25', '       (-25)'],
+
+  ].each do |from, to|
+    it do
+      subject.__send__(:teardown, from).should == to
     end
+  end
+
+  [
+    [:factor, 5],
+    [:spec, 10],
+    [:hoge, 12],
+    [:zero, 0],
 
-    before { set_options }
+  ].each do |position, term_number|
+    context position do
+      before do
+        options = {
+          :"#{position}_term_min" => term_number,
+          :"#{position}_term_max" => term_number,
+        }
+        subject.stub(:options => options)
+      end
+      it { subject.__send__(:term_number, position).should == term_number }
+    end
+  end
 
-    it_should_behave_like 'generator'
+  context 'operand' do
 
-    [
-      [' -25', ' (-25)'],
-      ['-25', '-25'],
-      [' 25', ' 25'],
-      ['       -25', '       (-25)'],
+    before(:all) do
+      @msg ='This operand need these options:'
+      @int = '1'
+      subject.stub(:create_integer => 1)
+    end
 
-    ].each do |from, to|
-      it do
-        subject.__send__(:teardown, from).should == to
+    context 'integer' do
+      before { set_options(:min => nil, :max => nil, :minus => nil) }
+      it { subject.__send__(:integer).should == @int }
+    end
+
+    context 'decimal' do
+      before do
+        set_options(
+          :min    => nil,
+          :max    => nil,
+          :minus  => nil,
+          :digits => 2
+        )
+      end
+      it { subject.__send__(:decimal).should match(/(?:#{@int}|0)\.\d\d?/) }
+    end
+
+    context 'fraction' do
+      before do
+        set_options(
+          :numerator_min   => nil,
+          :numerator_max   => nil,
+          :denominator_min => nil,
+          :denominator_max => nil,
+          :minus           => nil
+        )
+        subject.should_receive(:numerator_part).and_return('a')
+        subject.should_receive(:denominator_part).and_return('b')
       end
+      it { subject.__send__(:fraction).should == "a/b" }
     end
+  end
+end
+
+shared_examples_for 'HighOrderExpression' do
 
+  it_should_behave_like 'generator'
+
+  before(:all) { subject.instance_variable_set(:@options, {}) }
+
+  context 'discriminant' do
     [
-      [:factor, 5],
-      [:spec, 10],
-      [:hoge, 12],
-      [:zero, 0],
+      [1, 2, 1, 0],
+      [1, 4, 1, 12],
+      [2, 2, 2, -12],
 
-    ].each do |position, term_number|
-      context position do
-        before do
-          options = {
-            :"#{position}_term_min" => term_number,
-            :"#{position}_term_max" => term_number,
-          }
-          subject.stub(:options => options)
-        end
-        it { subject.__send__(:term_number, position).should == term_number }
+    ].each do |a, b, c, d|
+      it { subject.__send__(:discriminant, a, b, c).should == d }
+    end
+  end
+
+  context 'factorization' do
+
+    (2..3).each do |i|
+      it do
+        subject.stub(:create_integer => i)
+        subject.should_receive(:"easy_factorization#{i}")
+        subject.__send__(:factorization)
       end
     end
 
-    context 'operand' do
+    it do
+      subject.stub(:create_integer => 0)
+      expect { 
+        subject.__send__(:factorization)
+      }.to raise_error(NotImplementedError)
+    end
 
-      before(:all) do
-        @msg ='This operand need these options:'
-        @int = '1'
-        subject.stub(:create_integer => 1)
+    context 'easy' do
+
+      before do
+        subject.instance_variable_set(:@options, :x => ['x'])
+        subject.stub(:create_integer).and_return(2, 3, 4)
       end
 
-      context 'integer' do
-        before { set_options(:min => nil, :max => nil, :minus => nil) }
-        it { subject.__send__(:integer).should == @int }
+      it do
+        subject.should_receive(:factorization2).with(1, 5, 6, 'x')
+        subject.__send__(:easy_factorization2)
       end
 
-      context 'decimal' do
-        before do
-          set_options(
-            :min    => nil,
-            :max    => nil,
-            :minus  => nil,
-            :digits => 2
-          )
+      it do
+        subject.should_receive(:factorization3).with(1, 9, 26, 24, 'x')
+        subject.__send__(:easy_factorization3)
+      end
+
+      context 'factorization2' do
+
+        values = [[2, 3, 4], [1, 3, 4], [-1, 3, 4], [2, 1, 4], [2, -1, 4]]
+
+        values.zip([
+          '2x^2 + 3x + 4',
+          'x^2 + 3x + 4',
+          '-x^2 + 3x + 4',
+          '2x^2 + x + 4',
+          '2x^2 - x + 4',
+        ]).each do |(a, b, c), ptn|
+          it ptn do
+            expression = subject.__send__(:factorization2, a, b, c, 'x')
+            expression.should == ptn
+          end
+        end
+
+        values.zip([
+          '2x^2 + 3xy + 4y^2',
+          'x^2 + 3xy + 4y^2',
+          '-x^2 + 3xy + 4y^2',
+          '2x^2 + xy + 4y^2',
+          '2x^2 - xy + 4y^2',
+        ]).each do |(a, b, c), ptn|
+          it ptn do
+            expression = subject.__send__(:factorization2, a, b, c, ['x', 'y'])
+            expression.should == ptn
+          end
         end
-        it { subject.__send__(:decimal).should match(/(?:#{@int}|0)\.\d\d?/) }
       end
 
-      context 'fraction' do
+      context 'factorization3' do
+
+        values = [[2, 3, 4, 5], [1, 3, 4, 5], [-1, 3, 4, 5], [2, 1, 4, 5], [2, -1, 4, 5]]
+
+        values.zip([
+          '2x^3 + 3x^2y + 4xy^2 + 5y^3',
+          'x^3 + 3x^2y + 4xy^2 + 5y^3',
+          '-x^3 + 3x^2y + 4xy^2 + 5y^3',
+          '2x^3 + x^2y + 4xy^2 + 5y^3',
+          '2x^3 - x^2y + 4xy^2 + 5y^3',
+        ]).each do |(a, b, c, d), ptn|
+
+          it ptn do
+            expression = subject.__send__(:factorization3, a, b, c, d, ['x', 'y'])
+            expression.should == ptn
+          end
+        end
+      end
+    end
+
+    context 'expansion' do
+
+      context 'one variable' do
         before do
-          set_options(
-            :numerator_min   => nil,
-            :numerator_max   => nil,
-            :denominator_min => nil,
-            :denominator_max => nil,
-            :minus           => nil
-          )
-          subject.should_receive(:numerator_part).and_return('a')
-          subject.should_receive(:denominator_part).and_return('b')
+          @single = '(2x + 2)'
+          subject.stub(:create_integer => 2)
+          subject.stub(:single => @single)
+          subject.instance_variable_set(:@options, :x => ['x'])
+        end
+        it do
+          expression = subject.__send__(:expansion)
+          expression.should == "#{@single}#{@single}"
+        end
+        it do
+          subject.should_receive(:single).and_return('x', @single)
+          expression = subject.__send__(:expansion)
+          expression.should == "#{@single}x"
+        end
+        it do
+          subject.should_receive(:single).and_return(@single, 'x')
+          expression = subject.__send__(:expansion)
+          expression.should == "#{@single}x"
         end
-        it { subject.__send__(:fraction).should == "a/b" }
       end
     end
+  end
 end
 
 module ProblemExamples