validity 1.0.1 → 1.0.2
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.
- checksums.yaml +8 -8
- data/lib/validity.rb +22 -25
- data/lib/validity/frameworks/test_unit.rb +3 -0
- data/lib/validity/frameworks/test_unit/record.rb +83 -0
- data/lib/validity/record.rb +13 -32
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTNjOTI5MjRjZjljYTEyNmRhZDllMWY1YzQyNjM3NWI5Yjc3YmEyYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzVjNTdlY2RlNzU4YzJhYzhiNDMyYTVlODNiZTk5NWRlZjUwZmUzMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTVjZTQ1ZjYyYzk0ZWI4YzJjNDM5ZGRmOWFiZmFlNDdkNDA1MmZjODg4NDQx
|
10
|
+
NGZiZGQ1MjhhNzBlNTkyMTlmMDI5YjdlMzIzZDk5YTEzNjllN2I0MmI2OTI2
|
11
|
+
NTY2YWIxOGQ0YmY5NmRmNzJiZGZkZDRjZGQ1Y2Y5NTA5ZTc5OTk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDBkZDFlZDU5MGQ1MDNmMTYzYjA4NzUzZTgzNzFjOGI5OTRkYWQ1YzZlYzI0
|
14
|
+
MGJlMWNjOGYzMTkxOWUwMTQzYTNlNWExMDA3ZmZlNDAyYTkyOTIxZDcwMGRl
|
15
|
+
ZTdjZWYzMDg3YmU1MzU5NDgxMTE5OTU4NjU0YmViZWQxMzU3NmM=
|
data/lib/validity.rb
CHANGED
@@ -10,21 +10,21 @@
|
|
10
10
|
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
11
11
|
# License:: MIT
|
12
12
|
|
13
|
-
require '
|
13
|
+
require 'validity/frameworks/test_unit'
|
14
14
|
require 'validity/record'
|
15
15
|
|
16
16
|
module Validity
|
17
|
-
# Configures Validity to use the provided
|
17
|
+
# Configures Validity to use the provided test_framework.
|
18
18
|
#
|
19
19
|
# * *Args*:
|
20
|
-
# - +
|
20
|
+
# - +test_framework+ the framework to use for testing
|
21
21
|
# * *Returns*:
|
22
|
-
# - the test
|
22
|
+
# - the test framework Validity was configured to use
|
23
23
|
# * *Raises*:
|
24
|
-
# - +Unsupported+ if the provided test
|
25
|
-
def self.configure(
|
26
|
-
raise Unsupported.new(
|
27
|
-
@@
|
24
|
+
# - +Unsupported+ if the provided test framework is unsupported
|
25
|
+
def self.configure(test_framework)
|
26
|
+
raise Unsupported.new(test_framework) if !supported?(test_framework)
|
27
|
+
@@test_framework = test_framework
|
28
28
|
end
|
29
29
|
|
30
30
|
# Returns whether or not Validity is configured.
|
@@ -32,29 +32,29 @@ module Validity
|
|
32
32
|
# * *Returns*:
|
33
33
|
# - whether or not Validity has been configured
|
34
34
|
def self.configured?
|
35
|
-
!@@
|
35
|
+
!@@test_framework.nil?
|
36
36
|
end
|
37
37
|
|
38
38
|
# Resets Validity to an unconfigured state.
|
39
39
|
def self.reset!
|
40
|
-
@@
|
40
|
+
@@test_framework = nil
|
41
41
|
end
|
42
42
|
|
43
|
-
# Returns a list of test
|
43
|
+
# Returns a list of test frameworks Validity supports.
|
44
44
|
#
|
45
45
|
# * *Returns*:
|
46
|
-
# - a list of test
|
46
|
+
# - a list of test frameworks Validity supports
|
47
47
|
def self.supported
|
48
48
|
SUPPORTED
|
49
49
|
end
|
50
50
|
|
51
|
-
# A Validity specific error signifying an unsupported test
|
51
|
+
# A Validity specific error signifying an unsupported test framework.
|
52
52
|
#
|
53
53
|
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
54
54
|
# License:: MIT
|
55
55
|
class Unsupported < RuntimeError
|
56
|
-
def initialize(
|
57
|
-
super(UNSUPPORTED % {
|
56
|
+
def initialize(test_framework)
|
57
|
+
super(UNSUPPORTED % {test_framework: test_framework})
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -78,24 +78,21 @@ module Validity
|
|
78
78
|
raise Unconfigured.new if !configured?
|
79
79
|
end
|
80
80
|
|
81
|
-
module TestUnit
|
82
|
-
end
|
83
|
-
|
84
81
|
SUPPORTED = [TestUnit]
|
85
|
-
UNSUPPORTED = "%{
|
82
|
+
UNSUPPORTED = "%{test_framework} is not a supported test framework, please specify one of #{SUPPORTED.join(', ')}"
|
86
83
|
|
87
84
|
private
|
88
85
|
|
89
|
-
@@
|
86
|
+
@@test_framework = nil
|
90
87
|
|
91
|
-
# Returns whether or not a test
|
88
|
+
# Returns whether or not a test framework is supported by Validity.
|
92
89
|
#
|
93
90
|
# * *Args*:
|
94
|
-
# - +
|
91
|
+
# - +test_framework+ the framework to check
|
95
92
|
# * *Returns*:
|
96
|
-
# - whether or not the
|
97
|
-
def self.supported?(
|
98
|
-
SUPPORTED.include?(
|
93
|
+
# - whether or not the framework is supported by Validity
|
94
|
+
def self.supported?(test_framework)
|
95
|
+
SUPPORTED.include?(test_framework)
|
99
96
|
end
|
100
97
|
end
|
101
98
|
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Validity
|
2
|
+
module TestUnit
|
3
|
+
module Record
|
4
|
+
extend Test::Unit::Assertions
|
5
|
+
# Asserts a record has a +belongs_to+ association as indicated by the provided
|
6
|
+
# +field+ and that the record equals the +target+ record, if provided.
|
7
|
+
#
|
8
|
+
# * *Args*:
|
9
|
+
# - +record+ the record to validate
|
10
|
+
# - +field+ the fields to check for has_many association
|
11
|
+
# - +targets+ the target associated records
|
12
|
+
def self.belongs_to(record, field, target)
|
13
|
+
clazz = record.class
|
14
|
+
assert_respond_to record, field, "#{clazz} cannot find associated #{field}"
|
15
|
+
|
16
|
+
one = record.send(field)
|
17
|
+
assert_not_nil one, "#{clazz} does not have associated #{field}"
|
18
|
+
if target
|
19
|
+
assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Asserts that a record responds to the +delegated+ method and that
|
24
|
+
# the returned object is equal to the object referenced by +delegated_to+.
|
25
|
+
#
|
26
|
+
# * *Args*:
|
27
|
+
# - +record+ the record to validate
|
28
|
+
# - +field+ the fields to check for presence
|
29
|
+
def self.delegates(record, delegated, delegated_to)
|
30
|
+
clazz = record.class
|
31
|
+
assert_respond_to record, delegated, "#{clazz} does not respond to #{delegated}"
|
32
|
+
assert_equal delegated_to.send(delegated), record.send(delegated), "Delegated objects do not match"
|
33
|
+
end
|
34
|
+
|
35
|
+
# Asserts that the given +field+ must be present for a record to be valid.
|
36
|
+
#
|
37
|
+
# * *Args*:
|
38
|
+
# - +record+ the record to validate
|
39
|
+
# - +field+ the fields to check for presence
|
40
|
+
def self.field_presence(record, field)
|
41
|
+
record.send("#{field}=", nil)
|
42
|
+
|
43
|
+
clazz = record.class
|
44
|
+
assert !record.valid?, "#{clazz} is considered valid with nil #{field}"
|
45
|
+
assert !record.save, "#{clazz} saved without #{field} field"
|
46
|
+
assert record.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Asserts that the given +field+ must be unique for a record to be valid.
|
50
|
+
#
|
51
|
+
# * *Args*:
|
52
|
+
# - +record+ the record to validate
|
53
|
+
# - +field+ the fields to check for uniqueness
|
54
|
+
def self.field_uniqueness(record, field)
|
55
|
+
dup = record.dup
|
56
|
+
|
57
|
+
clazz = dup.class
|
58
|
+
assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}"
|
59
|
+
assert !dup.save, "#{clazz} saved with a duplicate #{field}"
|
60
|
+
assert dup.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Asserts a record has a +has_many+ association as indicated by the provided
|
64
|
+
# +field+ and that the many records equal the +targets+ records, if provided.
|
65
|
+
#
|
66
|
+
# * *Args*:
|
67
|
+
# - +record+ the record to validate
|
68
|
+
# - +field+ the fields to check for has_many association
|
69
|
+
# - +targets+ the target associated records
|
70
|
+
def self.has_many(record, field, targets)
|
71
|
+
clazz = record.class
|
72
|
+
assert_respond_to record, field, "#{clazz} cannot find associated #{field}"
|
73
|
+
|
74
|
+
many = record.send(field)
|
75
|
+
assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}"
|
76
|
+
if targets
|
77
|
+
assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
data/lib/validity/record.rb
CHANGED
@@ -8,7 +8,6 @@ module Validity
|
|
8
8
|
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
9
9
|
# License:: MIT
|
10
10
|
class Record
|
11
|
-
include Test::Unit::Assertions
|
12
11
|
include Validity
|
13
12
|
|
14
13
|
attr_reader :record
|
@@ -33,14 +32,7 @@ module Validity
|
|
33
32
|
# - +field+ the fields to check for has_many association
|
34
33
|
# - +targets+ the target associated records
|
35
34
|
def belongs_to(field, target = nil)
|
36
|
-
|
37
|
-
assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
|
38
|
-
|
39
|
-
one = @record.send(field)
|
40
|
-
assert_not_nil one, "#{clazz} does not have associated #{field}"
|
41
|
-
if target
|
42
|
-
assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}"
|
43
|
-
end
|
35
|
+
test_class.belongs_to @record, field, target
|
44
36
|
end
|
45
37
|
|
46
38
|
# Asserts that the record responds to the +delegated+ method and that
|
@@ -49,9 +41,7 @@ module Validity
|
|
49
41
|
# * *Args*:
|
50
42
|
# - +field+ the fields to check for presence
|
51
43
|
def delegates(delegated, delegated_to)
|
52
|
-
|
53
|
-
assert_respond_to @record, delegated, "#{clazz} does not respond to #{delegated}"
|
54
|
-
assert_equal delegated_to.send(delegated), @record.send(delegated), "Delegated objects do not match"
|
44
|
+
test_class.delegates @record, delegated, delegated_to
|
55
45
|
end
|
56
46
|
|
57
47
|
# Asserts that the given +field+ must be present for the record to be valid.
|
@@ -59,12 +49,7 @@ module Validity
|
|
59
49
|
# * *Args*:
|
60
50
|
# - +field+ the fields to check for presence
|
61
51
|
def field_presence(field)
|
62
|
-
@record
|
63
|
-
|
64
|
-
clazz = @record.class
|
65
|
-
assert !@record.valid?, "#{clazz} is considered valid with nil #{field}"
|
66
|
-
assert !@record.save, "#{clazz} saved without #{field} field"
|
67
|
-
assert @record.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
52
|
+
test_class.field_presence @record, field
|
68
53
|
end
|
69
54
|
|
70
55
|
# Asserts that the given +field+ must be unique for the record to be valid.
|
@@ -72,12 +57,7 @@ module Validity
|
|
72
57
|
# * *Args*:
|
73
58
|
# - +field+ the fields to check for uniqueness
|
74
59
|
def field_uniqueness(field)
|
75
|
-
|
76
|
-
|
77
|
-
clazz = dup.class
|
78
|
-
assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}"
|
79
|
-
assert !dup.save, "#{clazz} saved with a duplicate #{field}"
|
80
|
-
assert dup.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
60
|
+
test_class.field_uniqueness @record, field
|
81
61
|
end
|
82
62
|
|
83
63
|
# Asserts the record has a +has_many+ association as indicated by the provided
|
@@ -87,14 +67,7 @@ module Validity
|
|
87
67
|
# - +field+ the fields to check for has_many association
|
88
68
|
# - +targets+ the target associated records
|
89
69
|
def has_many(field, targets = nil)
|
90
|
-
|
91
|
-
assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
|
92
|
-
|
93
|
-
many = @record.send(field)
|
94
|
-
assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}"
|
95
|
-
if targets
|
96
|
-
assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}"
|
97
|
-
end
|
70
|
+
test_class.has_many @record, field, targets
|
98
71
|
end
|
99
72
|
|
100
73
|
private
|
@@ -110,6 +83,14 @@ module Validity
|
|
110
83
|
def initialize(record)
|
111
84
|
self.record = record
|
112
85
|
end
|
86
|
+
|
87
|
+
# Returns the test class to be used based on the current configuration.
|
88
|
+
#
|
89
|
+
# * *Returns*:
|
90
|
+
# - the test class to use based on the current configuration.
|
91
|
+
def test_class
|
92
|
+
@@test_framework::Record
|
93
|
+
end
|
113
94
|
end
|
114
95
|
end
|
115
96
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Fornaciari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|
@@ -73,6 +73,8 @@ extensions: []
|
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
75
|
- lib/validity.rb
|
76
|
+
- lib/validity/frameworks/test_unit.rb
|
77
|
+
- lib/validity/frameworks/test_unit/record.rb
|
76
78
|
- lib/validity/record.rb
|
77
79
|
homepage: http://rubygems.org/gems/validity
|
78
80
|
licenses:
|