mongoid-minitest 0.1.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Helpers
4
- def to_sentence ary
5
- ary.collect(&:inspect).to_sentence
6
- end
7
-
8
- def class_of subject
9
- subject.is_a?(Class) ? subject : subject.class
10
- end
11
- end
12
- end
13
- end
@@ -1,47 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateAcceptanceMatcher < HaveValidationMatcher
5
- def initialize(field)
6
- super(field, :acceptance)
7
- end
8
-
9
- def accept_with(value)
10
- @accepted = value
11
- self
12
- end
13
-
14
- def matches?(subject)
15
- return false unless @result = super(subject)
16
-
17
- check_accepted_value if @accepted
18
-
19
- @result
20
- end
21
-
22
- def description
23
- desc = []
24
- desc << " accept with #{@accepted.inspect}" if @accepted
25
- super << desc.to_sentence
26
- end
27
-
28
- private
29
-
30
- def check_accepted_value
31
- actual = @validator.options[:accept]
32
-
33
- if actual == @accepted
34
- @positive_message << " accept with #{actual.inspect}"
35
- else
36
- @negative_message << " accept with #{actual.inspect}"
37
- @result = false
38
- end
39
- end
40
- end
41
-
42
- def validate_acceptance_of(field)
43
- ValidateAcceptanceMatcher.new(field)
44
- end
45
- end
46
- end
47
- end
@@ -1,19 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateAssociated < HaveValidationMatcher
5
- def initialize(association_name)
6
- super(association_name, :associated)
7
- end
8
-
9
- def description
10
- "validate associated #{@field.inspect}"
11
- end
12
- end
13
-
14
- def validate_associated(association_name)
15
- ValidateAssociated.new(association_name)
16
- end
17
- end
18
- end
19
- end
@@ -1,9 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- def validate_confirmation_of(field)
5
- HaveValidationMatcher.new(field, :confirmation)
6
- end
7
- end
8
- end
9
- end
@@ -1,52 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateExclusionMatcher < HaveValidationMatcher
5
- def initialize(field)
6
- super(field, :exclusion)
7
- end
8
-
9
- def to_not_allow(*values)
10
- @not_allowed_values = (values.length > 1) ? values.flatten : values[0]
11
- self
12
- end
13
-
14
- def matches?(subject)
15
- return false unless result = super(subject)
16
-
17
- if Array === @not_allowed_values
18
- allowed_values = @not_allowed_values - @validator.options[:in].to_a
19
- if allowed_values.empty?
20
- @positive_message << ' not allowing all values mentioned'
21
- else
22
- @negative_message << ' allowing the values:'
23
- @negative_message << " #{to_sentence(allowed_values)}"
24
- result = false
25
- end
26
- elsif @not_allowed_values
27
- if @not_allowed_values == @validator.options[:in]
28
- @positive_message << " not allowing values in #{@not_allowed_values.inspect}"
29
- else
30
- @negative_message << " not allowing values in #{@validator.options[:in].inspect}"
31
- result = false
32
- end
33
- end
34
-
35
- result
36
- end
37
-
38
- def description
39
- if Array === @not_allowed_values
40
- super << " not allowing the values: #{to_sentence(@not_allowed_values)}"
41
- elsif @not_allowed_values
42
- super << " not allowing the values in #{@not_allowed_values.inspect}"
43
- end
44
- end
45
- end
46
-
47
- def validate_exclusion_of(field)
48
- ValidateExclusionMatcher.new(field)
49
- end
50
- end
51
- end
52
- end
@@ -1,65 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateFormatMatcher < HaveValidationMatcher
5
- def initialize field
6
- super field, :format
7
- end
8
-
9
- def to_allow valid_value
10
- @valid = valid_value
11
- self
12
- end
13
-
14
- def to_not_allow invalid_value
15
- @invalid = invalid_value
16
- self
17
- end
18
-
19
- def matches? subject
20
- return false unless @result = super(subject)
21
-
22
- check_valid_value if @valid
23
- check_invalid_value if @invalid
24
-
25
- @result
26
- end
27
-
28
- def description
29
- desc = []
30
- desc << " allowing the value #{@valid.inspect}" if @valid
31
- desc << " not allowing the value #{@invalid.inspect}" if @invalid
32
- super << desc.to_sentence
33
- end
34
-
35
- private
36
-
37
- def check_valid_value
38
- if format == @valid || format =~ @valid
39
- @positive_message << " with #{@valid.inspect} as a valid value"
40
- else
41
- @negative_message << " with #{@valid.inspect} as an invalid value"
42
- @result = false
43
- end
44
- end
45
-
46
- def check_invalid_value
47
- if format !~ @invalid
48
- @positive_message << " with #{@invalid.inspect} as a invalid value"
49
- else
50
- @negative_message << " with #{@invalid.inspect} as a valid value"
51
- @result = false
52
- end
53
- end
54
-
55
- def format
56
- @validator.options[:with]
57
- end
58
- end
59
-
60
- def validate_format_of field
61
- ValidateFormatMatcher.new field
62
- end
63
- end
64
- end
65
- end
@@ -1,52 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateInclusionMatcher < HaveValidationMatcher
5
- def initialize(field)
6
- super(field, :inclusion)
7
- end
8
-
9
- def to_allow(*values)
10
- @allowed_values = (values.length > 1) ? values.flatten : values[0]
11
- self
12
- end
13
-
14
- def matches?(subject)
15
- return false unless result = super(subject)
16
-
17
- if Array === @allowed_values
18
- not_allowed_values = @allowed_values - @validator.options[:in].to_a
19
- if not_allowed_values.empty?
20
- @positive_message << ' allowing all values mentioned'
21
- else
22
- @negative_message << ' not allowing the following the values:'
23
- @negative_message << " #{not_allowed_values.inspect}"
24
- result = false
25
- end
26
- elsif @allowed_values
27
- if @allowed_values == @validator.options[:in]
28
- @positive_message << " allowing values in #{@allowed_values.inspect}"
29
- else
30
- @negative_message << " allowing values in #{@validator.options[:in].inspect}"
31
- result = false
32
- end
33
- end
34
-
35
- result
36
- end
37
-
38
- def description
39
- if Array === @allowed_values
40
- super << " allowing the values: #{to_sentence(@allowed_values)}"
41
- elsif @allowed_values
42
- super << " allowing the values in #{@allowed_values.inspect}"
43
- end
44
- end
45
- end
46
-
47
- def validate_inclusion_of(field)
48
- ValidateInclusionMatcher.new(field)
49
- end
50
- end
51
- end
52
- end
@@ -1,86 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateLengthMatcher < HaveValidationMatcher
5
- def initialize(field)
6
- super(field, :length)
7
- end
8
-
9
- def with_minimum(value)
10
- @minimum = value
11
- self
12
- end
13
- alias :with_min :with_minimum
14
-
15
- def with_maximum(value)
16
- @maximum = value
17
- self
18
- end
19
- alias :with_max :with_maximum
20
-
21
- def within(value)
22
- @within = value
23
- self
24
- end
25
- alias :in :within
26
-
27
- def matches?(subject)
28
- return false unless @result = super(subject)
29
-
30
- check_minimum if @minimum
31
- check_maximum if @maximum
32
- check_range if @within
33
-
34
- @result
35
- end
36
-
37
- def description
38
- desc = []
39
- desc << " with minimum #{@minimum}" if @minimum
40
- desc << " with maximum #{@maximum}" if @maximum
41
- desc << " within range #{@within}" if @within
42
- super << desc.to_sentence
43
- end
44
-
45
- private
46
-
47
- def check_minimum
48
- actual = @validator.options[:minimum]
49
- if actual == @minimum
50
- @positive_message << " with minimum of #{actual}"
51
- else
52
- @negative_message << " with minimum of #{actual}"
53
- @result = false
54
- end
55
- end
56
-
57
- def check_maximum
58
- actual = @validator.options[:maximum]
59
- if actual == @maximum
60
- @positive_message << " with maximum of #{actual}"
61
- else
62
- @negative_message << " with maximum of #{actual}"
63
- @result = false
64
- end
65
- end
66
-
67
- def check_range
68
- min, max = [@within.min, @within.max]
69
- actual_min = @validator.options[:minimum]
70
- actual_max = @validator.options[:maximum]
71
-
72
- if actual_min == min && actual_max == max
73
- @positive_message << " with range #{@within}"
74
- else
75
- @negative_message << " with range #{actual_min..actual_max}"
76
- @result = false
77
- end
78
- end
79
- end
80
-
81
- def validate_length_of(field)
82
- ValidateLengthMatcher.new(field)
83
- end
84
- end
85
- end
86
- end
@@ -1,9 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- def validate_presence_of(field)
5
- HaveValidationMatcher.new(field, :presence)
6
- end
7
- end
8
- end
9
- end
@@ -1,64 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class ValidateUniquenessMatcher < HaveValidationMatcher
5
- def initialize(field)
6
- super(field, :uniqueness)
7
- end
8
-
9
- def case_insensitive
10
- @case_insensitive = true
11
- self
12
- end
13
-
14
- def scoped_to(*scope)
15
- @scope = [scope].flatten.map(&:to_s)
16
- self
17
- end
18
-
19
- def matches?(subject)
20
- return false unless @result = super(subject)
21
-
22
- check_scope if @scope
23
- check_case_sensivity if @case_insensitive
24
-
25
- @result
26
- end
27
-
28
- def description
29
- desc = []
30
- desc << " scoped to #{to_sentence(@scope)}" if @scope
31
- desc << ' allowing case insensitive values' if @case_insensitive
32
- super << desc.to_sentence
33
- end
34
-
35
- private
36
-
37
- def check_scope
38
- actual_scope = [@validator.options[:scope]].flatten.map(&:to_s)
39
- message = " scoped to #{to_sentence(actual_scope)}"
40
-
41
- if actual_scope == @scope
42
- @positive_message << message
43
- else
44
- @negative_message << message
45
- @result = false
46
- end
47
- end
48
-
49
- def check_case_sensivity
50
- if @validator.options[:case_sensitive] == false
51
- @positive_message << ' with case insensitive values'
52
- else
53
- @negative_message << ' without case insensitive values'
54
- @result = false
55
- end
56
- end
57
- end
58
-
59
- def validate_uniqueness_of(field)
60
- ValidateUniquenessMatcher.new(field)
61
- end
62
- end
63
- end
64
- end
@@ -1,91 +0,0 @@
1
- module Mongoid
2
- module Matchers
3
- module Validations
4
- class HaveValidationMatcher < Matcher
5
- def initialize(field, validation_type)
6
- @field = field.to_s
7
- @type = validation_type.to_s
8
- end
9
-
10
- def with_message(expected_message)
11
- @expected_message = expected_message
12
- self
13
- end
14
-
15
- def on(*contexts)
16
- @expected_on = clean_contexts(contexts)
17
- self
18
- end
19
-
20
- def matches?(subject)
21
- @klass = class_of(subject)
22
- @validator = detect_validator
23
- @result = true
24
-
25
- check_validator
26
- check_message if @expected_message
27
- check_on if @expected_on
28
-
29
- @result
30
- end
31
-
32
- def failure_message
33
- "#{@klass} to #{description}; instead got #{@negative_message}"
34
- end
35
-
36
- def negative_failure_message
37
- "#{@klass} to not #{description}; instead got #{@positive_message}"
38
- end
39
-
40
- def description
41
- desc = "validate #{@type.inspect} of #{@field.inspect}"
42
- desc << " with message: #{@expected_message.inspect}" if @expected_message
43
- desc << " on #{@expected_on.empty? ? 'all actions' : to_sentence(@expected_on)}" if @expected_on
44
-
45
- desc
46
- end
47
-
48
- private
49
-
50
- # Make sure contexts is always an array + normalize AR 2.x and AR 3.x differences
51
- def clean_contexts(contexts)
52
- [contexts].flatten.reject{|ctx| ctx == :save}.compact
53
- end
54
-
55
- def check_validator
56
- if @validator
57
- @negative_message = "#{@type.inspect} validator for #{@field.inspect}"
58
- @positive_message = "#{@type.inspect} validator for #{@field.inspect}"
59
- else
60
- @negative_message = "no #{@type.inspect} validator for #{@field.inspect}"
61
- @result = false
62
- end
63
- end
64
-
65
- def check_message
66
- error_message = @validator.options[:message]
67
- if @expected_message == error_message
68
- @positive_message << " with message: #{error_message.inspect}"
69
- else
70
- @negative_message << " with message: #{error_message.inspect}"
71
- @result = false
72
- end
73
- end
74
-
75
- def check_on
76
- on = clean_contexts(@validator.options[:on])
77
- if on.sort == @expected_on.sort
78
- @positive_message << " on #{on.empty? ? 'all actions' : to_sentence(on)}"
79
- else
80
- @negative_message << " on #{on.empty? ? 'all actions' : to_sentence(on)}"
81
- @result = false
82
- end
83
- end
84
-
85
- def detect_validator
86
- @klass.validators_on(@field).detect { |v| v.kind.to_s == @type }
87
- end
88
- end
89
- end
90
- end
91
- end