OSDN Git Service

452aa6b15a58006128f7619d39e9bd917687aa31
[mint/mint-server.git] / spec / models / complex_number_arithmetic_edit_form_spec.rb
1 require 'spec_helper'
2
3 describe ComplexNumberArithmeticEditForm do
4   before do
5     @valid_attributes = {
6       :unit_id         => 1,
7       :level           => 1,
8       :amount          => 2,
9       :min             => 0,
10       :max             => 100,
11       :fractional_mode => false,
12       :term_number     => 2,
13       :operators_plus  => true,
14       :operators_minus => true,
15       :operators_times => true,
16 #      :operators_div   => true,
17     }
18     @klass = ComplexNumberArithmeticEditForm
19     @basic = @klass.new(@valid_attributes)
20   end
21
22   context "basic" do
23     it{ @basic.should be_valid }
24   end
25
26   context "validation" do
27
28     context "min" do
29       context "is string" do
30         subject{ @basic.min = "a"; @basic }
31         it{ should_not be_valid }
32       end
33       context "is nil" do
34         subject{ @basic.min = nil; @basic }
35         it{ should_not be_valid }
36       end
37       context "is empty" do
38         subject{ @basic.min = ""; @basic }
39         it{ should_not be_valid }
40       end
41       context "is float" do
42         subject{ @basic.min = 1.5; @basic }
43         it{ should_not be_valid }
44       end
45       context "is -1" do
46         subject{ @basic.min = -1; @basic }
47         it{ should_not be_valid }
48       end
49       context "is 0" do
50         subject{ @basic.min = 0; @basic }
51         it{ should be_valid }
52       end
53     end
54
55     context "max" do
56       context "is string" do
57         subject{ @basic.max = "a"; @basic }
58         it{ should_not be_valid }
59       end
60       context "is nil" do
61         subject{ @basic.max = nil; @basic }
62         it{ should_not be_valid }
63       end
64       context "is empty" do
65         subject{ @basic.max = ""; @basic }
66         it{ should_not be_valid }
67       end
68       context "is float" do
69         subject{ @basic.max = 1.5; @basic }
70         it{ should_not be_valid }
71       end
72       context "is -1" do
73         subject{ @basic.max = -1; @basic }
74         it{ should_not be_valid }
75       end
76       context "is 0" do
77         subject{ @basic.max = 0; @basic }
78         it{ should be_valid }
79       end
80     end
81
82     context "relation" do
83       context "min < max" do
84         before do
85           @basic.min = 10
86           @basic.max = 1
87         end
88         it{ @basic.should_not be_valid }
89         it "have a correct message" do
90           @basic.valid?
91           @basic.errors.full_messages.first.should == "Min must be greater than Max."
92         end
93       end
94       context "min = max" do
95         subject{
96           @basic.min = 10
97           @basic.max = 10
98           @basic
99         }
100         it{ should be_valid }
101       end
102     end
103
104     context "operators" do
105       context "no operators specified" do
106         subject{
107           @basic.operators_plus = false
108           @basic.operators_minus = false
109           @basic.operators_times = false
110           # @basic.operators_div = false
111           @basic
112         }
113         it{ should_not be_valid }
114       end
115     end
116   end
117
118   context "options_hash" do
119     subject{ @basic.options_hash }
120     it{
121       should == {
122         :min => 0, :max => 100,
123         :fractional_mode => false,
124         :term_number => 2,
125         :operators => %w[+ - * /]
126       }
127     }
128   end
129
130   context "attributes_for_model" do
131     subject{ @basic.attributes_for_model }
132     it{
133       should == {
134         :unit_id => 1,
135         :level   => 1,
136         :amount  => 2,
137         :options => @basic.options_hash.to_json,
138         :lock_version => 0
139       }
140     }
141   end
142
143   context "set_options" do
144     context "basic" do
145       subject{
146         options = {
147           :min => 1, :max => 99,
148           :fractional_mode => true,
149           :term_number => 3,
150           :operators => %w[+ - * div]
151         }
152         @basic.set_options(options.to_json)
153         @basic
154       }
155       its(:min){ should == 1 }
156       its(:max){ should == 99 }
157       its(:fractional_mode){ should be_true }
158       its(:term_number){ should == 3 }
159       its(:operators_plus){ should be_true }
160       its(:operators_minus){ should be_true }
161       its(:operators_times){ should be_true }
162       its(:operators_div){ should be_true }
163     end
164     context "set nothing" do
165       it "should not raise Exception" do
166         lambda{ @basic.set_options({}.to_json) }.should_not raise_exception
167       end
168     end
169   end
170 end