pathway 0.11.0 → 0.11.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27b3570a78198f711d1d781ec9f48cdf2cbe8846034abda268f5137f6c88f2fe
4
- data.tar.gz: d10a76ae2edb906058ac291a8e106f05457bf89ce86558355f331ddbc058b5d1
3
+ metadata.gz: bb77df296a81bb4d0d277c4c802e990727f58e9c6fd5dee20ca3e0986aa954f2
4
+ data.tar.gz: 6921ad015f25c29bf669d815b86518eba9c11938da78f8f37aec9ab6d9a3ed4f
5
5
  SHA512:
6
- metadata.gz: 751de3262b354b783d190959c2c72d251c73226886d4e808fe47421283e93961fd124d51dd65bb52df206532e24c3daf007487df384c0f45e1ce323d7cc1d537
7
- data.tar.gz: b21d812ee34395e29d160613e791fb0f2616351da56445653623429fbb9da091d8dba6d64bc2123de51799022aa9182a157ab985a848f68977a37d754e86c5b0
6
+ metadata.gz: 1f1d59e04131f194731a6890aa40cf498df3bd15fbe65818ed6e90b43e3a96a1ef94d647737bd94fa8e1e43eef3583a3bcc04df03c7e45ea155f7e6f998610a1
7
+ data.tar.gz: 222112d0f9a8a7c23499afa2d656f961fdef7e32df3de6c165e55fb81357d2db27605ea3882d1d7d2c1886165f25b27d9d272d6c493cd5fd5bfe5894ff25ae18
@@ -1,3 +1,7 @@
1
+ ## [0.11.1] - 2020-01-09
2
+ ### Changed
3
+ - Improve custom `rspec` matchers for testing field presence on schemas
4
+
1
5
  ## [0.11.0] - 2020-01-02
2
6
  ### Changed
3
7
  - Add support for `dry-validation` 1.0 and above
@@ -6,27 +6,38 @@ RSpec::Matchers.define :accept_optional_fields do |*fields|
6
6
  match do |form|
7
7
  @form, @fields = form, fields
8
8
 
9
- not_defined.empty? && not_optional.empty?
9
+ not_defined.empty? &&
10
+ not_optional.empty? &&
11
+ allowing_null_values_matches? &&
12
+ not_allowing_null_values_matches?
10
13
  end
11
14
 
12
15
  match_when_negated do |form|
16
+ raise NotImplementedError, 'expect().not_to accept_optional_fields.not_allowing_null_values is not supported.' if @allowing_null_values || @not_allowing_null_values
17
+
13
18
  @form, @fields = form, fields
14
19
 
15
20
  not_defined.empty? && optional.empty?
16
21
  end
17
22
 
18
23
  description do
19
- "accept #{field_list} as optional #{pluralize_fields}"
24
+ null_value_allowed = @allowing_null_values ? ' allowing null values' : ''
25
+ null_value_disallowed = @not_allowing_null_values ? ' not allowing null values' : ''
26
+ "accept #{field_list} as optional #{pluralize_fields}#{null_value_allowed}#{null_value_disallowed}"
20
27
  end
21
28
 
22
29
  failure_message do
23
- "Expected to accept #{field_list} as optional #{pluralize_fields} but " +
24
- [not_optional_list, not_defined_list].compact.join("; and ")
30
+ null_value_allowed = @allowing_null_values ? ' allowing null values' : ''
31
+ null_value_disallowed = @not_allowing_null_values ? ' not allowing null values' : ''
32
+
33
+ "Expected to accept #{field_list} as optional #{pluralize_fields}#{null_value_allowed}#{null_value_disallowed} but " +
34
+ as_sentence([not_optional_list, not_defined_list, accepting_null_list, not_accepting_null_list].compact,
35
+ connector: '; ', last_connector: '; and ')
25
36
  end
26
37
 
27
38
  failure_message_when_negated do
28
39
  "Did not expect to accept #{field_list} as optional #{pluralize_fields} but " +
29
- [optional_list, not_defined_list].compact.join("; and ")
40
+ [optional_list, not_defined_list].compact.join('; and ')
30
41
  end
31
42
 
32
43
  include Pathway::Rspec::FormSchemaHelpers
@@ -36,7 +47,19 @@ RSpec::Matchers.define :accept_optional_fields do |*fields|
36
47
  end
37
48
 
38
49
  def not_optional_list
39
- "#{as_list(not_required)} #{were_was(not_required)} not optional" if not_optional.any?
50
+ "#{as_list(not_optional)} #{were_was(not_optional)} not optional" if not_optional.any?
51
+ end
52
+
53
+ chain :allowing_null_values do
54
+ fail 'cannot use allowing_null_values and not_allowing_null_values at the same time' if @not_allowing_null_values
55
+
56
+ @allowing_null_values = true
57
+ end
58
+
59
+ chain :not_allowing_null_values do
60
+ fail 'cannot use allowing_null_values and not_allowing_null_values at the same time' if @allowing_null_values
61
+
62
+ @not_allowing_null_values = true
40
63
  end
41
64
  end
42
65
 
@@ -21,6 +21,14 @@ module Pathway
21
21
  "#{as_list(not_defined)} #{were_was(not_defined)} not defined" if not_defined.any?
22
22
  end
23
23
 
24
+ def accepting_null_list
25
+ "#{as_list(null_value_allowed)} #{were_was(null_value_allowed)} accepting null values" if @not_allowing_null_values && null_value_allowed.any?
26
+ end
27
+
28
+ def not_accepting_null_list
29
+ "#{as_list(null_value_disallowed)} #{were_was(null_value_disallowed)} not accepting null values" if @allowing_null_values && null_value_disallowed.any?
30
+ end
31
+
24
32
  def required
25
33
  @required ||= @fields.select { |field| required?(field) }
26
34
  end
@@ -29,12 +37,20 @@ module Pathway
29
37
  @optional ||= @fields.select { |field| optional?(field) }
30
38
  end
31
39
 
40
+ def null_value_allowed
41
+ @null_value_allowed ||= @fields.select { |field| null_value_allowed?(field) }
42
+ end
43
+
44
+ def null_value_disallowed
45
+ @null_value_disallowed ||= @fields.select { |field| null_value_disallowed?(field) }
46
+ end
47
+
32
48
  def not_required
33
49
  @not_required ||= defined - required
34
50
  end
35
51
 
36
52
  def not_optional
37
- @not_required ||= defined - optional
53
+ @not_optional ||= defined - optional
38
54
  end
39
55
 
40
56
  def not_defined
@@ -60,6 +76,26 @@ module Pathway
60
76
  left.type == :predicate && left.name == :key? && left.args.first == field
61
77
  end
62
78
  end
79
+
80
+ def allowing_null_values_matches?
81
+ @allowing_null_values ? @fields.all? { |field| null_value_allowed?(field) } : true
82
+ end
83
+
84
+ def not_allowing_null_values_matches?
85
+ @not_allowing_null_values ? @fields.all? { |field| null_value_disallowed?(field) } : true
86
+ end
87
+
88
+ def null_value_allowed?(field)
89
+ rule = rules[field]&.right&.rule
90
+ predicate = rule&.left
91
+ predicate.present? && predicate.type == :not && predicate.rules&.first&.name == :nil?
92
+ end
93
+
94
+ def null_value_disallowed?(field)
95
+ rule = rules[field]&.right&.rule
96
+ predicate = rule&.left
97
+ predicate.present? && predicate.type == :predicate && predicate.name == :filled?
98
+ end
63
99
  end
64
100
  end
65
101
  end
@@ -3,8 +3,8 @@
3
3
  module Pathway
4
4
  module Rspec
5
5
  module ListHelpers
6
- def as_list(items)
7
- as_sentence(items.map(&:inspect))
6
+ def as_list(items, **kwargs)
7
+ as_sentence(items.map(&:inspect), **kwargs)
8
8
  end
9
9
 
10
10
  def as_sentence(items, connector: ', ', last_connector: ' and ')
@@ -6,27 +6,38 @@ RSpec::Matchers.define :require_fields do |*fields|
6
6
  match do |form|
7
7
  @form, @fields = form, fields
8
8
 
9
- not_defined.empty? && not_required.empty?
9
+ not_defined.empty? &&
10
+ not_required.empty? &&
11
+ allowing_null_values_matches? &&
12
+ not_allowing_null_values_matches?
10
13
  end
11
14
 
12
15
  match_when_negated do |form|
16
+ raise NotImplementedError, 'expect().not_to require_fields.not_allowing_null_values is not supported.' if @allowing_null_values || @not_allowing_null_values
17
+
13
18
  @form, @fields = form, fields
14
19
 
15
20
  not_defined.empty? && required.empty?
16
21
  end
17
22
 
18
23
  description do
19
- "require #{field_list} as #{pluralize_fields}"
24
+ null_value_allowed = @allowing_null_values ? ' allowing null values' : ''
25
+ null_value_disallowed = @not_allowing_null_values ? ' not allowing null values' : ''
26
+ "require #{field_list} as #{pluralize_fields}#{null_value_allowed}#{null_value_disallowed}"
20
27
  end
21
28
 
22
29
  failure_message do
23
- "Expected to require #{field_list} as #{pluralize_fields} but " +
24
- [not_required_list, not_defined_list].compact.join("; and ")
30
+ null_value_allowed = @allowing_null_values ? ' allowing null values' : ''
31
+ null_value_disallowed = @not_allowing_null_values ? ' not allowing null values' : ''
32
+
33
+ "Expected to require #{field_list} as #{pluralize_fields}#{null_value_allowed}#{null_value_disallowed} but " +
34
+ as_sentence([not_required_list, not_defined_list, accepting_null_list, not_accepting_null_list].compact,
35
+ connector: '; ', last_connector: '; and ')
25
36
  end
26
37
 
27
38
  failure_message_when_negated do
28
39
  "Did not expect to require #{field_list} as #{pluralize_fields} but " +
29
- [required_list, not_defined_list].compact.join("; and ")
40
+ [required_list, not_defined_list].compact.join('; and ')
30
41
  end
31
42
 
32
43
  include Pathway::Rspec::FormSchemaHelpers
@@ -38,6 +49,18 @@ RSpec::Matchers.define :require_fields do |*fields|
38
49
  def not_required_list
39
50
  "#{as_list(not_required)} #{were_was(not_required)} not required" if not_required.any?
40
51
  end
52
+
53
+ chain :allowing_null_values do
54
+ fail 'cannot use allowing_null_values and not_allowing_null_values at the same time' if @not_allowing_null_values
55
+
56
+ @allowing_null_values = true
57
+ end
58
+
59
+ chain :not_allowing_null_values do
60
+ fail 'cannot use allowing_null_values and not_allowing_null_values at the same time' if @allowing_null_values
61
+
62
+ @not_allowing_null_values = true
63
+ end
41
64
  end
42
65
 
43
66
  RSpec::Matchers.alias_matcher :require_field, :require_fields
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pathway
4
- VERSION = '0.11.0'
4
+ VERSION = '0.11.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pathway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Herrero
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-02 00:00:00.000000000 Z
11
+ date: 2020-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-inflector