validation_matchers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore
ADDED
File without changes
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ValidationMatchers
|
2
|
+
module ActiveModel
|
3
|
+
def pass_validate_each(attribute, value)
|
4
|
+
PassEachValidationMatcher.new(attribute, value)
|
5
|
+
end
|
6
|
+
|
7
|
+
def fail_validate_each(attribute, value)
|
8
|
+
FailEachValidationMatcher.new(attribute, value)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
class Matcher
|
14
|
+
def initialize(attribute, value)
|
15
|
+
@value = value
|
16
|
+
@attribute = attribute
|
17
|
+
@record = TestRecord.new(attribute)
|
18
|
+
end
|
19
|
+
|
20
|
+
def run_validate_each!(validator)
|
21
|
+
@validator = validator
|
22
|
+
@validator.validate_each(@record, @attribute, @value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def unexpected_errors_ocurred_message
|
26
|
+
"expected no validation errors, got #{@record.errors[@attribute].inspect}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def expected_errors_to_occur_failure_message
|
30
|
+
"expected validation errors"
|
31
|
+
end
|
32
|
+
|
33
|
+
def errors_found?
|
34
|
+
!@record.errors[@attribute].empty?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class PassEachValidationMatcher < Matcher
|
39
|
+
def matches?(validator)
|
40
|
+
run_validate_each! validator
|
41
|
+
!errors_found?
|
42
|
+
end
|
43
|
+
alias :failure_message :unexpected_errors_ocurred_message
|
44
|
+
alias :negative_failure_message :expected_errors_to_occur_failure_message
|
45
|
+
end
|
46
|
+
|
47
|
+
class FailEachValidationMatcher < Matcher
|
48
|
+
def matches?(validator)
|
49
|
+
run_validate_each! validator
|
50
|
+
errors_found?
|
51
|
+
end
|
52
|
+
alias :failure_message :expected_errors_to_occur_failure_message
|
53
|
+
alias :negative_failure_message :unexpected_errors_ocurred_message
|
54
|
+
end
|
55
|
+
|
56
|
+
class TestRecord
|
57
|
+
attr_accessor :errors
|
58
|
+
def initialize( attr )
|
59
|
+
@errors = {attr => []}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_model'
|
3
|
+
require 'validation_matchers/active_model'
|
4
|
+
|
5
|
+
class ExampleValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
unless value == "valid value"
|
8
|
+
record.errors[attribute] << "not valid value"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe ExampleValidator do
|
14
|
+
include ValidationMatchers::ActiveModel
|
15
|
+
subject { ExampleValidator.new({:attributes=>{}}) }
|
16
|
+
|
17
|
+
it "allows a valid value" do
|
18
|
+
subject.should pass_validate_each(:attr, "valid value")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not allow an invalid value" do
|
22
|
+
subject.should_not pass_validate_each(:attr, "invalid value")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "rejects an invalid value" do
|
26
|
+
subject.should fail_validate_each(:attr, "invalid value")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not reject a valid value" do
|
30
|
+
subject.should_not fail_validate_each(:attr, "valid value")
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation_matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adrian Mowat, Peter Aitken
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "RSpec matchers for testing ActiveModel custom validations in isolation
|
15
|
+
\nfrom your application's models\n"
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- lib/validation_matchers/active_model.rb
|
24
|
+
- lib/validation_matchers/active_model/validate_each_matchers.rb
|
25
|
+
- spec/spec_helper.rb
|
26
|
+
- spec/validation_matchers/active_model/validate_each_matchers_spec.rb
|
27
|
+
homepage: https://github.com/edgecase/validation_matchers
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.10
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: RSpec matchers for testing ActiveModel custom validations
|
51
|
+
test_files:
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/validation_matchers/active_model/validate_each_matchers_spec.rb
|