scrivener 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1646faeced5318ac81a17b7fc8d05b59508b0d9
4
- data.tar.gz: 80b3fa99edfe7c6e079bafa33fe3e90870701d5d
3
+ metadata.gz: a106b8c9aa9ba83fc66fec4afc4c030da9f25291
4
+ data.tar.gz: d5918f87defb857d0870932c1914db935497aca8
5
5
  SHA512:
6
- metadata.gz: 207456238ab18834fa1ab9834610e19a20def26196fac1780edaee47ed6046d2581cbae5c0a07e58877c4a30b85e5a3a347b21406b0a910900ae19488c5e1ea7
7
- data.tar.gz: 5a8fe4ca21cd06578fa4287324f44ce58420ac8c9f7d87f1dccd34e0df3348eb7c2c10aa176a2ec6dd52cce3649a68af8dc1afdc152f789af22f1b4d2caecb27
6
+ metadata.gz: 5afc2e5969db3a93670a20ede2992d3a1e095369d4407292bda46956f3d50fa8bc857d1dc9233adaf45fb044f99bdc672414fc0dc58655cc3ee1dc6d02295cd6
7
+ data.tar.gz: cab3e3c0e586c192768ce7788359cfcb0fd36a8b8681f7097a5cd4deeeddf544fc0482fffba70e09cf3708dbee3b752c5726649c1c3e89ccd56a8e6eb6e2413e
data/README.md CHANGED
@@ -184,6 +184,14 @@ end
184
184
  Checks that the given field is not nil or empty. The error code for this
185
185
  assertion is `:not_present`.
186
186
 
187
+ ### assert_equal
188
+
189
+ Check that the attribute has the expected value. It uses === for
190
+ comparison, so type checks are possible too. Note that in order to
191
+ make the case equality work, the check inverts the order of the
192
+ arguments: `assert_equal :foo, Bar` is translated to the expression
193
+ `Bar === send(:foo)`.
194
+
187
195
  ### assert_format
188
196
 
189
197
  Checks that the given field matches the provided regular expression.
@@ -1,7 +1,7 @@
1
1
  require_relative "scrivener/validations"
2
2
 
3
3
  class Scrivener
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
 
6
6
  include Validations
7
7
 
@@ -12,6 +12,7 @@ class Scrivener
12
12
  # * assert_member
13
13
  # * assert_length
14
14
  # * assert_decimal
15
+ # * assert_equal
15
16
  #
16
17
  # The core tenets that Scrivener::Validations advocates can be summed up in a
17
18
  # few bullet points:
@@ -168,6 +169,27 @@ class Scrivener
168
169
  assert_format att, DECIMAL, error
169
170
  end
170
171
 
172
+ # Check that the attribute has the expected value. It uses === for
173
+ # comparison, so type checks are possible too. Note that in order
174
+ # to make the case equality work, the check inverts the order of
175
+ # the arguments: `assert_equal :foo, Bar` is translated to the
176
+ # expression `Bar === send(:foo)`.
177
+ #
178
+ # @example
179
+ #
180
+ # def validate
181
+ # assert_equal :status, "pending"
182
+ # assert_equal :quantity, Fixnum
183
+ # end
184
+ #
185
+ # @param [Symbol] att The attribute you wish to verify for equality.
186
+ # @param [Object] value The value you want to test against.
187
+ # @param [Array<Symbol, Symbol>] error The error that should be returned
188
+ # when the validation fails.
189
+ def assert_equal(att, value, error = [att, :not_equal])
190
+ assert value === send(att), error
191
+ end
192
+
171
193
  # The grand daddy of all assertions. If you want to build custom
172
194
  # assertions, or even quick and dirty ones, you can simply use this method.
173
195
  #
@@ -1,6 +1,6 @@
1
1
  require File.expand_path("../lib/scrivener", File.dirname(__FILE__))
2
2
 
3
- class S < Scrivener
3
+ class A < Scrivener
4
4
  attr_accessor :a
5
5
  attr_accessor :b
6
6
  end
@@ -10,34 +10,34 @@ scope do
10
10
  atts = { :a => 1, :b => 2, :c => 3 }
11
11
 
12
12
  assert_raise NoMethodError do
13
- s = S.new(atts)
13
+ filter = A.new(atts)
14
14
  end
15
15
  end
16
16
 
17
17
  test "not raise when there are less fields" do
18
18
  atts = { :a => 1 }
19
19
 
20
- assert s = S.new(atts)
20
+ assert filter = A.new(atts)
21
21
  end
22
22
 
23
23
  test "return attributes" do
24
24
  atts = { :a => 1, :b => 2 }
25
25
 
26
- s = S.new(atts)
26
+ filter = A.new(atts)
27
27
 
28
- assert_equal atts, s.attributes
28
+ assert_equal atts, filter.attributes
29
29
  end
30
30
 
31
31
  test "return only the required attributes" do
32
32
  atts = { :a => 1, :b => 2 }
33
33
 
34
- s = S.new(atts)
34
+ filter = A.new(atts)
35
35
 
36
- assert_equal s.slice(:a), { :a => 1 }
36
+ assert_equal filter.slice(:a), { :a => 1 }
37
37
  end
38
38
  end
39
39
 
40
- class T < Scrivener
40
+ class B < Scrivener
41
41
  attr_accessor :a
42
42
  attr_accessor :b
43
43
 
@@ -51,54 +51,54 @@ scope do
51
51
  test "validations" do
52
52
  atts = { :a => 1, :b => 2 }
53
53
 
54
- t = T.new(atts)
54
+ filter = B.new(atts)
55
55
 
56
- assert t.valid?
56
+ assert filter.valid?
57
57
  end
58
58
 
59
59
  test "validation errors" do
60
60
  atts = { :a => 1 }
61
61
 
62
- t = T.new(atts)
62
+ filter = B.new(atts)
63
63
 
64
- assert_equal false, t.valid?
65
- assert_equal [], t.errors[:a]
66
- assert_equal [:not_present], t.errors[:b]
64
+ assert_equal false, filter.valid?
65
+ assert_equal [], filter.errors[:a]
66
+ assert_equal [:not_present], filter.errors[:b]
67
67
  end
68
68
 
69
69
  test "attributes without @errors" do
70
70
  atts = { :a => 1, :b => 2 }
71
71
 
72
- t = T.new(atts)
72
+ filter = B.new(atts)
73
73
 
74
- t.valid?
75
- assert_equal atts, t.attributes
74
+ filter.valid?
75
+ assert_equal atts, filter.attributes
76
76
  end
77
77
  end
78
78
 
79
- class Quote
79
+ class C
80
80
  include Scrivener::Validations
81
81
 
82
- attr_accessor :foo
82
+ attr_accessor :a
83
83
 
84
84
  def validate
85
- assert_present :foo
85
+ assert_present :a
86
86
  end
87
87
  end
88
88
 
89
89
  scope do
90
90
  test "validations without Scrivener" do
91
- q = Quote.new
92
- q.foo = 1
93
- assert q.valid?
91
+ filter = C.new
92
+ filter.a = 1
93
+ assert filter.valid?
94
94
 
95
- q = Quote.new
96
- assert_equal false, q.valid?
97
- assert_equal [:not_present], q.errors[:foo]
95
+ filter = C.new
96
+ assert_equal false, filter.valid?
97
+ assert_equal [:not_present], filter.errors[:a]
98
98
  end
99
99
  end
100
100
 
101
- class Post < Scrivener
101
+ class D < Scrivener
102
102
  attr_accessor :url, :email
103
103
 
104
104
  def validate
@@ -109,52 +109,52 @@ end
109
109
 
110
110
  scope do
111
111
  test "email & url" do
112
- p = Post.new({})
112
+ filter = D.new({})
113
113
 
114
- assert ! p.valid?
115
- assert_equal [:not_url], p.errors[:url]
116
- assert_equal [:not_email], p.errors[:email]
114
+ assert ! filter.valid?
115
+ assert_equal [:not_url], filter.errors[:url]
116
+ assert_equal [:not_email], filter.errors[:email]
117
117
 
118
- p = Post.new(url: "google.com", email: "egoogle.com")
118
+ filter = D.new(url: "google.com", email: "egoogle.com")
119
119
 
120
- assert ! p.valid?
121
- assert_equal [:not_url], p.errors[:url]
122
- assert_equal [:not_email], p.errors[:email]
120
+ assert ! filter.valid?
121
+ assert_equal [:not_url], filter.errors[:url]
122
+ assert_equal [:not_email], filter.errors[:email]
123
123
 
124
- p = Post.new(url: "http://google.com", email: "me@google.com")
125
- assert p.valid?
124
+ filter = D.new(url: "http://google.com", email: "me@google.com")
125
+ assert filter.valid?
126
126
  end
127
127
  end
128
128
 
129
- class Person < Scrivener
130
- attr_accessor :username
129
+ class E < Scrivener
130
+ attr_accessor :a
131
131
 
132
132
  def validate
133
- assert_length :username, 3..10
133
+ assert_length :a, 3..10
134
134
  end
135
135
  end
136
136
 
137
137
  scope do
138
138
  test "length validation" do
139
- p = Person.new({})
139
+ filter = E.new({})
140
140
 
141
- assert ! p.valid?
142
- assert p.errors[:username].include?(:not_in_range)
141
+ assert ! filter.valid?
142
+ assert filter.errors[:a].include?(:not_in_range)
143
143
 
144
- p = Person.new(username: "fo")
145
- assert ! p.valid?
146
- assert p.errors[:username].include?(:not_in_range)
144
+ filter = E.new(a: "fo")
145
+ assert ! filter.valid?
146
+ assert filter.errors[:a].include?(:not_in_range)
147
147
 
148
- p = Person.new(username: "foofoofoofo")
149
- assert ! p.valid?
150
- assert p.errors[:username].include?(:not_in_range)
148
+ filter = E.new(a: "foofoofoofo")
149
+ assert ! filter.valid?
150
+ assert filter.errors[:a].include?(:not_in_range)
151
151
 
152
- p = Person.new(username: "foo")
153
- assert p.valid?
152
+ filter = E.new(a: "foo")
153
+ assert filter.valid?
154
154
  end
155
155
  end
156
156
 
157
- class Order < Scrivener
157
+ class F < Scrivener
158
158
  attr_accessor :status
159
159
 
160
160
  def validate
@@ -164,38 +164,99 @@ end
164
164
 
165
165
  scope do
166
166
  test "member validation" do
167
- o = Order.new({})
168
- assert ! o.valid?
169
- assert_equal [:not_valid], o.errors[:status]
167
+ filter = F.new({})
168
+ assert ! filter.valid?
169
+ assert_equal [:not_valid], filter.errors[:status]
170
170
 
171
- o = Order.new(status: "foo")
172
- assert ! o.valid?
173
- assert_equal [:not_valid], o.errors[:status]
171
+ filter = F.new(status: "foo")
172
+ assert ! filter.valid?
173
+ assert_equal [:not_valid], filter.errors[:status]
174
174
 
175
175
  %w{pending paid delivered}.each do |status|
176
- o = Order.new(status: status)
177
- assert o.valid?
176
+ filter = F.new(status: status)
177
+ assert filter.valid?
178
178
  end
179
179
  end
180
180
  end
181
181
 
182
- class Product < Scrivener
183
- attr_accessor :price
182
+ class G < Scrivener
183
+ attr_accessor :a
184
184
 
185
185
  def validate
186
- assert_decimal :price
186
+ assert_decimal :a
187
187
  end
188
188
  end
189
189
 
190
190
  scope do
191
191
  test "decimal validation" do
192
- p = Product.new({})
193
- assert ! p.valid?
194
- assert_equal [:not_decimal], p.errors[:price]
192
+ filter = G.new({})
193
+ assert ! filter.valid?
194
+ assert_equal [:not_decimal], filter.errors[:a]
195
195
 
196
- %w{10 10.1 10.100000 0.100000 .1000}.each do |price|
197
- p = Product.new(price: price)
198
- assert p.valid?
196
+ %w{10 10.1 10.100000 0.100000 .1000}.each do |a|
197
+ filter = G.new(a: a)
198
+ assert filter.valid?
199
199
  end
200
200
  end
201
201
  end
202
+
203
+ class H < Scrivener
204
+ attr_accessor :a
205
+
206
+ def validate
207
+ assert_length :a, 3..10
208
+ end
209
+ end
210
+
211
+ scope do
212
+ test "length validation" do
213
+ filter = H.new({})
214
+
215
+ assert ! filter.valid?
216
+ assert filter.errors[:a].include?(:not_in_range)
217
+
218
+ filter = H.new(a: "fo")
219
+ assert ! filter.valid?
220
+ assert filter.errors[:a].include?(:not_in_range)
221
+
222
+ filter = H.new(a: "foofoofoofo")
223
+ assert ! filter.valid?
224
+ assert filter.errors[:a].include?(:not_in_range)
225
+
226
+ filter = H.new(a: "foo")
227
+ assert filter.valid?
228
+ end
229
+ end
230
+
231
+ class I < Scrivener
232
+ attr_accessor :a
233
+ attr_accessor :b
234
+
235
+ def validate
236
+ assert_equal :a, "foo"
237
+ assert_equal :b, Fixnum
238
+ end
239
+ end
240
+
241
+
242
+ scope do
243
+ test "equality validation" do
244
+ filter = I.new({})
245
+
246
+ assert ! filter.valid?
247
+ assert filter.errors[:a].include?(:not_equal)
248
+ assert filter.errors[:b].include?(:not_equal)
249
+
250
+ filter = I.new(a: "foo", b: "bar")
251
+ assert ! filter.valid?
252
+
253
+ filter = I.new(a: "foo")
254
+ assert ! filter.valid?
255
+ assert filter.errors[:a].empty?
256
+ assert filter.errors[:b].include?(:not_equal)
257
+
258
+ filter = I.new(a: "foo", b: 42)
259
+ filter.valid?
260
+ assert filter.valid?
261
+ end
262
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrivener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-25 00:00:00.000000000 Z
11
+ date: 2013-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest