From dba6ddc5440906a9e39e155d90fbfff02c39e473 Mon Sep 17 00:00:00 2001 From: Tomohiro Nishimura Date: Sat, 13 Mar 2010 02:28:29 +0900 Subject: [PATCH] refacotred spec for arithmetic_base generator --- spec/generator/arithmetic_base_spec.rb | 96 +------------------------ spec/spec_helper.rb | 127 +++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 94 deletions(-) diff --git a/spec/generator/arithmetic_base_spec.rb b/spec/generator/arithmetic_base_spec.rb index 99cf228..b0b97a2 100644 --- a/spec/generator/arithmetic_base_spec.rb +++ b/spec/generator/arithmetic_base_spec.rb @@ -5,101 +5,9 @@ require File.dirname(__FILE__) + '/../spec_helper.rb' module Mint::Generator describe Arithmetic do - - def set_options(options = {}) - defaults = {:term_number => 1, :operators => ['@']} - subject.__send__(:options=, defaults.merge(options)) - end - - before { set_options } - subject { Arithmetic.new } - - context 'generate problem' do - - before(:all) do - subject.class. - __send__(:define_method, :generate_problem) do - expression - end - end - - before { subject.should_receive(:expression) } - - it('do') { subject.generate } - - it 'can set options' do - options = { :a => 'a', :b => 'b' } - subject.generate(options) - subject.instance_variable_get(:@options). - should include(:a, :b) - end - end - - context 'create expression' do - - (1..10).each do |i| - - it "call x #{i}" do - set_options(:term_number => i) - subject.should_receive(:operand).exactly(i) - subject.should_receive(:last_operand).exactly(i) - subject.should_receive(:last_operator).exactly(i) - subject.__send__(:expression) - end - - it "@ x #{i-1}" do - set_options(:term_number => i) - generator = subject.dup - def generator.operand; 'operand' end - generator.__send__(:expression).scan('@').should have(i-1).operators - end - end - end - - context 'operand' do - - before(:all) do - @msg ='This operand need these options:' - @int = '1' - def subject.create_integer(*args); 1 end - end - - context 'integer' do - - it do - set_options(:min => nil, :max => nil, :minus => nil) - subject.__send__(:integer).should == @int - end - end - - context 'decimal' do - - it do - set_options( - :min => nil, - :max => nil, - :minus => nil, - :digits => 2) - subject.__send__(:decimal).should match(/(?:#{@int}|0)\.\d\d?/) - end - end - - context 'fraction' do - - it 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') - subject.__send__(:fraction).should == "a/b" - end - end - end + it_should_behave_like 'Arithmetic' + it { expect { subject.__send__(:operand) }.to raise_error } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4159591..5716174 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -57,6 +57,133 @@ shared_examples_for 'generator' do end end +shared_examples_for 'Arithmetic' do + + 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], + + ].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 + + context 'generate problem' do + + before(:all) do + subject.class. + __send__(:define_method, :generate_problem) do + expression + end + end + + before { subject.should_receive(:expression) } + + it('generate') { subject.generate } + it('generate_problem') { subject.__send__(:generate_problem) } + + it 'can set options' do + options = { :a => 'a', :b => 'b' } + subject.generate(options) + subject.instance_variable_get(:@options). + should include(:a, :b) + end + end + + context 'create expression' do + + (1..10).each do |i| + + it "call x #{i}" do + set_options(:term_number => i) + subject.should_receive(:operand).exactly(i) + subject.should_receive(:last_operand).exactly(i) + subject.should_receive(:last_operator).exactly(i) + subject.__send__(:expression) + end + + it "@ x #{i-1}" do + set_options(:term_number => i) + generator = subject.dup + def generator.operand; 'operand' end + generator.__send__(:expression).scan('@').should have(i-1).operators + end + end + end + + context 'operand' do + + before(:all) do + @msg ='This operand need these options:' + @int = '1' + subject.stub(:create_integer => 1) + end + + 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 + module ProblemExamples def self.get(*types) -- 2.11.0