OSDN Git Service

dda8b6ba610f977334688649c67895a9a6c8d846
[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           '\frac{1}{(x + 4) \times (x - 2) \div 5}',
45           '1 / ((x + 4) * (x - 2) div 5)',
46           '\frac{1}{(x + 4) \times (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) \times 10.23',
59           '(-83.68) * 10.23',
60           '(-83.68) \times 10.23',
61         ],
62         [
63           'x^2 + 7x - 8',
64           'x^2 + 7 * x - 8',
65           'x^2 + 7x - 8',
66           'x^2 + 7 * x - 8',
67           'x^2 + 7x - 8',
68         ],
69         [
70           '3.4 / 54 div 2 / 6',
71           '3.4 / 54 / (2 / 6)',
72           '\frac{3.4}{54} \div \frac{2}{6}',
73           '3.4 / 54 div (2 / 6)',
74           '\frac{3.4}{54} \divide \frac{2}{6}',
75         ],
76         [
77           '5x * 2y',
78           '5 * x * 2 * y',
79           '5x \times 2y',
80           '5 * x * 2 * y',
81           '5x \times 2y',
82         ],
83         [
84           '5x^2 * 2y^3',
85           '5 * x^2 * 2 * y^3',
86           '5x^2 \times 2y^3',
87           '5 * x^2 * 2 * y^3',
88           '5x^2 \times 2y^3',
89         ],
90         [
91           '2root(2) * 35',
92           '2 * sqrt(2) * 35',
93           '2\sqrt{2} \times 35',
94           '2 * sqrt(2) * 35',
95           '2\sqrt{2} \times 35',
96         ],
97         [ '2i', '2 * %i', '2\i', '2 * %i', '2\i', ],
98         [ '2e', '2 * %e', '2\e', '2 * %e', '2\e', ],
99         [ '2pi', '2 * %pi', '2\pi', '2 * %pi', '2\pi', ],
100       ].each do |original, maxima, latex, normalize, ascii_math_ml|
101
102         context original do
103
104           before(:all) do
105             @original  = original
106             @maxima    = maxima
107             @latex     = latex
108             @normalize = normalize
109             @ascii_math_ml = ascii_math_ml
110           end
111
112           subject { MintExpressionParser.new.parse(@original) }
113
114           it 'original' do
115             subject.to_original.should == @original
116           end
117
118           it 'maxima' do
119             subject.to_maxima.should == @maxima
120           end
121
122           it 'latex' do
123             subject.to_latex.should == @latex
124           end
125
126           it 'normalize' do
127             subject.normalize.should == @normalize
128           end
129
130           it 'ascii_math_ml' do
131             subject.to_ascii_math_ml.should == @ascii_math_ml
132           end
133
134           it 'to_s as normalize' do
135             subject.to_s.should == @normalize
136           end
137         end
138       end
139     end
140   end
141
142   describe NullExpression do
143
144     subject { NullExpression.new }
145
146     before { @other = Expression.new('5', LiteralNode.new(5)) }
147
148     [
149       :to_original,
150       :to_latex,
151       :to_maxima,
152       :to_ascii_math_ml,
153       :normalize,
154     ].each do |meth|
155       it { subject.__send__(meth).should be_empty }
156     end
157
158     it { subject.==(@other).should be_false }
159     it { subject.inspect.should match(/#<Mint::Expression:\d+ ''>/) }
160   end
161 end
162