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
class LiteralNode # :nodoc:
attr_accessor :value, :minus, :parenthesis
+ alias parenthesis? parenthesis
+ alias minus? minus
+
def initialize(value)
@value = value
@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
@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
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
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) }