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,273 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::Checker
4
+ require "anodator/checker"
5
+
6
+ include Anodator
7
+ include Anodator::Validator
8
+
9
+ describe Checker, ".new" do
10
+ before(:each) do
11
+ @input_spec = InputSpec.new([
12
+ { :number => "0", :name => "ID" },
13
+ { :number => "1", :name => "First name" },
14
+ { :number => "2", :name => "Family name" },
15
+ { :number => "3", :name => "Birthday" },
16
+ { :number => "4", :name => "Sex" },
17
+ { :number => "5", :name => "Phone number" },
18
+ { :number => "6", :name => "ZIP" },
19
+ ])
20
+ @rule_set = RuleSet.new
21
+ # First name
22
+ validator =
23
+ ComplexValidator.new(:validators => [LengthValidator.new("1", :in => 3..25),
24
+ FormatValidator.new("1", :format => /^[a-z]+$/i)])
25
+ @rule_set << Rule.new("First name",
26
+ Message.new("[[1::name]] is invalid."),
27
+ validator)
28
+ # Family name
29
+ validator =
30
+ ComplexValidator.new(:validators => [LengthValidator.new("2", :in => 3..25),
31
+ FormatValidator.new("2", :format => /^[a-z]+$/i)])
32
+ @rule_set << Rule.new("Family name",
33
+ Message.new("[[2::name]] is invalid."),
34
+ validator)
35
+ # Birthday
36
+ @rule_set << Rule.new("Birthday",
37
+ Message.new("[[3::name]] is invalid."),
38
+ FormatValidator.new("3", :format => /^\d{4}-\d{2}-\d{2}$/))
39
+ # Sex
40
+ @rule_set << Rule.new("Sex",
41
+ Message.new("[[4::name]] is invalid."),
42
+ InclusionValidator.new("4", :in => %W(M F)))
43
+ # Phone number
44
+ @rule_set << Rule.new("Phone number",
45
+ Message.new("[[5::name]] is invalid."),
46
+ FormatValidator.new("5", :format => /^\d+-\d+-\d$/, :allow_blank => true))
47
+ # ZIP
48
+ @rule_set << Rule.new("ZIP",
49
+ Message.new("[[6::name]] is invalid."),
50
+ FormatValidator.new("6", :format => /^\d{3}-\d{4}$/, :allow_blank => true))
51
+ @error_list_output_spec = OutputSpec.new([
52
+ "ID",
53
+ :error_level,
54
+ :target_numbers,
55
+ :error_message,
56
+ ],
57
+ :target => OutputSpec::TARGET_ERROR,
58
+ :include_no_error => true)
59
+ @error_report_output_spec = OutputSpec.new([
60
+ "ID",
61
+ :error_count,
62
+ :warning_count,
63
+ :error_and_warning_count,
64
+ ],
65
+ :target => OutputSpec::TARGET_DATA)
66
+ @valid_values = %W(1 Tetsuhisa MAKINO 1980-01-25 M 1-2-3 123-3456)
67
+ @invalid_values = ["1", "te", "ma", "1980/01/25", "C", "000000000000", "1234-567"]
68
+ end
69
+
70
+ context "with no parameter" do
71
+ it "should raise ArgumentError" do
72
+ lambda {
73
+ Checker.new
74
+ }.should raise_error ArgumentError
75
+ end
76
+ end
77
+
78
+ context "with only input_spec" do
79
+ it "should raise ArgumentError" do
80
+ lambda {
81
+ Checker.new(@input_spec)
82
+ }.should raise_error ArgumentError
83
+ end
84
+ end
85
+
86
+ context "with input_spec and rule_set" do
87
+ it "should raise ArgumentError" do
88
+ lambda {
89
+ Checker.new(@input_spec, @rule_set)
90
+ }.should raise_error ArgumentError
91
+ end
92
+ end
93
+
94
+ context "with input_spec, rule_set and default_output_spec" do
95
+ it "should not raise error" do
96
+ lambda {
97
+ Checker.new(@input_spec, @rule_set, @error_list_output_spec)
98
+ }.should_not raise_error
99
+ end
100
+ end
101
+ end
102
+
103
+ describe "on after creation" do
104
+ before(:each) do
105
+ @input_spec = InputSpec.new([
106
+ { :number => "0", :name => "ID" },
107
+ { :number => "1", :name => "First name" },
108
+ { :number => "2", :name => "Family name" },
109
+ { :number => "3", :name => "Birthday" },
110
+ { :number => "4", :name => "Sex" },
111
+ { :number => "5", :name => "Phone number" },
112
+ { :number => "6", :name => "ZIP" },
113
+ ])
114
+ @rule_set = RuleSet.new
115
+ # First name
116
+ validator =
117
+ ComplexValidator.new(:validators => [LengthValidator.new("1", :in => 3..25),
118
+ FormatValidator.new("1", :format => /^[a-z]+$/i)])
119
+ @rule_set << Rule.new("First name",
120
+ Message.new("[[1::name]] is invalid."),
121
+ validator)
122
+ # Family name
123
+ validator =
124
+ ComplexValidator.new(:validators => [LengthValidator.new("2", :in => 3..25),
125
+ FormatValidator.new("2", :format => /^[a-z]+$/i)])
126
+ @rule_set << Rule.new("Family name",
127
+ Message.new("[[2::name]] is invalid."),
128
+ validator)
129
+ # Birthday
130
+ @rule_set << Rule.new("Birthday",
131
+ Message.new("[[3::name]] is invalid."),
132
+ FormatValidator.new("3", :format => /^\d{4}-\d{2}-\d{2}$/))
133
+ # Sex
134
+ @rule_set << Rule.new("Sex",
135
+ Message.new("[[4::name]] is invalid."),
136
+ InclusionValidator.new("4", :in => %W(M F)))
137
+ # Phone number
138
+ @rule_set << Rule.new("Phone number",
139
+ Message.new("[[5::name]] is invalid."),
140
+ FormatValidator.new("5", :format => /^\d+-\d+-\d$/, :allow_blank => true))
141
+ # ZIP
142
+ @rule_set << Rule.new("ZIP",
143
+ Message.new("[[6::name]] is invalid."),
144
+ FormatValidator.new("6", :format => /^\d{3}-\d{4}$/, :allow_blank => true))
145
+ @error_list_output_spec = OutputSpec.new([
146
+ "ID",
147
+ :error_level,
148
+ :target_numbers,
149
+ :error_message,
150
+ ],
151
+ :target => OutputSpec::TARGET_ERROR,
152
+ :include_no_error => true)
153
+ @error_report_output_spec = OutputSpec.new([
154
+ "ID",
155
+ :error_count,
156
+ :warning_count,
157
+ :error_and_warning_count,
158
+ ],
159
+ :target => OutputSpec::TARGET_DATA)
160
+ @valid_values = %W(1 Tetsuhisa MAKINO 1980-01-25 M 1-2-3 123-3456)
161
+ @invalid_values = ["1", "te", "ma", "1980/01/25", "C", "000000000000", "1234-567"]
162
+
163
+ @checker = Checker.new(@input_spec, @rule_set, @error_list_output_spec)
164
+ end
165
+
166
+ describe "#add_output_spec" do
167
+ before(:each) do
168
+ @proc = lambda {
169
+ @checker.add_output_spec(@error_report_output_spec)
170
+ }
171
+ end
172
+
173
+ it "should not raise error" do
174
+ @proc.should_not raise_error
175
+ end
176
+
177
+ context "after process" do
178
+ before(:each) do
179
+ @proc.call
180
+ end
181
+
182
+ it "should have 2 output_specs" do
183
+ @checker.instance_eval("@output_specs.size").should == 2
184
+ end
185
+ end
186
+ end
187
+
188
+ describe "#run" do
189
+ before(:each) do
190
+ @checker.add_output_spec(@error_report_output_spec)
191
+ end
192
+
193
+ context "with valid values" do
194
+ before(:each) do
195
+ @proc = lambda {
196
+ @checker.run(@valid_values)
197
+ }
198
+ end
199
+
200
+ it { @proc.should_not raise_error }
201
+
202
+ it "should return 2 outputs" do
203
+ @proc.call.size.should == 2
204
+ end
205
+ end
206
+
207
+ context "with invalid values" do
208
+ before(:each) do
209
+ @proc = lambda {
210
+ @checker.run(@invalid_values)
211
+ }
212
+ end
213
+
214
+ it { @proc.should_not raise_error }
215
+
216
+ it "should return 2 outputs" do
217
+ @proc.call.size.should == 2
218
+ end
219
+ end
220
+ end
221
+
222
+ describe "#validate_configuration" do
223
+ context "when include invalid rule" do
224
+ before(:each) do
225
+ @rule_set << Rule.new("Unknown",
226
+ Message.new("message"),
227
+ FormatValidator.new("3", :format => //))
228
+ end
229
+
230
+ it "should raise InvalidConfiguration" do
231
+ lambda {
232
+ Checker.new(@input_spec,
233
+ @rule_set,
234
+ @error_list_output_spec, true)
235
+ }.should raise_error InvalidConfiguration
236
+ end
237
+ end
238
+
239
+ context "when include invalid output spec" do
240
+ before(:each) do
241
+ @invalid_output_spec = OutputSpec.new(["Unknown"])
242
+ end
243
+
244
+ it "should raise InvalidConfiguration" do
245
+ lambda {
246
+ Checker.new(@input_spec,
247
+ @rule_set,
248
+ @invalid_output_spec, true)
249
+ }.should raise_error InvalidConfiguration
250
+ end
251
+ end
252
+ end
253
+
254
+ describe "#rule_info" do
255
+ before(:each) do
256
+ @checker.add_output_spec(@error_report_output_spec)
257
+ end
258
+
259
+ context "with valid values" do
260
+ before(:each) do
261
+ @proc = lambda {
262
+ @checker.rule_info
263
+ }
264
+ end
265
+
266
+ it { @proc.should_not raise_error }
267
+
268
+ it "should return 2 outputs" do
269
+ @proc.call.should be_a String
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,100 @@
1
+ require "spec_helper"
2
+
3
+ # Anodator::InputSpecItem
4
+ require "anodator/input_spec_item"
5
+
6
+ include Anodator
7
+
8
+ describe InputSpecItem, ".new" do
9
+ context "with no parameter" do
10
+ it "should raise error ArgumentError" do
11
+ lambda {
12
+ InputSpecItem.new
13
+ }.should raise_error ArgumentError
14
+ end
15
+ end
16
+
17
+ context "with only number parameter" do
18
+ it "should raise ArgumentError" do
19
+ lambda {
20
+ InputSpecItem.new("1")
21
+ }.should raise_error ArgumentError
22
+ end
23
+ end
24
+
25
+ context "with blank number parameter" do
26
+ it "should raise ArgumentError" do
27
+ lambda {
28
+ InputSpecItem.new("", "item_1")
29
+ }.should raise_error ArgumentError
30
+ end
31
+ end
32
+
33
+ context "with nil number parameter" do
34
+ it "should raise ArgumentError" do
35
+ lambda {
36
+ InputSpecItem.new(nil, "item_1")
37
+ }.should raise_error ArgumentError
38
+ end
39
+ end
40
+
41
+ context "with blank name parameter" do
42
+ it "should raise ArgumentError" do
43
+ lambda {
44
+ InputSpecItem.new("1", "")
45
+ }.should raise_error ArgumentError
46
+ end
47
+ end
48
+
49
+ context "with nil name parameter" do
50
+ it "should raise ArgumentError" do
51
+ lambda {
52
+ InputSpecItem.new("1", nil)
53
+ }.should raise_error ArgumentError
54
+ end
55
+ end
56
+
57
+ context "with number and name parameter" do
58
+ before(:all) do
59
+ @new_proc = lambda {
60
+ InputSpecItem.new("1", "item_1")
61
+ }
62
+ end
63
+
64
+ it "should not raise error" do
65
+ @new_proc.should_not raise_error
66
+ end
67
+
68
+ context "after generated" do
69
+ before(:all) do
70
+ @input_spec_item = @new_proc.call
71
+ end
72
+
73
+ it { @input_spec_item.number.should == "1" }
74
+ it { @input_spec_item.name.should == "item_1" }
75
+ it { @input_spec_item.type.should == "STRING" }
76
+ end
77
+ end
78
+
79
+ context "with number, name and type(NUMERIC) parameter" do
80
+ before(:all) do
81
+ @new_proc = lambda {
82
+ InputSpecItem.new("1", "item_1", "NUMERIC")
83
+ }
84
+ end
85
+
86
+ it "should not raise error" do
87
+ @new_proc.should_not raise_error
88
+ end
89
+
90
+ context "after generated" do
91
+ before(:all) do
92
+ @input_spec_item = @new_proc.call
93
+ end
94
+
95
+ it { @input_spec_item.number.should == "1" }
96
+ it { @input_spec_item.name.should == "item_1" }
97
+ it { @input_spec_item.type.should == "NUMERIC" }
98
+ end
99
+ end
100
+ end