OSDN Git Service

suport (\d + sqrt(\d)) style square_root_arithmetic
authorTomohiro Nishimura <tomohiro68@gmail.com>
Wed, 24 Mar 2010 09:57:34 +0000 (18:57 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Wed, 24 Mar 2010 09:57:34 +0000 (18:57 +0900)
  * and remove validations (not work!)

lib/mint/generator/square_root_arithmetic.rb
spec/generator/square_root_arithmetic_spec.rb

index fa0111e..b928920 100644 (file)
@@ -28,13 +28,13 @@ module Mint::Generator
   #
   class SquareRootArithmetic < Arithmetic
 
-    root_pattern = /[2-9]?sqrt\((?:[2-9]|\d\d)\)/
-    term_pattern = /#{root_pattern} [+\-] #{root_pattern}/
-    validation /\A\(#{root_pattern}\)\^2\z/
-    validation /\A#{term_pattern}\z/
-    validation /\A\(#{term_pattern}\)(?:\^2)? [+\-] \(#{term_pattern}(?:\^2)?\)\z/
-    validation /\A\(#{term_pattern}\) [\*\/] \(#{term_pattern}\)\z/
-    validation /\A#{root_pattern} \* #{root_pattern}\z/
+    root_pattern = /[2-9]?sqrt\((?:[2-9]|\d\d)\)/
+    term_pattern = /#{root_pattern} [+\-] #{root_pattern}/
+    validation /\A\(#{root_pattern}\)\^2\z/
+    validation /\A#{term_pattern}\z/
+    validation /\A\(#{term_pattern}\)(?:\^2)? [+\-] \(#{term_pattern}(?:\^2)?\)\z/
+    validation /\A\(#{term_pattern}\) [\*\/] \(#{term_pattern}\)\z/
+    validation /\A#{root_pattern} \* #{root_pattern}\z/
 
     private
 
@@ -54,12 +54,11 @@ module Mint::Generator
     end
 
     def square_root_expression_parts
-      results = []
-      term_number(:single).times do
-        results << create_square_root_number
-        results << %w[ + - ].sample
-      end
-      results
+      term_number = term_number(:single)
+      results = term_number.times.map { create_square_root_number }
+      results.sort!
+      operators = term_number.times.map { %w[ + - ].sample }
+      results.zip(operators).flatten
     end
 
     def create_square_root_number
index 68e0811..4d02b43 100644 (file)
@@ -74,28 +74,48 @@ module Mint::Generator
 
       context 'some term' do
 
-        before(:all) do
-          @common_settings = {
-              :min => 2,
-              :single_term_min => 2,
-              :single_term_max => 2,
-          }
+        context 'min 2' do
+          before(:all) { @common_settings = { :min => 2, :single_term_min => 2, :single_term_max => 2, } }
+
+          term_pattern = /\(sqrt\(\d\d?\) [\-\+] sqrt\(\d\d?\)\)/
+          [
+            [{}, /\A#{term_pattern} [\*] #{term_pattern}\z/],
+            [{:operators => ['+']}, /\A#{term_pattern} \+ #{term_pattern}\z/],
+            [{:operators => ['-']}, /\A#{term_pattern} \- #{term_pattern}\z/],
+            [{:use_power => true}, /\A#{term_pattern}(?:\^2)? [\*] #{term_pattern}(?:\^2)?\z/],
+
+          ].each do |options, pattern|
+            context options.inspect do
+              before do
+                settings = @common_settings.merge(options)
+                @problem = subject.generate(settings)
+              end
+              it { @problem.first.should match(pattern) }
+            end
+          end
         end
 
-        term_pattern = /\(sqrt\(\d\d?\) [\-\+] sqrt\(\d\d?\)\)/
-        [
-          [{}, /\A#{term_pattern} [\*] #{term_pattern}\z/],
-          [{:operators => ['+']}, /\A#{term_pattern} \+ #{term_pattern}\z/],
-          [{:operators => ['-']}, /\A#{term_pattern} \- #{term_pattern}\z/],
-          [{:use_power => true}, /\A#{term_pattern}(?:\^2)? [\*] #{term_pattern}(?:\^2)?\z/],
-
-        ].each do |options, pattern|
-          context options.inspect do
-            before do
-              settings = @common_settings.merge(options)
-              @problem = subject.generate(settings)
+        context 'max 1' do
+          before(:all) do
+            @common_settings = {}
+            subject.stub(:create_integer).and_return(2, 5, 1, 2, 2, 1, 5, 2)
+          end
+
+          term_pattern = /\(\d\d? [\-\+] sqrt\(\d\d?\)\)/
+          [
+            [{}, /\A#{term_pattern} [\*] #{term_pattern}\z/],
+            [{:operators => ['+']}, /\A#{term_pattern} \+ #{term_pattern}\z/],
+            [{:operators => ['-']}, /\A#{term_pattern} \- #{term_pattern}\z/],
+            [{:use_power => true}, /\A#{term_pattern}(?:\^2)? [\*] #{term_pattern}(?:\^2)?\z/],
+
+          ].each do |options, pattern|
+            context options.inspect do
+              before do
+                settings = @common_settings.merge(options)
+                @problem = subject.generate(settings)
+              end
+              it { @problem.first.should match(pattern) }
             end
-            it { @problem.first.should match(pattern) }
           end
         end
       end