anodator 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,355 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::OutputSpec
4
+ require "anodator/output_spec"
5
+
6
+ include Anodator
7
+
8
+ describe OutputSpec, ".new" do
9
+ context "with no paramerters" do
10
+ before(:each) do
11
+ @new_proc = lambda {
12
+ OutputSpec.new
13
+ }
14
+ end
15
+
16
+ it "should not raise error" do
17
+ @new_proc.should_not raise_error
18
+ end
19
+
20
+ context "after generated" do
21
+ before(:each) do
22
+ @output_spec = @new_proc.call
23
+ end
24
+
25
+ it "#items should be empty" do
26
+ @output_spec.items.should be_empty
27
+ end
28
+
29
+ it "#target should be OutputSpec::TARGET_ERROR by default" do
30
+ @output_spec.target.should == OutputSpec::TARGET_ERROR
31
+ end
32
+
33
+ it "#include_no_error should be false by default" do
34
+ @output_spec.include_no_error.should be_false
35
+ end
36
+ end
37
+ end
38
+
39
+ context "with several error items parameter" do
40
+ before(:each) do
41
+ @new_proc = lambda {
42
+ OutputSpec.new([
43
+ :target_numbers,
44
+ :target_names,
45
+ :target_values,
46
+ :error_message,
47
+ :error_level,
48
+ ])
49
+ }
50
+ end
51
+
52
+ it "should not raise error" do
53
+ @new_proc.should_not raise_error
54
+ end
55
+
56
+ context "after generated" do
57
+ before(:each) do
58
+ @output_spec = @new_proc.call
59
+ end
60
+
61
+ it "#items should not be empty" do
62
+ @output_spec.items.should_not be_empty
63
+ end
64
+
65
+ it "#target should be OutputSpec::TARGET_ERROR by default" do
66
+ @output_spec.target.should == OutputSpec::TARGET_ERROR
67
+ end
68
+
69
+ it "#include_no_error should be false by default" do
70
+ @output_spec.include_no_error.should be_false
71
+ end
72
+ end
73
+ end
74
+
75
+ context "with several input items parameter with error items" do
76
+ before(:each) do
77
+ @new_proc = lambda {
78
+ OutputSpec.new([
79
+ "1",
80
+ "2",
81
+ "3",
82
+ "name",
83
+ ])
84
+ }
85
+ end
86
+
87
+ it "should not raise error" do
88
+ @new_proc.should_not raise_error
89
+ end
90
+
91
+ context "after generated" do
92
+ before(:each) do
93
+ @output_spec = @new_proc.call
94
+ end
95
+
96
+ it "#items should not be empty" do
97
+ @output_spec.items.should_not be_empty
98
+ end
99
+
100
+ it "#target should be OutputSpec::TARGET_ERROR by default" do
101
+ @output_spec.target.should == OutputSpec::TARGET_ERROR
102
+ end
103
+
104
+ it "#include_no_error should be false by default" do
105
+ @output_spec.include_no_error.should be_false
106
+ end
107
+ end
108
+ end
109
+
110
+ context "with items and :target option" do
111
+ before(:each) do
112
+ @new_proc = lambda {
113
+ OutputSpec.new([
114
+ "1",
115
+ "2",
116
+ "3",
117
+ "name",
118
+ ],
119
+ :target => OutputSpec::TARGET_DATA)
120
+ }
121
+ end
122
+
123
+ it "should not raise error" do
124
+ @new_proc.should_not raise_error
125
+ end
126
+
127
+ context "after generated" do
128
+ before(:each) do
129
+ @output_spec = @new_proc.call
130
+ end
131
+
132
+ it "#items should not be empty" do
133
+ @output_spec.items.should_not be_empty
134
+ end
135
+
136
+ it "#target should be OutputSpec::TARGET_DATA" do
137
+ @output_spec.target.should == OutputSpec::TARGET_DATA
138
+ end
139
+
140
+ it "#include_no_error should be false by default" do
141
+ @output_spec.include_no_error.should be_false
142
+ end
143
+ end
144
+ end
145
+
146
+ context "with items and :target option and :include_no_error option" do
147
+ before(:each) do
148
+ @new_proc = lambda {
149
+ OutputSpec.new([
150
+ "1",
151
+ "2",
152
+ "3",
153
+ "name",
154
+ :target_numbers,
155
+ :error_message,
156
+ :error_level,
157
+ ],
158
+ :target => OutputSpec::TARGET_ERROR,
159
+ :include_no_error => true)
160
+ }
161
+ end
162
+
163
+ it "should not raise error" do
164
+ @new_proc.should_not raise_error
165
+ end
166
+
167
+ context "after generated" do
168
+ before(:each) do
169
+ @output_spec = @new_proc.call
170
+ end
171
+
172
+ it "#items should not be empty" do
173
+ @output_spec.items.should_not be_empty
174
+ end
175
+
176
+ it "#target should be OutputSpec::TARGET_ERROR" do
177
+ @output_spec.target.should == OutputSpec::TARGET_ERROR
178
+ end
179
+
180
+ it "#include_no_error should be true" do
181
+ @output_spec.include_no_error.should be_true
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ describe OutputSpec, "#generate" do
188
+ before(:each) do
189
+ # preparation
190
+ @input_spec = InputSpec.new([
191
+ { :number => "1", :name => "first name" },
192
+ { :number => "2", :name => "family name" },
193
+ { :number => "3", :name => "nickname" },
194
+ { :number => "4", :name => "sex" },
195
+ ])
196
+ @rule_set = RuleSet.new
197
+ @rule_set << Rule.new("1",
198
+ Message.new("[[1::name]] Cannot be blank"),
199
+ Validator::PresenceValidator.new("1"))
200
+ @rule_set << Rule.new("2",
201
+ Message.new("[[2::name]] Cannot be blank"),
202
+ Validator::PresenceValidator.new("2"))
203
+ @rule_set << Rule.new("3",
204
+ Message.new("[[3::name]] Cannot be blank"),
205
+ Validator::PresenceValidator.new("3"),
206
+ nil, Rule::LEVEL_WARNING)
207
+ @rule_set << Rule.new(["3", "4"],
208
+ Message.new("[[4::name]] Must be 'M' or 'F'"),
209
+ Validator::InclusionValidator.new("4",
210
+ :in => %W(M F)))
211
+ Validator::Base.values = @input_spec
212
+ end
213
+
214
+ context "when including errors" do
215
+ before(:each) do
216
+ @values = ["", "", "", ""]
217
+ @input_spec.source = @values
218
+ @rule_set.check_all
219
+ end
220
+
221
+ context "generate error list" do
222
+ before(:each) do
223
+ @output_spec = OutputSpec.new([
224
+ "1",
225
+ "2",
226
+ "nickname",
227
+ "4",
228
+ :target_numbers,
229
+ :target_names,
230
+ :error_message,
231
+ :error_level,
232
+ ],
233
+ :target => OutputSpec::TARGET_ERROR,
234
+ :include_no_error => true)
235
+ end
236
+
237
+ it "should generate error list datas" do
238
+ @output_spec.generate(@input_spec, @rule_set.results).should ==
239
+ [
240
+ ["", "", "", "", "1", "first name", "first name Cannot be blank", Rule::LEVEL_ERROR.to_s],
241
+ ["", "", "", "", "2", "family name", "family name Cannot be blank", Rule::LEVEL_ERROR.to_s],
242
+ ["", "", "", "", "3", "nickname", "nickname Cannot be blank", Rule::LEVEL_WARNING.to_s],
243
+ ["", "", "", "", "3 4", "nickname sex", "sex Must be 'M' or 'F'", Rule::LEVEL_ERROR.to_s],
244
+ ]
245
+ end
246
+ end
247
+
248
+ context "generate data list" do
249
+ before(:each) do
250
+ @output_spec = OutputSpec.new([
251
+ "1",
252
+ "2",
253
+ "nickname",
254
+ "4",
255
+ :target_numbers,
256
+ :target_names,
257
+ :error_message,
258
+ :error_level,
259
+ :error_count,
260
+ :warning_count,
261
+ :error_and_warning_count,
262
+ ],
263
+ :target => OutputSpec::TARGET_DATA,
264
+ :include_no_error => true)
265
+ end
266
+
267
+ it "should generate list data" do
268
+ @output_spec.generate(@input_spec, @rule_set.results).should ==
269
+ [
270
+ ["", "", "", "", "", "", "", "", "3", "1", "4"],
271
+ ]
272
+ end
273
+ end
274
+ end
275
+
276
+ context "when including no errors" do
277
+ before(:each) do
278
+ @values = ["Tetsuhisa", "MAKINO", "makitetsu", "M"]
279
+ @input_spec.source = @values
280
+ @rule_set.check_all
281
+ end
282
+
283
+ context "generate error list include no error" do
284
+ before(:each) do
285
+ @output_spec = OutputSpec.new([
286
+ "1",
287
+ "2",
288
+ "nickname",
289
+ "4",
290
+ :target_numbers,
291
+ :target_names,
292
+ :error_message,
293
+ :error_level,
294
+ ],
295
+ :target => OutputSpec::TARGET_ERROR,
296
+ :include_no_error => true)
297
+ end
298
+
299
+ it "should generate one data" do
300
+ @output_spec.generate(@input_spec, @rule_set.results).should ==
301
+ [
302
+ ["Tetsuhisa", "MAKINO", "makitetsu", "M", "", "", "", ""]
303
+ ]
304
+ end
305
+ end
306
+
307
+ context "generate error list not include no error" do
308
+ before(:each) do
309
+ @output_spec = OutputSpec.new([
310
+ "1",
311
+ "2",
312
+ "nickname",
313
+ "4",
314
+ :target_numbers,
315
+ :target_names,
316
+ :error_message,
317
+ :error_level,
318
+ ],
319
+ :target => OutputSpec::TARGET_ERROR,
320
+ :include_no_error => false)
321
+ end
322
+
323
+ it "should be empty" do
324
+ @output_spec.generate(@input_spec, @rule_set.results).should be_empty
325
+ end
326
+ end
327
+
328
+ context "generate data list" do
329
+ before(:each) do
330
+ @output_spec = OutputSpec.new([
331
+ "1",
332
+ "2",
333
+ "nickname",
334
+ "4",
335
+ :target_numbers,
336
+ :target_names,
337
+ :error_message,
338
+ :error_level,
339
+ :error_count,
340
+ :warning_count,
341
+ :error_and_warning_count,
342
+ ],
343
+ :target => OutputSpec::TARGET_DATA,
344
+ :include_no_error => true)
345
+ end
346
+
347
+ it "should not raise error" do
348
+ @output_spec.generate(@input_spec, @rule_set.results).should ==
349
+ [
350
+ ["Tetsuhisa", "MAKINO", "makitetsu", "M", "", "", "", "", "0", "0", "0"],
351
+ ]
352
+ end
353
+ end
354
+ end
355
+ end
@@ -0,0 +1,190 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::RuleSet
4
+ require "anodator/rule_set"
5
+
6
+ include Anodator
7
+
8
+ describe RuleSet, ".new" do
9
+ context "with no parameters" do
10
+ it "should not raise error" do
11
+ lambda {
12
+ RuleSet.new
13
+ }.should_not raise_error
14
+ end
15
+ end
16
+ end
17
+
18
+ describe RuleSet, "after generated" do
19
+ before(:each) do
20
+ # preparation
21
+ @input_spec = InputSpec.new([
22
+ { :number => "1", :name => "first name"},
23
+ { :number => "2", :name => "family name"},
24
+ { :number => "3", :name => "phone"},
25
+ { :number => "4", :name => "mail address"},
26
+ { :number => "5", :name => "sex"},
27
+ { :number => "6", :name => "age"},
28
+ ])
29
+ @records =
30
+ [
31
+ [ "Tetsuhisa" , "" , "0000-0000-0000" , "tim.makino@gmail.com" , "M" , "31" ],
32
+ [ "Takashi" , "Nakajima" , "0000-00-00-0001" , "nakajima@example@com" , "Male" , "28.4" ],
33
+ ]
34
+
35
+ ### Rules
36
+ @rule_set = RuleSet.new
37
+
38
+ ### set datas
39
+ Validator::Base.values = @input_spec
40
+ end
41
+
42
+ describe RuleSet, "#add_rule" do
43
+ before(:each) do
44
+ # first name
45
+ @rule_set.add_rule(Rule.new("1",
46
+ Message.new("[[1::name]] cannot be blank."),
47
+ Validator::PresenceValidator.new("1")))
48
+ # family name
49
+ @rule_set.add_rule(Rule.new("2",
50
+ Message.new("[[2::name]] cannot be blank."),
51
+ Validator::PresenceValidator.new("2")))
52
+ # phone
53
+ @rule_set.add_rule(Rule.new("phone",
54
+ Message.new("[[phone::name]] is invalid format."),
55
+ Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
56
+ Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING))
57
+ # mail address
58
+ @rule_set.add_rule(Rule.new("4",
59
+ Message.new("[[4:name]] is invalid format."),
60
+ Validator::FormatValidator.new("4", :format => /^[^@]+@[^@]+$/, :allow_blank => true)))
61
+ # sex
62
+ @rule_set.add_rule(Rule.new("5",
63
+ Message.new("[[5::name]] must be M/F/Man/Woman."),
64
+ Validator::InclusionValidator.new("5", :in => %W(M F Man Woman))))
65
+ # age
66
+ @rule_set.add_rule(Rule.new("age",
67
+ Message.new("[[age::name]] must be integer."),
68
+ Validator::NumericValidator.new("age", :only_integer => true)))
69
+ end
70
+
71
+ it "should have all rules" do
72
+ @rule_set.instance_eval("@rules.count").should == 6
73
+ end
74
+ end
75
+
76
+ describe RuleSet, "#<<" do
77
+ before(:each) do
78
+ # first name
79
+ @rule_set << Rule.new("1",
80
+ Message.new("[[1::name]] cannot be blank."),
81
+ Validator::PresenceValidator.new("1"))
82
+ # family name
83
+ @rule_set << Rule.new("2",
84
+ Message.new("[[2::name]] cannot be blank."),
85
+ Validator::PresenceValidator.new("2"))
86
+ # phone
87
+ @rule_set << Rule.new("phone",
88
+ Message.new("[[phone::name]] is invalid format."),
89
+ Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
90
+ Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
91
+ # mail address
92
+ @rule_set << Rule.new("4",
93
+ Message.new("[[4::name]] is invalid format."),
94
+ Validator::FormatValidator.new("4", :format => /^[^@]+@[^@]+$/, :allow_blank => true))
95
+ # sex
96
+ @rule_set << Rule.new("5",
97
+ Message.new("[[5::name]] must be M/F/Man/Woman."),
98
+ Validator::InclusionValidator.new("5", :in => %W(M F Man Woman)))
99
+ # age
100
+ @rule_set << Rule.new("age",
101
+ Message.new("[[age::name]] must be integer."),
102
+ Validator::NumericValidator.new("age", :only_integer => true))
103
+ end
104
+
105
+ it "should have all rules" do
106
+ @rule_set.instance_eval("@rules.count").should == 6
107
+ end
108
+ end
109
+
110
+ describe RuleSet, "#check_all" do
111
+ before(:each) do
112
+ # first name
113
+ @rule_set << Rule.new("1",
114
+ Message.new("[[1::name]] cannot be blank."),
115
+ Validator::PresenceValidator.new("1"))
116
+ # family name
117
+ @rule_set << Rule.new("2",
118
+ Message.new("[[2::name]] cannot be blank."),
119
+ Validator::PresenceValidator.new("2"))
120
+ # phone
121
+ @rule_set << Rule.new("phone",
122
+ Message.new("[[phone::name]] is invalid format."),
123
+ Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
124
+ Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
125
+ # mail address
126
+ @rule_set << Rule.new("4",
127
+ Message.new("[[4::name]] is invalid format."),
128
+ Validator::FormatValidator.new("4", :format => /^[^@]+@[^@]+$/, :allow_blank => true))
129
+ # sex
130
+ @rule_set << Rule.new("5",
131
+ Message.new("[[5::name]] must be M/F/Man/Woman."),
132
+ Validator::InclusionValidator.new("5", :in => %W(M F Man Woman)))
133
+ # age
134
+ @rule_set << Rule.new("age",
135
+ Message.new("[[age::name]] must be integer."),
136
+ Validator::NumericValidator.new("age", :only_integer => true))
137
+
138
+ @proc = lambda {
139
+ @input_spec.source = @records.first
140
+ @rule_set.check_all
141
+ }
142
+ end
143
+
144
+ it "should not raise error" do
145
+ @proc.should_not raise_error
146
+ end
147
+
148
+ it "should have results" do
149
+ @proc.call
150
+ @rule_set.instance_eval("@results.count").should_not be_zero
151
+ end
152
+ end
153
+
154
+ describe RuleSet, "#results" do
155
+ before(:each) do
156
+ # first name
157
+ @rule_set << Rule.new("1",
158
+ Message.new("[[1::name]] cannot be blank."),
159
+ Validator::PresenceValidator.new("1"))
160
+ # family name
161
+ @rule_set << Rule.new("2",
162
+ Message.new("[[2::name]] cannot be blank."),
163
+ Validator::PresenceValidator.new("2"))
164
+ # phone
165
+ @rule_set << Rule.new("phone",
166
+ Message.new("[[phone::name]] is invalid format.([[phone::value]])"),
167
+ Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
168
+ Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
169
+ # mail address
170
+ @rule_set << Rule.new("4",
171
+ Message.new("[[4::name]] is invalid format.([[4::value]])"),
172
+ Validator::FormatValidator.new("4", :format => /^[^@]+@[^@]+$/, :allow_blank => true))
173
+ # sex
174
+ @rule_set << Rule.new("5",
175
+ Message.new("[[5::name]] must be M/F/Man/Woman.([[5::value]])"),
176
+ Validator::InclusionValidator.new("5", :in => %W(M F Man Woman)))
177
+ # age
178
+ @rule_set << Rule.new("age",
179
+ Message.new("[[age::name]] must be integer.([[age::value]])"),
180
+ Validator::NumericValidator.new("age", :only_integer => true))
181
+
182
+ @input_spec.source = @records.last
183
+ @rule_set.check_all
184
+ end
185
+
186
+ it "should have results count 4" do
187
+ @rule_set.results.count.should == 4
188
+ end
189
+ end
190
+ end