OSDN Git Service

fix expression with minus bug
authorTomohiro Nishimura <tomohiro68@gmail.com>
Wed, 17 Mar 2010 09:13:07 +0000 (18:13 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Wed, 17 Mar 2010 09:13:07 +0000 (18:13 +0900)
lib/mint/builder/expression_tree.rb
spec/builder/expression_tree_spec.rb
spec/builder_spec.rb
spec/expression_spec.rb

index fd8681e..9cd604e 100644 (file)
@@ -5,6 +5,9 @@ module Mint
   class ExpressionNode # :nodoc:
     attr_reader :left, :right, :operator
     attr_accessor :minus, :parenthesis
+    alias parenthesis? parenthesis
+    alias minus? minus
+
     def initialize(left, right)
       @left     = left
       @right    = right
@@ -96,6 +99,9 @@ module Mint
 
   class LiteralNode # :nodoc:
     attr_accessor :value, :minus, :parenthesis
+    alias parenthesis? parenthesis
+    alias minus? minus
+
     def initialize(value)
       @value = value
 
@@ -121,7 +127,7 @@ module Mint
       @value.to_s
     end
     def show(string)
-      return "(-#{string})"   if minus
+      return "(-#{string})" if minus
       return "(#{string})"  if parenthesis
       return "(-#{string})" if minus && parenthesis
       string
@@ -151,11 +157,19 @@ module Mint
       @power = power
     end
     def to_s_local
-      "#{value}^#{power}"
+      _power = filter_minus(power) {|v| v.to_s }
+      "#{value}^#{_power}"
     end
     def to_latex_local
       return to_s if value.instance_of?(String)
-      "#{value.to_latex}^#{power.to_latex}"
+      _power = filter_minus(power) {|v| v.to_latex }
+      "#{value.to_latex}^#{_power}"
+    end
+    def filter_minus(value)
+      if value.instance_of?(LiteralNode) && value.minus?
+        return "-#{value.__send__(:to_s_local)}"
+      end
+      yield value
     end
   end
 
index fa94362..dfcf8f4 100644 (file)
@@ -107,6 +107,19 @@ module Mint
     it 'shows itself as string' do
       subject.to_s.should == "#{@left} #{@operator} #{@right}"
     end
+
+    context 'options' do
+      context 'minus' do
+        before { subject.minus = true }
+        it { subject.minus.should be_true }
+        it { subject.should be_minu }
+      end
+      context 'parenthesis' do
+        before { subject.parenthesis = true }
+        it { subject.parenthesis.should be_true }
+        it { subject.should be_parenthesis }
+      end
+    end
   end
 
   describe AdditionNode do
index d872a4f..e44cd59 100644 (file)
@@ -11,8 +11,8 @@ module Mint
 
       before(:all) do
         @original = '(x^-2 + 4)(y^3 --2)'
-        @normalized = '(x^-2 + 4) * (y^3 - -2)'
-        @latex = '(x^-2 + 4) \times (y^3 - -2)'
+        @normalized = '(x^-2 + 4) * (y^3 - (-2))'
+        @latex = '(x^-2 + 4) \times (y^3 - (-2))'
       end
 
       subject { Builder.build(@original) }
index ca5e7b6..64cd71e 100644 (file)
@@ -39,17 +39,17 @@ module Mint
         ],
         [
           '25.33 - (-0.99)',
-          '25.33 - -0.99',
-          '25.33 - -0.99',
-          '25.33 - -0.99',
-          '25.33 - -0.99',
+          '25.33 - (-0.99)',
+          '25.33 - (-0.99)',
+          '25.33 - (-0.99)',
+          '25.33 - (-0.99)',
         ],
         [
-          '-83.68 * 10.23',
-          '-83.68 * 10.23',
-          '-83.68 \times 10.23',
-          '-83.68 * 10.23',
-          '-83.68 \times 10.23',
+          '(-83.68) * 10.23',
+          '(-83.68) * 10.23',
+          '(-83.68) \times 10.23',
+          '(-83.68) * 10.23',
+          '(-83.68) \times 10.23',
         ],
         [
           'x^2 + 7x - 8',