rspec_jsonapi_serializer 1.1.2 → 1.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
  SHA256:
3
- metadata.gz: 61f6a27fca68262e1efe72673c7519610c53c72de7f5aeb393c9ad5b32864bfd
4
- data.tar.gz: 7f5745572ef15b1442df7d055802f5ffec3e5aac01c14b92e97e80db827f6387
3
+ metadata.gz: de385c644fa13cd583e2c85c16bf015e883e616acd9a1c2f97ad4715e1280fbb
4
+ data.tar.gz: be5bb549771e9a1fdad1eebeccf6681fedb5fb1d146458a0ed5f64b6fed0c1f8
5
5
  SHA512:
6
- metadata.gz: 8f35c497af303a5802f192639b5d36997d6ea76626d7229cbc9d1990882f7163bdb5f6ac12e4f93e2990a95d9c21888ffa465e02ac5edc0a414ced5ef0c9dadf
7
- data.tar.gz: 7c0ba61d35ad01c788164f650a60b0d532f856cac08f05f974c33c04309b92d7656c776452d7b83418580c700b6f5accab09b6e8d8a7d40c923f848c73e87b29
6
+ metadata.gz: be7897f9eeb88466697814a55c599ea43102bb10d30ebfe8e2913196c478ef5e28e5f7136de9ab84c36adf092dba15761f7561146496382e9b38a662bb9b431c
7
+ data.tar.gz: 6e67c25b2d7e37da12976f1c4973ff3579a82f707057162e32a68a2a997d626679b3705a15a937c0d51c0e3eeeba7a065bcd84f3967b149041fef0aa777fc28e
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "rspec_jsonapi_serializer/matchers/base"
4
4
  require "rspec_jsonapi_serializer/matchers/association_matchers/serializer_matcher"
5
+ require "rspec_jsonapi_serializer/matchers/association_matchers/id_method_name_matcher"
5
6
  require "rspec_jsonapi_serializer/metadata/relationships"
6
7
 
7
8
  module RSpecJSONAPISerializer
@@ -26,32 +27,22 @@ module RSpecJSONAPISerializer
26
27
  self
27
28
  end
28
29
 
29
- def description
30
- description = "#{association_message} #{expected}"
30
+ def id_method_name(value)
31
+ add_submatcher AssociationMatchers::IdMethodNameMatcher.new(value, expected)
31
32
 
32
- [description, submatchers.map(&:description)].flatten.join(' ')
33
- end
34
-
35
- def failure_message
36
- "Expected #{expectation}"
33
+ self
37
34
  end
38
35
 
39
- def failure_message_when_negated
40
- "Did not expect #{expectation}"
36
+ def main_failure_message
37
+ [expected_message, actual_message].compact.join(", ")
41
38
  end
42
39
 
43
40
  private
44
41
 
45
42
  attr_reader :relationship_matcher, :relationship_type
46
43
 
47
- def expectation
48
- expectation = "#{serializer_name} to #{association_message} #{expected}"
49
-
50
- submatchers_expectations = failing_submatchers.map do |submatcher|
51
- "(#{submatcher.expectation})"
52
- end.compact.join(", ")
53
-
54
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
44
+ def expected_message
45
+ "expected #{serializer_name} to #{association_message} #{expected}"
55
46
  end
56
47
 
57
48
  def relationship_matches?
@@ -63,7 +54,7 @@ module RSpecJSONAPISerializer
63
54
  end
64
55
 
65
56
  def association_message
66
- relationship_matcher.to_s.split("_").join(" ")
57
+ relationship_matcher.to_s.split('_')
67
58
  end
68
59
 
69
60
  def actual
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/metadata/relationships"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ module AssociationMatchers
8
+ class IdMethodNameMatcher < Base
9
+ def initialize(value, relationship_target)
10
+ super(value)
11
+
12
+ @relationship_target = relationship_target
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ actual == expected
19
+ end
20
+
21
+ def main_failure_message
22
+ [expected_message, actual_message].compact.join(", ")
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :relationship_target
28
+
29
+ def expected_message
30
+ "expected #{serializer_name} to use #{expected} as id_method_name for #{relationship_target}"
31
+ end
32
+
33
+ def actual_message
34
+ actual ? "got #{actual} instead" : nil
35
+ end
36
+
37
+ def actual
38
+ metadata.relationship(relationship_target).id_method_name
39
+ end
40
+
41
+ def metadata
42
+ Metadata::Relationships.new(serializer_instance)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -18,18 +18,18 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "with serializer #{expected}"
23
- end
24
-
25
- def expectation
26
- [ "with serializer #{expected}", actual_message ].compact.join(", ")
21
+ def main_failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :relationship_target
32
28
 
29
+ def expected_message
30
+ "expected #{serializer_name} to use #{expected} as serializer for #{relationship_target}"
31
+ end
32
+
33
33
  def actual_message
34
34
  actual ? "got #{actual} instead" : nil
35
35
  end
@@ -13,17 +13,27 @@ module RSpecJSONAPISerializer
13
13
  end
14
14
 
15
15
  def failure_message
16
- raise NotImplementedError
16
+ ([main_failure_message] + submatcher_failure_messages).compact.join("\n")
17
17
  end
18
18
 
19
19
  def failure_message_when_negated
20
- raise NotImplementedError
20
+ ([main_failure_message_when_negated] + submatcher_failure_messages_when_negated)
21
+ .compact
22
+ .join("\n")
21
23
  end
22
24
 
23
25
  protected
24
26
 
25
27
  attr_reader :expected, :serializer_instance, :submatchers
26
28
 
29
+ def main_failure_message
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def main_failure_message_when_negated
34
+ raise NotImplementedError
35
+ end
36
+
27
37
  def add_submatcher(submatcher)
28
38
  submatchers << submatcher
29
39
  end
@@ -40,6 +50,16 @@ module RSpecJSONAPISerializer
40
50
  serializer_instance.class.name
41
51
  end
42
52
 
53
+ private
54
+
55
+ def submatcher_failure_messages
56
+ failing_submatchers.map(&:failure_message)
57
+ end
58
+
59
+ def submatcher_failure_messages_when_negated
60
+ failing_submatchers.map(&:failure_message_when_negated)
61
+ end
62
+
43
63
  def failing_submatchers
44
64
  @failing_submatchers ||= submatchers.select do |submatcher|
45
65
  !submatcher.matches?(serializer_instance)
@@ -13,20 +13,16 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def serializer(value)
17
- association_matcher.serializer(value)
18
- end
19
-
20
- def description
21
- association_matcher.description
16
+ def id_method_name(value)
17
+ association_matcher.id_method_name(value)
22
18
  end
23
19
 
24
- def failure_message
25
- association_matcher.failure_message
20
+ def serializer(value)
21
+ association_matcher.serializer(value)
26
22
  end
27
23
 
28
- def failure_message_when_negated
29
- association_matcher.failure_message_when_negated
24
+ def main_failure_message
25
+ association_matcher.main_failure_message
30
26
  end
31
27
 
32
28
  private
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "have attribute #{expected}"
22
+ add_submatcher HaveAttributeMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have attribute #{expected}." unless has_attribute?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to have attribute #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def attributes
52
34
  @attributes ||= serializer_instance.class.try(:attributes_to_serialize) || {}
53
35
  end
@@ -18,30 +18,20 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :attribute
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize #{attribute} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
- "got #{value_to_string(actual)} instead" if attributes.has_key?(attribute)
39
- end
40
-
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if attributes.has_key?(attribute)
45
35
  end
46
36
 
47
37
  def actual
@@ -11,7 +11,7 @@ module RSpecJSONAPISerializer
11
11
  actual == expected
12
12
  end
13
13
 
14
- def failure_message
14
+ def main_failure_message
15
15
  "expected that #{serializer_name} to have id :#{expected}, got :#{actual} instead"
16
16
  end
17
17
 
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "have link #{expected}"
22
+ add_submatcher HaveLinkMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have link #{expected}." unless has_link?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to have link #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def has_link?
52
34
  links.has_key?(expected)
53
35
  end
@@ -18,30 +18,20 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :link
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize link #{link} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
- "got #{value_to_string(actual)} instead" if links.has_key?(link)
39
- end
40
-
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if links.has_key?(link)
45
35
  end
46
36
 
47
37
  def actual
@@ -13,20 +13,16 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def serializer(value)
17
- association_matcher.serializer(value)
18
- end
19
-
20
- def description
21
- association_matcher.description
16
+ def id_method_name(value)
17
+ association_matcher.id_method_name(value)
22
18
  end
23
19
 
24
- def failure_message
25
- association_matcher.failure_message
20
+ def serializer(value)
21
+ association_matcher.serializer(value)
26
22
  end
27
23
 
28
- def failure_message_when_negated
29
- association_matcher.failure_message_when_negated
24
+ def main_failure_message
25
+ association_matcher.main_failure_message
30
26
  end
31
27
 
32
28
  private
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "serialize meta #{expected}"
22
+ add_submatcher HaveMetaMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to serialize meta #{expected}." unless has_meta?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to serialize meta #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def metas
52
34
  @metas ||= serializable_hash.dig(:data, :meta) || {}
53
35
  end
@@ -18,33 +18,22 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :meta
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize meta #{meta} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
34
  "got #{actual.nil? ? 'nil' : actual} instead" if metas.has_key?(meta)
39
35
  end
40
36
 
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
45
- end
46
-
47
-
48
37
  def actual
49
38
  metas[meta]
50
39
  end
@@ -13,20 +13,16 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def serializer(value)
17
- association_matcher.serializer(value)
18
- end
19
-
20
- def description
21
- association_matcher.description
16
+ def id_method_name(value)
17
+ association_matcher.id_method_name(value)
22
18
  end
23
19
 
24
- def failure_message
25
- association_matcher.failure_message
20
+ def serializer(value)
21
+ association_matcher.serializer(value)
26
22
  end
27
23
 
28
- def failure_message_when_negated
29
- association_matcher.failure_message_when_negated
24
+ def main_failure_message
25
+ association_matcher.main_failure_message
30
26
  end
31
27
 
32
28
  private
@@ -11,7 +11,7 @@ module RSpecJSONAPISerializer
11
11
  actual == expected
12
12
  end
13
13
 
14
- def failure_message
14
+ def main_failure_message
15
15
  "expected that #{serializer_name} to have type :#{expected}, got :#{actual} instead"
16
16
  end
17
17
 
@@ -15,6 +15,8 @@ module RSpecJSONAPISerializer
15
15
  # expect(serializer).to belong_to(:team)
16
16
  # If you have a custom serializer, you can assert its value with the `serializer` submatcher
17
17
  # expect(serializer).to belong_to(:team).serializer(TeamSerializer)
18
+ # If you have a custom id, you can assert its value with the `id_method_name` submatcher
19
+ # expect(serializer).to belong_to(:team).id_method_name(:team_slug)
18
20
  def belong_to(expected)
19
21
  BelongToMatcher.new(expected)
20
22
  end
@@ -49,6 +51,8 @@ module RSpecJSONAPISerializer
49
51
  # expect(serializer).to have_many(:teams)
50
52
  # If you have a custom serializer, you can assert its value with the `serializer` submatcher
51
53
  # expect(serializer).to have_many(:teams).serializer(TeamSerializer)
54
+ # If you have a custom id, you can assert its value with the `id_method_name` submatcher
55
+ # expect(serializer).to have_many(:teams).id_method_name(:team_slugs)
52
56
  def have_many(expected)
53
57
  HaveManyMatcher.new(expected)
54
58
  end
@@ -67,6 +71,8 @@ module RSpecJSONAPISerializer
67
71
  # expect(serializer).to have_one(:team)
68
72
  # If you have a custom serializer, you can assert its value with the `serializer` submatcher
69
73
  # expect(serializer).to have_one(:team).serializer(TeamSerializer)
74
+ # If you have a custom id, you can assert its value with the `id_method_name` submatcher
75
+ # expect(serializer).to have_one(:team).id_method_name(:team_slug)
70
76
  def have_one(expected)
71
77
  HaveOneMatcher.new(expected)
72
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RSpecJSONAPISerializer
4
- VERSION = "1.1.2"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_jsonapi_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateus Cruz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-21 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializer
@@ -34,6 +34,7 @@ files:
34
34
  - lib/rspec_jsonapi_serializer.rb
35
35
  - lib/rspec_jsonapi_serializer/matchers.rb
36
36
  - lib/rspec_jsonapi_serializer/matchers/association_matcher.rb
37
+ - lib/rspec_jsonapi_serializer/matchers/association_matchers/id_method_name_matcher.rb
37
38
  - lib/rspec_jsonapi_serializer/matchers/association_matchers/serializer_matcher.rb
38
39
  - lib/rspec_jsonapi_serializer/matchers/base.rb
39
40
  - lib/rspec_jsonapi_serializer/matchers/belong_to_matcher.rb
@@ -71,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
72
  - !ruby/object:Gem::Version
72
73
  version: '0'
73
74
  requirements: []
74
- rubygems_version: 3.3.7
75
+ rubygems_version: 3.2.16
75
76
  signing_key:
76
77
  specification_version: 4
77
78
  summary: RSpec matchers for jsonapi-serializer.