anodator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +28 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.ja.rdoc +33 -0
  7. data/README.rdoc +41 -0
  8. data/Rakefile +50 -0
  9. data/VERSION +1 -0
  10. data/example/example_01.rb +129 -0
  11. data/lib/anodator/anodator_error.rb +5 -0
  12. data/lib/anodator/check_result.rb +41 -0
  13. data/lib/anodator/checker.rb +58 -0
  14. data/lib/anodator/input_spec.rb +199 -0
  15. data/lib/anodator/input_spec_item.rb +33 -0
  16. data/lib/anodator/message.rb +59 -0
  17. data/lib/anodator/output_spec.rb +164 -0
  18. data/lib/anodator/rule.rb +97 -0
  19. data/lib/anodator/rule_set.rb +52 -0
  20. data/lib/anodator/utils.rb +234 -0
  21. data/lib/anodator/validator/base.rb +168 -0
  22. data/lib/anodator/validator/blank_validator.rb +14 -0
  23. data/lib/anodator/validator/complex_validator.rb +60 -0
  24. data/lib/anodator/validator/configuration_error.rb +8 -0
  25. data/lib/anodator/validator/date_validator.rb +151 -0
  26. data/lib/anodator/validator/format_validator.rb +48 -0
  27. data/lib/anodator/validator/inclusion_validator.rb +21 -0
  28. data/lib/anodator/validator/length_validator.rb +37 -0
  29. data/lib/anodator/validator/numeric_validator.rb +46 -0
  30. data/lib/anodator/validator/presence_validator.rb +14 -0
  31. data/lib/anodator/validator.rb +10 -0
  32. data/lib/anodator.rb +3 -0
  33. data/spec/anodator/check_result_spec.rb +101 -0
  34. data/spec/anodator/checker_spec.rb +273 -0
  35. data/spec/anodator/input_spec_item_spec.rb +100 -0
  36. data/spec/anodator/input_spec_spec.rb +584 -0
  37. data/spec/anodator/message_spec.rb +112 -0
  38. data/spec/anodator/output_spec_spec.rb +355 -0
  39. data/spec/anodator/rule_set_spec.rb +190 -0
  40. data/spec/anodator/rule_spec.rb +169 -0
  41. data/spec/anodator/validator/base_spec.rb +214 -0
  42. data/spec/anodator/validator/blank_validator_spec.rb +52 -0
  43. data/spec/anodator/validator/complex_validator_spec.rb +268 -0
  44. data/spec/anodator/validator/date_validator_spec.rb +350 -0
  45. data/spec/anodator/validator/format_validator_spec.rb +158 -0
  46. data/spec/anodator/validator/inclusion_validator_spec.rb +77 -0
  47. data/spec/anodator/validator/length_validator_spec.rb +236 -0
  48. data/spec/anodator/validator/numeric_validator_spec.rb +468 -0
  49. data/spec/anodator/validator/presence_validator_spec.rb +52 -0
  50. data/spec/anodator/validator_spec.rb +16 -0
  51. data/spec/anodator_spec.rb +2 -0
  52. data/spec/spec_helper.rb +12 -0
  53. metadata +188 -0
@@ -0,0 +1,169 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::Rule
4
+ require "anodator/rule"
5
+
6
+ include Anodator
7
+
8
+ describe Rule, "#new" do
9
+ context "with no parameters" do
10
+ it "should raise ArgumentError" do
11
+ lambda {
12
+ Rule.new
13
+ }.should raise_error ArgumentError
14
+ end
15
+ end
16
+
17
+ context "with only target_expressions" do
18
+ it "should raise ArgumentError" do
19
+ lambda {
20
+ Rule.new(["1", "2", "3"])
21
+ }.should raise_error ArgumentError
22
+ end
23
+ end
24
+
25
+ context "with target_expressions and message" do
26
+ it "should raise ArgumentError" do
27
+ lambda {
28
+ Rule.new(["1", "2", "3"],
29
+ Message.new("'[[1::name]]' cannot be blank"))
30
+ }.should raise_error ArgumentError
31
+ end
32
+ end
33
+
34
+ context "with target_expressions, message and validator" do
35
+ before(:each) do
36
+ @new_proc = lambda {
37
+ v1 = Validator::PresenceValidator.new("1")
38
+ v2 = Validator::PresenceValidator.new("2")
39
+ @validator = Validator::ComplexValidator.new(:validators => [v1, v2])
40
+ @message = Message.new("'[[1::name]]' and '[[2::name]]' cannot be blank")
41
+ Rule.new(["1", "2"], @message, @validator)
42
+ }
43
+ end
44
+
45
+ it "should not raise Error" do
46
+ @new_proc.should_not raise_error
47
+ end
48
+
49
+ context "after generated" do
50
+ before(:each) do
51
+ @rule = @new_proc.call
52
+ end
53
+
54
+ it "#target_expressions should equal by initialize" do
55
+ @rule.target_expressions.should == ["1", "2"]
56
+ end
57
+
58
+ it "#message should equal by initialize" do
59
+ @rule.message.should == @message
60
+ end
61
+
62
+ it "#validator should equal by initialize" do
63
+ @rule.validator.should == @validator
64
+ end
65
+
66
+ it "#prerequisite should be nil" do
67
+ @rule.prerequisite.should be_nil
68
+ end
69
+
70
+ it "#level should equal by default(LEVEL_ERROR)" do
71
+ @rule.level.should == Rule::LEVEL_ERROR
72
+ end
73
+ end
74
+ end
75
+
76
+ context "with all parameters" do
77
+ before(:each) do
78
+ @new_proc = lambda {
79
+ v1 = Validator::PresenceValidator.new("1")
80
+ v2 = Validator::PresenceValidator.new("2")
81
+ @message = Message.new("'[[1::name]]' and '[[2::name]]' cannot be blank")
82
+ @validator = Validator::ComplexValidator.new(:validators => [v1, v2])
83
+ @prerequisite = Validator::BlankValidator.new("3")
84
+ Rule.new(["1", "2"], @message, @validator, @prerequisite, Rule::LEVEL_WARNING)
85
+ }
86
+ end
87
+
88
+ it "should not raise Error" do
89
+ @new_proc.should_not raise_error
90
+ end
91
+
92
+ context "after generated" do
93
+ before(:each) do
94
+ @rule = @new_proc.call
95
+ end
96
+
97
+ it "#target_expressions should equal by initialize" do
98
+ @rule.target_expressions.should == ["1", "2"]
99
+ end
100
+
101
+ it "#message should equal by initialize" do
102
+ @rule.message.should == @message
103
+ end
104
+
105
+ it "#validator should equal by initialize" do
106
+ @rule.validator.should == @validator
107
+ end
108
+
109
+ it "#prerequisite should equal by initialize" do
110
+ @rule.prerequisite.should == @prerequisite
111
+ end
112
+
113
+ it "#level should equal by initialize" do
114
+ @rule.level.should == Rule::LEVEL_WARNING
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ describe Rule, "#check" do
121
+ before(:each) do
122
+ @input_spec = InputSpec.new([
123
+ { :number => "1", :name => "item_1" },
124
+ { :number => "2", :name => "item_2" },
125
+ { :number => "3", :name => "item_3" },
126
+ ])
127
+ v1 = Validator::PresenceValidator.new("1")
128
+ v2 = Validator::PresenceValidator.new("2")
129
+ @message = Message.new("'[[1::name]]' and '[[2::name]]' cannot be blank")
130
+ @validator = Validator::ComplexValidator.new(:validators => [v1, v2], :logic => Validator::ComplexValidator::LOGIC_AND)
131
+ @prerequisite = Validator::BlankValidator.new("3")
132
+ @rule = Rule.new(["item_1", "2"], @message, @validator, @prerequisite)
133
+ end
134
+
135
+ context "when check rule is valid" do
136
+ before(:each) do
137
+ @input_spec.source = ["1", "2", ""]
138
+ Validator::Base.values = @input_spec
139
+ end
140
+
141
+ it { @rule.check.should be_nil }
142
+ end
143
+
144
+ context "when check rule is not valid" do
145
+ before(:each) do
146
+ @input_spec.source = ["1", "", ""]
147
+ Validator::Base.values = @input_spec
148
+ end
149
+
150
+ it { @rule.check.should_not be_nil }
151
+ it { @rule.check.should be_a CheckResult }
152
+
153
+ it "CheckResult#target_numbers must be only number expression" do
154
+ result = @rule.check
155
+ result.target_numbers.each do |number|
156
+ @input_spec.spec_item_by_expression(number).number.should == number
157
+ end
158
+ end
159
+ end
160
+
161
+ context "when prerequisite not matched" do
162
+ before(:each) do
163
+ @input_spec.source = ["1", "", "3"]
164
+ Validator::Base.values = @input_spec
165
+ end
166
+
167
+ it { @rule.check.should be_nil }
168
+ end
169
+ end
@@ -0,0 +1,214 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::Validator::Base
4
+ require "anodator/validator/base"
5
+
6
+ describe Anodator::Validator::Base, "#new" do
7
+ context "with no parameters" do
8
+ it "should raise ArgumentError" do
9
+ lambda {
10
+ Anodator::Validator::Base.new
11
+ }.should raise_error ArgumentError
12
+ end
13
+ end
14
+
15
+ context "with only target(nil) parameter" do
16
+ it "should raise ArgumentError" do
17
+ lambda {
18
+ Anodator::Validator::Base.new(nil)
19
+ }.should raise_error ArgumentError
20
+ end
21
+ end
22
+
23
+ context "with only target("") parameter" do
24
+ it "should raise ArgumentError" do
25
+ lambda {
26
+ Anodator::Validator::Base.new("")
27
+ }.should raise_error ArgumentError
28
+ end
29
+ end
30
+
31
+ context "with only target(34) parameter" do
32
+ before(:each) do
33
+ @new_proc = lambda {
34
+ Anodator::Validator::Base.new("34")
35
+ }
36
+ end
37
+
38
+ it "should not raise ArgumentError" do
39
+ @new_proc.should_not raise_error ArgumentError
40
+ end
41
+
42
+ it "#allow_blank? should be false" do
43
+ @base = @new_proc.call
44
+ @base.should_not be_allow_blank
45
+ end
46
+ end
47
+
48
+ context "with target(name) parameter" do
49
+ context "and allow_blank(true) parameter" do
50
+ before(:each) do
51
+ @new_proc = lambda {
52
+ Anodator::Validator::Base.new("name", :allow_blank => true)
53
+ }
54
+ end
55
+
56
+ it "should not raise ArgumentError" do
57
+ @new_proc.should_not raise_error ArgumentError
58
+ end
59
+
60
+ it "#allow_blank? should be true" do
61
+ @base = @new_proc.call
62
+ @base.should be_allow_blank
63
+ end
64
+ end
65
+
66
+ context "and allow_blank(false) parameter" do
67
+ before(:each) do
68
+ @new_proc = lambda {
69
+ Anodator::Validator::Base.new("name", :allow_blank => false)
70
+ }
71
+ end
72
+
73
+ it "should not raise ArgumentError" do
74
+ @new_proc.should_not raise_error ArgumentError
75
+ end
76
+
77
+ it "#allow_blank? should be false" do
78
+ @base = @new_proc.call
79
+ @base.should_not be_allow_blank
80
+ end
81
+ end
82
+
83
+ context "and description parameter" do
84
+ before(:each) do
85
+ @new_proc = lambda {
86
+ Anodator::Validator::Base.new("name", :description => "description message here.")
87
+ }
88
+ end
89
+
90
+ it "should not raise ArgumentError" do
91
+ @new_proc.should_not raise_error ArgumentError
92
+ end
93
+
94
+ it "#description should be 'description message here.'" do
95
+ @base = @new_proc.call
96
+ @base.description.should == "description message here."
97
+ end
98
+ end
99
+
100
+ context "and unknown parameter" do
101
+ before(:each) do
102
+ @new_proc = lambda {
103
+ Anodator::Validator::Base.new("name", :unknown => "value")
104
+ }
105
+ end
106
+
107
+ it "should raise ArgumentError" do
108
+ @new_proc.should raise_error ArgumentError
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ describe Anodator::Validator::Base, "#validate" do
115
+ it "Anodator::Validator::Base#validate raise NotImplementedError" do
116
+ lambda {
117
+ Anodator::Validator::Base.new("1").validate
118
+ }.should raise_error NoMethodError
119
+ end
120
+ end
121
+
122
+ describe Anodator::Validator::Base, "create a new Validator class to inherit" do
123
+ before(:all) do
124
+ module Anodator
125
+ module Validator
126
+ class SomeValidator < Base
127
+ valid_option_keys :some_option
128
+ default_options :some_option => true
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ it "should have additional option" do
135
+ Anodator::Validator::SomeValidator.valid_option_keys.
136
+ should be_include(:some_option)
137
+ end
138
+
139
+ it "should have default option ':allow_blank'" do
140
+ Anodator::Validator::SomeValidator.valid_option_keys.
141
+ should be_include(:allow_blank)
142
+ end
143
+
144
+ it "should have default value is true for option 'some_option'" do
145
+ Anodator::Validator::SomeValidator.default_options[:some_option].
146
+ should be_true
147
+ end
148
+ end
149
+
150
+ describe Anodator::Validator::Base, "create a new Validator class with unknown defualt options" do
151
+ before(:all) do
152
+ @new_class_proc = lambda {
153
+ module Anodator
154
+ module Validator
155
+ class InvalidDefaultOptionValidator < Base
156
+ valid_option_keys :some_option
157
+ default_options :wrong_option => 11
158
+ end
159
+ end
160
+ end
161
+ }
162
+ end
163
+
164
+ it "should raise error" do
165
+ @new_class_proc.should raise_error ArgumentError
166
+ end
167
+ end
168
+
169
+ describe Anodator::Validator::Base, "set values for validation" do
170
+ context "for nil value" do
171
+ it "should raise argument error" do
172
+ lambda {
173
+ Anodator::Validator::Base.values = nil
174
+ }.should raise_error ArgumentError
175
+ end
176
+ end
177
+
178
+ context "for String value" do
179
+ it "should not raise argument error" do
180
+ lambda {
181
+ Anodator::Validator::Base.values = "value"
182
+ }.should_not raise_error ArgumentError
183
+ end
184
+ end
185
+
186
+ context "for Array values" do
187
+ it "should not raise argument error" do
188
+ lambda {
189
+ Anodator::Validator::Base.values = [1, 2, 3]
190
+ }.should_not raise_error ArgumentError
191
+ end
192
+ end
193
+
194
+ context "for Hash values" do
195
+ it "should not raise argument error" do
196
+ lambda {
197
+ Anodator::Validator::Base.values = { "1" => 1, "2" => 2, "3" => 3 }
198
+ }.should_not raise_error ArgumentError
199
+ end
200
+ end
201
+
202
+ context "for another values what can respond to [] method" do
203
+ it "should not raise argument error" do
204
+ lambda {
205
+ class Dict
206
+ def []
207
+ return ""
208
+ end
209
+ end
210
+ Anodator::Validator::Base.values = Dict.new
211
+ }.should_not raise_error ArgumentError
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::Validator::BlankValidator
4
+ require "anodator/validator/blank_validator"
5
+
6
+ describe Anodator::Validator::BlankValidator, ".new" do
7
+ context "with no parameters" do
8
+ it "should raise ArgumentError" do
9
+ lambda {
10
+ Anodator::Validator::BlankValidator.new
11
+ }.should raise_error ArgumentError
12
+ end
13
+ end
14
+
15
+ context "with only target parameter" do
16
+ it "should not raise error" do
17
+ lambda {
18
+ Anodator::Validator::BlankValidator.new("1")
19
+ }.should_not raise_error
20
+ end
21
+ end
22
+ end
23
+
24
+ describe Anodator::Validator::BlankValidator, "#valid?" do
25
+ before(:each) do
26
+ @validator = Anodator::Validator::BlankValidator.new("1")
27
+ end
28
+
29
+ context "target value is blank" do
30
+ before(:each) do
31
+ Anodator::Validator::Base.values = { "1" => "" }
32
+ end
33
+
34
+ it { @validator.should be_valid }
35
+ end
36
+
37
+ context "target value is '1'" do
38
+ before(:each) do
39
+ Anodator::Validator::Base.values = { "1" => "1" }
40
+ end
41
+
42
+ it { @validator.should_not be_valid }
43
+ end
44
+
45
+ context "target value is 'some message'" do
46
+ before(:each) do
47
+ Anodator::Validator::Base.values = { "1" => "some message" }
48
+ end
49
+
50
+ it { @validator.should_not be_valid }
51
+ end
52
+ end
@@ -0,0 +1,268 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::Validator::ComplexValidator
4
+ require "anodator/validator/presence_validator"
5
+ require "anodator/validator/complex_validator"
6
+
7
+ include Anodator::Validator
8
+
9
+ describe ComplexValidator, ".new" do
10
+ context "with no paramerters" do
11
+ it "should not raise error" do
12
+ lambda {
13
+ ComplexValidator.new
14
+ }.should_not raise_error
15
+ end
16
+ end
17
+
18
+ context "with :validators only, without :logic" do
19
+ before(:each) do
20
+ @new_proc = lambda {
21
+ validators = []
22
+ validators << PresenceValidator.new("1")
23
+ validators << PresenceValidator.new("3")
24
+ ComplexValidator.new(:validators => validators)
25
+ }
26
+ end
27
+
28
+ it "should not raise error" do
29
+ @new_proc.should_not raise_error
30
+ end
31
+
32
+ it ":logic should be ComplexValidator::LOGIC_AND" do
33
+ complex_validator = @new_proc.call
34
+ complex_validator.options[:logic].should == ComplexValidator::LOGIC_AND
35
+ end
36
+ end
37
+
38
+ context "with :validators and :and_or option" do
39
+ before(:each) do
40
+ @new_proc = lambda {
41
+ validators = []
42
+ validators << PresenceValidator.new("1")
43
+ validators << PresenceValidator.new("3")
44
+ ComplexValidator.new(:validators => validators,
45
+ :logic => ComplexValidator::LOGIC_OR)
46
+ }
47
+ end
48
+
49
+ it "should not raise error" do
50
+ @new_proc.should_not raise_error
51
+ end
52
+
53
+ it ":logic should be ComplexValidator::LOGIC_OR" do
54
+ complex_validator = @new_proc.call
55
+ complex_validator.options[:logic].should == ComplexValidator::LOGIC_OR
56
+ end
57
+ end
58
+ end
59
+
60
+ describe ComplexValidator, "#valid?" do
61
+ context "no paramerters" do
62
+ before(:each) do
63
+ @validator = ComplexValidator.new
64
+ Base.values = {
65
+ "1" => "123",
66
+ "2" => "",
67
+ "3" => "message",
68
+ "5" => "value",
69
+ }
70
+ end
71
+
72
+ it "should raise error ConfigurationError" do
73
+ lambda {
74
+ @validator.valid?
75
+ }.should raise_error ConfigurationError
76
+ end
77
+ end
78
+
79
+ context "only :validators option" do
80
+ before(:each) do
81
+ @validator = ComplexValidator.new
82
+ validators = []
83
+ validators << PresenceValidator.new("1")
84
+ validators << PresenceValidator.new("3")
85
+ @validator = ComplexValidator.new(:validators => validators)
86
+ end
87
+
88
+ context "for values are valid" do
89
+ before(:each) do
90
+ Base.values = {
91
+ "1" => "1",
92
+ "2" => "",
93
+ "3" => "3",
94
+ }
95
+ end
96
+
97
+ it { @validator.should be_valid }
98
+ end
99
+
100
+ context "for values one valid, the other invalid" do
101
+ before(:each) do
102
+ Base.values = {
103
+ "1" => "1",
104
+ "2" => "",
105
+ "3" => "",
106
+ }
107
+ end
108
+
109
+ it { @validator.should_not be_valid }
110
+ end
111
+
112
+ context "for values are invalid at all values" do
113
+ before(:each) do
114
+ Base.values = {
115
+ "1" => "",
116
+ "2" => "3",
117
+ "3" => "",
118
+ }
119
+ end
120
+
121
+ it { @validator.should_not be_valid }
122
+ end
123
+ end
124
+
125
+ context ":validators and :logic OR option" do
126
+ before(:each) do
127
+ validators = []
128
+ validators << PresenceValidator.new("1")
129
+ validators << PresenceValidator.new("3")
130
+ @validator = ComplexValidator.new(:validators => validators,
131
+ :logic => ComplexValidator::LOGIC_OR)
132
+ end
133
+
134
+ context "for values are valid" do
135
+ before(:each) do
136
+ Base.values = {
137
+ "1" => "1",
138
+ "2" => "",
139
+ "3" => "3",
140
+ }
141
+ end
142
+
143
+ it { @validator.should be_valid }
144
+ end
145
+
146
+ context "for values one valid, the other invalid" do
147
+ before(:each) do
148
+ Base.values = {
149
+ "1" => "1",
150
+ "2" => "",
151
+ "3" => "",
152
+ }
153
+ end
154
+
155
+ it { @validator.should be_valid }
156
+ end
157
+
158
+ context "for values are invalid at all values" do
159
+ before(:each) do
160
+ Base.values = {
161
+ "1" => "",
162
+ "2" => "3",
163
+ "3" => "",
164
+ }
165
+ end
166
+
167
+ it { @validator.should_not be_valid }
168
+ end
169
+ end
170
+
171
+ context "for nested complex validator(A && (B || C))" do
172
+ before(:each) do
173
+ validators1, validators2 = [], []
174
+ validators2 << PresenceValidator.new("A")
175
+ validators1 << PresenceValidator.new("B")
176
+ validators1 << PresenceValidator.new("C")
177
+ validator1 = ComplexValidator.new(:validators => validators1,
178
+ :logic => ComplexValidator::LOGIC_OR)
179
+ validators2 << validator1
180
+ @validator = ComplexValidator.new(:validators => validators2,
181
+ :logic => ComplexValidator::LOGIC_AND)
182
+ end
183
+
184
+ context "for A(1), B(1), C(1)" do
185
+ before(:each) do
186
+ Base.values = {
187
+ "A" => "1",
188
+ "B" => "1",
189
+ "C" => "1",
190
+ }
191
+ end
192
+
193
+ it { @validator.should be_valid }
194
+ end
195
+
196
+ context "for A(), B(1), C(1)" do
197
+ before(:each) do
198
+ Base.values = {
199
+ "A" => "",
200
+ "B" => "1",
201
+ "C" => "1",
202
+ }
203
+ end
204
+
205
+ it { @validator.should_not be_valid }
206
+ end
207
+
208
+ context "for A(), B(), C(1)" do
209
+ before(:each) do
210
+ Base.values = {
211
+ "A" => "",
212
+ "B" => "",
213
+ "C" => "1",
214
+ }
215
+ end
216
+
217
+ it { @validator.should_not be_valid }
218
+ end
219
+
220
+ context "for A(1), B(), C()" do
221
+ before(:each) do
222
+ Base.values = {
223
+ "A" => "1",
224
+ "B" => "",
225
+ "C" => "",
226
+ }
227
+ end
228
+
229
+ it { @validator.should_not be_valid }
230
+ end
231
+
232
+ context "for A(1), B(), C(1)" do
233
+ before(:each) do
234
+ Base.values = {
235
+ "A" => "1",
236
+ "B" => "",
237
+ "C" => "1",
238
+ }
239
+ end
240
+
241
+ it { @validator.should be_valid }
242
+ end
243
+
244
+ context "for A(1), B(1), C()" do
245
+ before(:each) do
246
+ Base.values = {
247
+ "A" => "1",
248
+ "B" => "1",
249
+ "C" => "",
250
+ }
251
+ end
252
+
253
+ it { @validator.should be_valid }
254
+ end
255
+
256
+ context "for A(), B(), C()" do
257
+ before(:each) do
258
+ Base.values = {
259
+ "A" => "",
260
+ "B" => "",
261
+ "C" => "",
262
+ }
263
+ end
264
+
265
+ it { @validator.should_not be_valid }
266
+ end
267
+ end
268
+ end