formkeeper 0.0.4
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.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +7 -0
- data/formkeeper.gemspec +19 -0
- data/lib/formkeeper/version.rb +3 -0
- data/lib/formkeeper.rb +849 -0
- data/spec/asset/messages.yaml +18 -0
- data/spec/messages_spec.rb +24 -0
- data/spec/record_spec.rb +28 -0
- data/spec/report_spec.rb +83 -0
- data/spec/respondent_spec.rb +174 -0
- data/spec/rule_spec.rb +138 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/validator_spec.rb +273 -0
- metadata +76 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
login:
|
2
|
+
username:
|
3
|
+
present: You must input name
|
4
|
+
ascii: Name should be ASCII
|
5
|
+
length: Name's length should be between 8 and 16
|
6
|
+
DEFAULT: Input your name correctly!
|
7
|
+
password:
|
8
|
+
present: You must input password
|
9
|
+
ascii: Password should be ASCII
|
10
|
+
length: Password's length should be between 8 and 16
|
11
|
+
DEFAULT:
|
12
|
+
username:
|
13
|
+
ascii: Name should be ASCII
|
14
|
+
length: Name's length should be between 8 and 16
|
15
|
+
DEFAULT: Input your name correctly!
|
16
|
+
password:
|
17
|
+
ascii: Password should be ASCII
|
18
|
+
length: Password's length should be between 8 and 16
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormKeeper::Messages do
|
4
|
+
|
5
|
+
it "handles messages file correctly" do
|
6
|
+
|
7
|
+
messages = FormKeeper::Messages.from_file(File.dirname(__FILE__) + '/asset/messages.yaml')
|
8
|
+
|
9
|
+
messages.get('login', 'username', 'present').should == "You must input name"
|
10
|
+
|
11
|
+
# work with symbols
|
12
|
+
messages.get(:login, :username, :present).should == "You must input name"
|
13
|
+
|
14
|
+
# unknown field - use DEFAULT action block
|
15
|
+
messages.get(:login, :username, :unknown).should == "Input your name correctly!"
|
16
|
+
|
17
|
+
# unknown field and DEFAULT action block not found
|
18
|
+
messages.get(:login, :password, :unknown).should == "password is invalid."
|
19
|
+
|
20
|
+
# unknown action - search from DEFAULT action block
|
21
|
+
messages.get(:unknown, :username, :present).should == "Input your name correctly!"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/spec/record_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormKeeper::Record do
|
4
|
+
|
5
|
+
it "handles failure correctly" do
|
6
|
+
record = FormKeeper::Record.new(:username)
|
7
|
+
|
8
|
+
record.name.should == :username
|
9
|
+
|
10
|
+
record.value.should be_nil
|
11
|
+
record.value = 'foo'
|
12
|
+
record.value.should == 'foo'
|
13
|
+
|
14
|
+
record.failed?.should_not be_true
|
15
|
+
|
16
|
+
record.fail(:present)
|
17
|
+
record.fail(:length)
|
18
|
+
|
19
|
+
record.failed?.should be_true
|
20
|
+
record.failed_by?(:present).should be_true
|
21
|
+
record.failed_by?(:length).should be_true
|
22
|
+
record.failed_by?(:characters).should_not be_true
|
23
|
+
|
24
|
+
record.failed_constraints[0].should == :present
|
25
|
+
record.failed_constraints[1].should == :length
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/spec/report_spec.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormKeeper::Report do
|
4
|
+
|
5
|
+
it "handles valid records correctly" do
|
6
|
+
|
7
|
+
report = FormKeeper::Report.new
|
8
|
+
record1 = FormKeeper::Record.new(:username)
|
9
|
+
record1.value = 'foo'
|
10
|
+
record2 = FormKeeper::Record.new(:password)
|
11
|
+
record2.value = 'bar'
|
12
|
+
|
13
|
+
report << record1
|
14
|
+
report << record2
|
15
|
+
|
16
|
+
report.failed?.should_not be_true
|
17
|
+
report[:username].should == 'foo'
|
18
|
+
report[:password].should == 'bar'
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
it "handles invalid records correctly" do
|
23
|
+
|
24
|
+
report = FormKeeper::Report.new
|
25
|
+
record1 = FormKeeper::Record.new(:username)
|
26
|
+
record1.value = 'foo'
|
27
|
+
record2 = FormKeeper::Record.new(:password)
|
28
|
+
record2.value = 'bar'
|
29
|
+
record2.fail(:length)
|
30
|
+
record3 = FormKeeper::Record.new(:email)
|
31
|
+
record3.value = 'bar'
|
32
|
+
record3.fail(:present)
|
33
|
+
record3.fail(:length)
|
34
|
+
|
35
|
+
report << record1
|
36
|
+
report << record2
|
37
|
+
report << record3
|
38
|
+
|
39
|
+
report.failed?.should be_true
|
40
|
+
report.failed_on?(:password).should be_true
|
41
|
+
report.failed_on?(:password, :length).should be_true
|
42
|
+
report.failed_on?(:password, :present).should_not be_true
|
43
|
+
report.failed_on?(:email).should be_true
|
44
|
+
report.failed_on?(:email, :present).should be_true
|
45
|
+
report.failed_on?(:email, :length).should be_true
|
46
|
+
report.failed_on?(:email, :ascii).should_not be_true
|
47
|
+
report[:username].should == 'foo'
|
48
|
+
report[:password].should be_nil
|
49
|
+
report[:email].should be_nil
|
50
|
+
|
51
|
+
report.failed_fields.should == [:password, :email]
|
52
|
+
report.failed_constraints(:password).should == [:length]
|
53
|
+
report.failed_constraints(:email).should == [:present, :length]
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
it "handles messages correctly" do
|
58
|
+
|
59
|
+
messages = FormKeeper::Messages.from_file(File.dirname(__FILE__) + '/asset/messages.yaml')
|
60
|
+
|
61
|
+
report = FormKeeper::Report.new(messages)
|
62
|
+
record1 = FormKeeper::Record.new(:username)
|
63
|
+
record1.value = 'foo'
|
64
|
+
record2 = FormKeeper::Record.new(:password)
|
65
|
+
record2.value = 'bar'
|
66
|
+
record2.fail(:length)
|
67
|
+
record3 = FormKeeper::Record.new(:email)
|
68
|
+
record3.value = 'bar'
|
69
|
+
record3.fail(:present)
|
70
|
+
record3.fail(:length)
|
71
|
+
|
72
|
+
report << record1
|
73
|
+
report << record2
|
74
|
+
report << record3
|
75
|
+
|
76
|
+
report.messages(:login).should == ["Password's length should be between 8 and 16", "email is invalid."]
|
77
|
+
report.messages(:login, :password).should == ["Password's length should be between 8 and 16"]
|
78
|
+
report.message(:login, :password, :ascii).should be_nil
|
79
|
+
report.message(:login, :password, :length).should == "Password's length should be between 8 and 16"
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormKeeper::Respondent do
|
4
|
+
|
5
|
+
it "fills text form correctly" do
|
6
|
+
filled = FormKeeper::Respondent.new.fill_up(<<-HTML, { 'username'=> "Andy >", 'password'=> "Hug", 'message'=> "Hello!<>" })
|
7
|
+
<!DOCTYPE html SYSTEM>
|
8
|
+
<html>
|
9
|
+
<head>test</head>
|
10
|
+
<body>
|
11
|
+
<h1>Login</h1>
|
12
|
+
<form action="/api/post" method="post">
|
13
|
+
<input type="text" name="username" value="input your name here">
|
14
|
+
<input type="password" name="password">
|
15
|
+
<textarea name="message">foobar</textarea>
|
16
|
+
</form>
|
17
|
+
</body>
|
18
|
+
</html>
|
19
|
+
HTML
|
20
|
+
filled.should == <<-HTML
|
21
|
+
<!DOCTYPE html SYSTEM>
|
22
|
+
<html>
|
23
|
+
<head>test</head>
|
24
|
+
<body>
|
25
|
+
<h1>Login</h1>
|
26
|
+
<form action="/api/post" method="post">
|
27
|
+
<input type="text" name="username" value="Andy >" />
|
28
|
+
<input type="password" name="password" value="Hug" />
|
29
|
+
<textarea name="message">Hello!<></textarea>
|
30
|
+
</form>
|
31
|
+
</body>
|
32
|
+
</html>
|
33
|
+
HTML
|
34
|
+
end
|
35
|
+
|
36
|
+
it "checks checkboxes and radio-buttons correctly" do
|
37
|
+
filled = FormKeeper::Respondent.new.fill_up(<<-HTML, { 'food'=> "1", 'media'=> ["1", "2"] })
|
38
|
+
<!DOCTYPE html SYSTEM>
|
39
|
+
<html>
|
40
|
+
<head>test</head>
|
41
|
+
<body>
|
42
|
+
<form action="/api/post" method="post">
|
43
|
+
<h2>Choose your favarite</h2>
|
44
|
+
<label>Sushi</label><input type="radio" name="food" value="0" checked>
|
45
|
+
<label>Sake</label><input type="radio" name="food" value="1">
|
46
|
+
<label>Umeboshi</label><input type="radio" name="food" value="2">
|
47
|
+
<h2>Where did you know about this page?</h2>
|
48
|
+
<label>web site</label><input type="radio" name="media" value="0" checked>
|
49
|
+
<label>magazine</label><input type="radio" name="media" value="1">
|
50
|
+
<label>TV show</label><input type="radio" name="media" value="2">
|
51
|
+
</form>
|
52
|
+
</body>
|
53
|
+
</html>
|
54
|
+
HTML
|
55
|
+
filled.should == <<-HTML
|
56
|
+
<!DOCTYPE html SYSTEM>
|
57
|
+
<html>
|
58
|
+
<head>test</head>
|
59
|
+
<body>
|
60
|
+
<form action="/api/post" method="post">
|
61
|
+
<h2>Choose your favarite</h2>
|
62
|
+
<label>Sushi</label><input type="radio" name="food" value="0" />
|
63
|
+
<label>Sake</label><input type="radio" name="food" value="1" checked="checked" />
|
64
|
+
<label>Umeboshi</label><input type="radio" name="food" value="2" />
|
65
|
+
<h2>Where did you know about this page?</h2>
|
66
|
+
<label>web site</label><input type="radio" name="media" value="0" />
|
67
|
+
<label>magazine</label><input type="radio" name="media" value="1" checked="checked" />
|
68
|
+
<label>TV show</label><input type="radio" name="media" value="2" checked="checked" />
|
69
|
+
</form>
|
70
|
+
</body>
|
71
|
+
</html>
|
72
|
+
HTML
|
73
|
+
end
|
74
|
+
|
75
|
+
it "fills select-box form correctly" do
|
76
|
+
filled = FormKeeper::Respondent.new.fill_up(<<-HTML, { 'favorite' => '1' })
|
77
|
+
<!DOCTYPE html SYSTEM>
|
78
|
+
<html>
|
79
|
+
<head>test</head>
|
80
|
+
<body>
|
81
|
+
<form action="/api/post" method="post">
|
82
|
+
<select name="favorite">
|
83
|
+
<option value="1">white</option>
|
84
|
+
<option value="2">black</option>
|
85
|
+
<option value="3" selected>blue</option>
|
86
|
+
</select>
|
87
|
+
</form>
|
88
|
+
</body>
|
89
|
+
</html>
|
90
|
+
HTML
|
91
|
+
filled.should == <<-HTML
|
92
|
+
<!DOCTYPE html SYSTEM>
|
93
|
+
<html>
|
94
|
+
<head>test</head>
|
95
|
+
<body>
|
96
|
+
<form action="/api/post" method="post">
|
97
|
+
<select name="favorite">
|
98
|
+
<option value="1" selected="selected">white</option>
|
99
|
+
<option value="2">black</option>
|
100
|
+
<option value="3">blue</option>
|
101
|
+
</select>
|
102
|
+
</form>
|
103
|
+
</body>
|
104
|
+
</html>
|
105
|
+
HTML
|
106
|
+
end
|
107
|
+
|
108
|
+
it "fills multiple select-box form correctly" do
|
109
|
+
filled = FormKeeper::Respondent.new.fill_up(<<-HTML, { 'favorite' => ['1', '2'] })
|
110
|
+
<!DOCTYPE html SYSTEM>
|
111
|
+
<html>
|
112
|
+
<head>test</head>
|
113
|
+
<body>
|
114
|
+
<form action="/api/post" method="post">
|
115
|
+
<select name="favorite" multiple>
|
116
|
+
<option value="1">white</option>
|
117
|
+
<option value="2">black</option>
|
118
|
+
<option value="3" selected>blue</option>
|
119
|
+
</select>
|
120
|
+
</form>
|
121
|
+
</body>
|
122
|
+
</html>
|
123
|
+
HTML
|
124
|
+
filled.should == <<-HTML
|
125
|
+
<!DOCTYPE html SYSTEM>
|
126
|
+
<html>
|
127
|
+
<head>test</head>
|
128
|
+
<body>
|
129
|
+
<form action="/api/post" method="post">
|
130
|
+
<select name="favorite" multiple>
|
131
|
+
<option value="1" selected="selected">white</option>
|
132
|
+
<option value="2" selected="selected">black</option>
|
133
|
+
<option value="3">blue</option>
|
134
|
+
</select>
|
135
|
+
</form>
|
136
|
+
</body>
|
137
|
+
</html>
|
138
|
+
HTML
|
139
|
+
end
|
140
|
+
|
141
|
+
it "fills non-multiple select-box form correctly" do
|
142
|
+
filled = FormKeeper::Respondent.new.fill_up(<<-HTML, { 'favorite' => ['1', '2'] })
|
143
|
+
<!DOCTYPE html SYSTEM>
|
144
|
+
<html>
|
145
|
+
<head>test</head>
|
146
|
+
<body>
|
147
|
+
<form action="/api/post" method="post">
|
148
|
+
<select name="favorite">
|
149
|
+
<option value="1">white</option>
|
150
|
+
<option value="2">black</option>
|
151
|
+
<option value="3" selected>blue</option>
|
152
|
+
</select>
|
153
|
+
</form>
|
154
|
+
</body>
|
155
|
+
</html>
|
156
|
+
HTML
|
157
|
+
filled.should == <<-HTML
|
158
|
+
<!DOCTYPE html SYSTEM>
|
159
|
+
<html>
|
160
|
+
<head>test</head>
|
161
|
+
<body>
|
162
|
+
<form action="/api/post" method="post">
|
163
|
+
<select name="favorite">
|
164
|
+
<option value="1" selected="selected">white</option>
|
165
|
+
<option value="2">black</option>
|
166
|
+
<option value="3">blue</option>
|
167
|
+
</select>
|
168
|
+
</form>
|
169
|
+
</body>
|
170
|
+
</html>
|
171
|
+
HTML
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
data/spec/rule_spec.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FormKeeper::Rule do
|
4
|
+
|
5
|
+
it "handles default filter correctly" do
|
6
|
+
|
7
|
+
rule = FormKeeper::Rule.new
|
8
|
+
rule.filters :strip
|
9
|
+
|
10
|
+
rule.default_filters[0].should == :strip
|
11
|
+
|
12
|
+
rule.filters :upcase, :downcase
|
13
|
+
rule.default_filters[0].should == :upcase
|
14
|
+
rule.default_filters[1].should == :downcase
|
15
|
+
end
|
16
|
+
|
17
|
+
it "handles field criteria correctly" do
|
18
|
+
|
19
|
+
rule = FormKeeper::Rule.new
|
20
|
+
|
21
|
+
# missing parameters
|
22
|
+
proc {
|
23
|
+
rule.field :field01
|
24
|
+
}.should raise_error(ArgumentError)
|
25
|
+
|
26
|
+
proc {
|
27
|
+
rule.field :field01, {}
|
28
|
+
}.should_not raise_error
|
29
|
+
|
30
|
+
proc {
|
31
|
+
rule.field :field01, :present => true
|
32
|
+
}.should_not raise_error
|
33
|
+
|
34
|
+
proc {
|
35
|
+
rule.field :field01, :default => 'foobar'
|
36
|
+
}.should_not raise_error
|
37
|
+
|
38
|
+
# don't set present and default at once
|
39
|
+
proc {
|
40
|
+
rule.field :username, :present => true, :default => 'foobar'
|
41
|
+
}.should raise_error(ArgumentError)
|
42
|
+
|
43
|
+
proc {
|
44
|
+
rule.field :field01, :default => 'foobar', :filters => [:strip]
|
45
|
+
}.should_not raise_error
|
46
|
+
|
47
|
+
proc {
|
48
|
+
rule.field :field01, :default => 'foobar', :filters => :strip
|
49
|
+
}.should_not raise_error
|
50
|
+
|
51
|
+
# invalid filters
|
52
|
+
proc {
|
53
|
+
rule.field :username, :present => true, :filters => { :foo => 'bar' }
|
54
|
+
}.should raise_error(ArgumentError)
|
55
|
+
|
56
|
+
proc {
|
57
|
+
rule.field :field01, :default => 'foobar', :length => 0..10
|
58
|
+
}.should_not raise_error
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
it "handles checkbox criteria correctly" do
|
63
|
+
|
64
|
+
rule = FormKeeper::Rule.new
|
65
|
+
|
66
|
+
proc {
|
67
|
+
rule.checkbox :field01
|
68
|
+
}.should raise_error(ArgumentError)
|
69
|
+
|
70
|
+
proc {
|
71
|
+
rule.checkbox :field01, {}
|
72
|
+
}.should_not raise_error
|
73
|
+
|
74
|
+
proc {
|
75
|
+
rule.checkbox :field01, :count => 1
|
76
|
+
}.should_not raise_error
|
77
|
+
|
78
|
+
proc {
|
79
|
+
rule.checkbox :field01, :count => 0..2
|
80
|
+
}.should_not raise_error
|
81
|
+
|
82
|
+
proc {
|
83
|
+
rule.checkbox :field01, :count => 'hoge'
|
84
|
+
}.should raise_error(ArgumentError)
|
85
|
+
|
86
|
+
proc {
|
87
|
+
rule.checkbox :field01, :count => 1, :length => 0..10
|
88
|
+
}.should_not raise_error
|
89
|
+
|
90
|
+
proc {
|
91
|
+
rule.checkbox :field01, :count => 1, :length => 0..10, :filters => [:strip]
|
92
|
+
}.should_not raise_error
|
93
|
+
|
94
|
+
proc {
|
95
|
+
rule.checkbox :field01, :count => 1, :length => 0..10, :filters => :strip
|
96
|
+
}.should_not raise_error
|
97
|
+
|
98
|
+
# invalid filters
|
99
|
+
proc {
|
100
|
+
rule.checkbox :username, :count => 1, :filters => { :foo => 'bar' }
|
101
|
+
}.should raise_error(ArgumentError)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "handles combination criteria correctly" do
|
105
|
+
|
106
|
+
rule = FormKeeper::Rule.new
|
107
|
+
proc {
|
108
|
+
rule.combination :custom
|
109
|
+
}.should raise_error
|
110
|
+
|
111
|
+
# missing constraint
|
112
|
+
proc {
|
113
|
+
rule.combination :custom, :fields => [:field01, :field02]
|
114
|
+
}.should raise_error(ArgumentError)
|
115
|
+
|
116
|
+
# missing fields
|
117
|
+
proc {
|
118
|
+
rule.combination :custom, :date => true
|
119
|
+
}.should raise_error(ArgumentError)
|
120
|
+
|
121
|
+
proc {
|
122
|
+
rule.combination :custom, :fields => [:field01, :field02], :date => true
|
123
|
+
}.should_not raise_error
|
124
|
+
|
125
|
+
proc {
|
126
|
+
rule.combination :custom, :fields => [:field01, :field02], :date => true, :filters => [:strip]
|
127
|
+
}.should_not raise_error
|
128
|
+
|
129
|
+
proc {
|
130
|
+
rule.combination :custom, :fields => [:field01, :field02], :date => true, :filters => :strip
|
131
|
+
}.should_not raise_error
|
132
|
+
|
133
|
+
proc {
|
134
|
+
rule.combination :custom, :fields => [:field01, :field02], :date => true, :filters => {}
|
135
|
+
}.should raise_error(ArgumentError)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|