OSDN Git Service

add linear function graph spec
[mint/mint-lib.git] / spec / builder_spec.rb
1 # -*- coding: utf-8 -*-
2
3 require File.dirname(__FILE__) + '/spec_helper.rb'
4
5 module Mint
6   describe Builder do
7
8     subject { Builder }
9
10     describe 'build expression' do
11
12       before(:all) do
13         @original = '(x^-2 + 4)(y^3 --2)'
14         @normalized = '(x^-2 + 4) * (y^3 - -2)'
15         @latex = '(x^-2 + 4) \times (y^3 - -2)'
16       end
17
18       subject { Builder.build(@original) }
19
20       it 'original' do
21         subject.to_original.should == @original
22       end
23
24       it 'normalized' do
25         subject.normalize.should == @normalized
26       end
27
28       it 'to_s as normalized' do
29         subject.to_s.should == @normalized
30       end
31
32       it 'latex' do
33         subject.to_latex.should == @latex
34       end
35     end
36
37     describe 'validates expression' do
38
39       context 'valid expression' do
40         [
41           '1 + 2',
42           '2 + 5',
43           ' 6+ 5',
44           ' 2 *3 ',
45           '   2- 2 * 3',
46           'x - 2 * 3 +      8',
47           '2+3-1',
48         ].each do |expr|
49           it "without parenthesis #{expr}" do
50             expect { subject.build(expr) }.to_not raise_error
51           end
52         end
53
54         [
55           '2 * ( 3 + 5 )',
56           '2 * (( 3 + 5 ) - 2)',
57           '2 * (3 + 5)',
58           ' 2   * (3+5 )',
59           '(2 * ( 3 + 5 ) )',
60           '     ( 2   * 3   +5)',
61           '2 * (3) + 5',
62           '(x^3 + 2)(x^2- 3)(x + 5)',
63         ].each do |expr|
64           it "with parenthesis #{expr}" do
65             expect { subject.build(expr) }.to_not raise_error
66           end
67         end
68       end
69
70       context 'invalid expression' do
71         [
72           '1 +',
73           '+ 5',
74           '+ 5 *',
75           '2 * 3 +',
76           '/ 2 * 3',
77           '* 2 * 3 +',
78           '2 * + 3',
79         ].each do |expr|
80           it "without parenthesis #{expr}" do
81             expect { subject.build(expr) }.to raise_error
82           end
83         end
84
85         [
86           '2 * 3 + 5 )',
87           '2 * ( 3 + 5',
88           '2 * ( ( 3 + 5 )',
89           '2 * ( 3 + 5 ) )',
90           '( 2 * 3 + 5',
91           '( 2 * 3 +) 5',
92           '2 (* 3 + 5)',
93           '2 ()* 3 + 5)',
94           '2 () * 3 + 5',
95         ].each do |expr|
96           it "with parenthesis #{expr}" do
97             expect { subject.build(expr) }.to raise_error
98           end
99         end
100       end
101     end
102   end
103 end
104