OSDN Git Service

fix multiple operator representation
[mint/mint-lib.git] / spec / expression_spec.rb
1 # -*- coding: utf-8 -*-
2
3 require File.dirname(__FILE__) + '/spec_helper.rb'
4
5 module Mint
6
7   describe Expression do
8
9     describe 'comparing' do
10       context 'equality' do
11         ProblemExamples.get(:original, :existing).each do |expression, other|
12           it expression do
13             expr  = Mint::Builder.build(expression)
14             othr = Mint::Builder.build(other)
15             expr.should == othr
16           end
17           it 'use maxima' do
18             expr  = Mint::Builder.build(expression)
19             othr = Mint::Builder.build(other)
20             othr_maxima = othr.to_maxima
21             othr.stub(:normalize => '')
22             othr.stub(:to_maxima => othr_maxima)
23             expr.should == othr
24           end
25         end
26         context 'null expression' do
27           ProblemExamples.get(:original, :existing).each do |expression, _|
28             context expression do
29               subject { Mint::Builder.build(expression) }
30               before { @null = Mint::Builder.build(nil) }
31               it { subject.should_not == @null }
32             end
33           end
34         end
35       end
36     end
37
38     describe 'formating' do
39
40       [
41         [
42           '1 / (x + 4)(x - 2) div 5',
43           '1 / ((x + 4) * (x - 2) / 5)',
44           '1 / ((x + 4) * (x - 2) div 5)',
45           '\frac{1}{(x + 4)(x - 2) \div 5}',
46           '\frac{1}{(x + 4)(x - 2) \divide 5}',
47         ],
48         [
49           '25.33 - (-0.99)',
50           '25.33 - (-0.99)',
51           '25.33 - (-0.99)',
52           '25.33 - (-0.99)',
53           '25.33 - (-0.99)',
54         ],
55         [
56           '(-83.68) * 10.23',
57           '(-83.68) * 10.23',
58           '(-83.68) * 10.23',
59           '(-83.68) \times 10.23',
60           '(-83.68) \times 10.23',
61         ],
62         [
63           'x^2 + 7x - 8',
64           'x^2 + 7 * x - 8',
65           'x^2 + 7 * x - 8',
66           'x^2 + 7x - 8',
67           'x^2 + 7x - 8',
68         ],
69         [
70           '3.4 / 54 div 2 / 6',
71           '3.4 / 54 / (2 / 6)',
72           '3.4 / 54 div (2 / 6)',
73           '\frac{3.4}{54} \div \frac{2}{6}',
74           '\frac{3.4}{54} \divide \frac{2}{6}',
75         ],
76         [
77           '5 * (-2)',
78           '5 * (-2)',
79           '5 * (-2)',
80           '5 \times (-2)',
81           '5 \times (-2)',
82         ],
83         [
84           '5 * (-2x)',
85           '5 * ((-2) * x)',
86           '5 * ((-2) * x)',
87           '5 \times ((-2)x)',
88           '5 \times ((-2)x)',
89         ],
90         [
91           '5x * 2y',
92           '5 * x * 2 * y',
93           '5 * x * 2 * y',
94           '5x \times 2y',
95           '5x \times 2y',
96         ],
97         [
98           '5x^2 * 2y^3',
99           '5 * x^2 * 2 * y^3',
100           '5 * x^2 * 2 * y^3',
101           '5x^2 \times 2y^3',
102           '5x^2 \times 2y^3',
103         ],
104         [
105           '2root(2) * 35',
106           '2 * sqrt(2) * 35',
107           '2 * sqrt(2) * 35',
108           '2\sqrt{2} \times 35',
109           '2\sqrt{2} \times 35',
110         ],
111         [ '2i', '2 * %i', '2 * %i', '2\i', '2\i', ],
112         [ '2e', '2 * %e', '2 * %e', '2\e', '2\e', ],
113         [ '2pi', '2 * %pi', '2 * %pi', '2\pi', '2\pi', ],
114       ].each do |original, maxima, normalize, latex, ascii_math_ml|
115
116         context original do
117
118           before(:all) do
119             @original  = original
120             @maxima    = maxima
121             @latex     = latex
122             @normalize = normalize
123             @ascii_math_ml = ascii_math_ml
124           end
125
126           subject { MintExpressionParser.new.parse(@original) }
127
128           it 'original' do
129             subject.to_original.should == @original
130           end
131
132           it 'maxima' do
133             subject.to_maxima.should == @maxima
134           end
135
136           it 'latex' do
137             subject.to_latex.should == @latex
138           end
139
140           it 'ascii_math_ml' do
141             subject.to_ascii_math_ml.should == @ascii_math_ml
142           end
143
144           it 'normalize' do
145             subject.normalize.should == @normalize
146           end
147
148           it 'to_s as normalize' do
149             subject.to_s.should == @normalize
150           end
151         end
152       end
153     end
154   end
155
156   describe NullExpression do
157
158     subject { NullExpression.new }
159
160     before { @other = Expression.new('5', LiteralNode.new(5)) }
161
162     [
163       :to_original,
164       :to_latex,
165       :to_maxima,
166       :to_ascii_math_ml,
167       :normalize,
168     ].each do |meth|
169       it { subject.__send__(meth).should be_empty }
170     end
171
172     it { subject.==(@other).should be_false }
173     it { subject.inspect.should match(/#<Mint::Expression:\d+ ''>/) }
174   end
175 end
176