rspec-request_snapshot 0.1.2 → 0.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: ca942ac1703a10b7062475e78aa65cee5be244d06fa630bec9c6e317807e7cba
4
- data.tar.gz: 985cecfe4a2b42897cac99fcca8d2eabd473f6210b181cd1c4167e92d4c71ae0
3
+ metadata.gz: 63e2665c7ef3ad56f77f9cdf3d037e1c889a15218ffa8350929ba3fe77c63335
4
+ data.tar.gz: ab9c6146284efd2db0b2f9821160ccebc83a75ad40b1c1826e1c1c3b0c103573
5
5
  SHA512:
6
- metadata.gz: 410e38254160532d97af140d308742cc122cb84c234586d91f1f36fb31cb1ba557efbce1e4b298c3abfeba1794b0b7ab7faab81df3e0ab9a0741a7b547022b1e
7
- data.tar.gz: 407246c76b84a0b0b185cd2dfec465983cdfeb7ddfdc34197344a542aedbd79358fb0c18f78f6dc5549cefc989da022c127cc56f62e27a9be00b9dd41efd796d
6
+ metadata.gz: e9a7b6a9592f44ca0cab09167e15aaa90436935aab9f16989fe68488edebb0f22fd945a83c47bc8ccdba1b8c8c6733f1ae40fa337cd82f9572d308e0b7fd8939
7
+ data.tar.gz: 5af511e46ac7d7968970b4738e39a5d1657652492b9239a85136db6c5f705862bec92ed7a1b5211eb4ae3696f6c00986e2fa74248190acba7488ca0569f50170
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-request_snapshot (0.1.1)
4
+ rspec-request_snapshot (0.2.0)
5
5
  rspec (~> 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -59,6 +59,9 @@ expect(response.body).to match_snapshot("api/resources_index", format: :text)
59
59
 
60
60
  # Defining specific test dynamic attributes
61
61
  expect(response.body).to match_snapshot("api/resources_index", dynamic_attributes: %w(confirmed_at relation_id))
62
+
63
+ # Ignoring order for certain arrays (this will ignore the ordering for the countries array inside the json response)
64
+ expect(response.body).to match_snapshot("api/resources_index", ignore_order: %w(countries))
62
65
  ```
63
66
 
64
67
  ## Development
@@ -0,0 +1,12 @@
1
+ module Rspec::RequestSnapshot::Handlers
2
+ class Base
3
+ def initialize(options = {})
4
+ @options = options
5
+ end
6
+
7
+ def dynamic_attributes
8
+ @dynamic_attributes ||= RSpec.configuration.request_snapshots_dynamic_attributes |
9
+ Array(@options[:dynamic_attributes])
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,45 @@
1
+ class Rspec::RequestSnapshot::Handlers::JSON < Rspec::RequestSnapshot::Handlers::Base
2
+ def compare(actual, expected)
3
+ equal = true
4
+
5
+ actual.each do |key, value|
6
+ if actual[key].is_a?(Hash)
7
+ equal &= compare_hash(key, actual, expected)
8
+ elsif actual[key].is_a?(Array)
9
+ equal &= compare_array(key, actual, expected)
10
+ else
11
+ equal &= compare_value(key, actual, expected)
12
+ end
13
+ end
14
+
15
+ equal
16
+ end
17
+
18
+ def comparable(str)
19
+ JSON.parse(str)
20
+ end
21
+
22
+ def writable(str)
23
+ JSON.pretty_generate(JSON.parse(str))
24
+ end
25
+
26
+ private
27
+
28
+ def compare_hash(key, actual, expected)
29
+ actual[key].is_a?(Hash) && expected[key].is_a?(Hash) && compare(actual[key], expected[key])
30
+ end
31
+
32
+ def compare_array(key, actual, expected)
33
+ if actual[key].is_a?(Array) && expected[key].is_a?(Array)
34
+ if @options[:ignore_order] && @options[:ignore_order].include?(key)
35
+ actual[key].sort == expected[key].sort
36
+ else
37
+ actual[key] == expected[key]
38
+ end
39
+ end
40
+ end
41
+
42
+ def compare_value(key, actual ,expected)
43
+ dynamic_attributes.include?(key) ? true : actual[key] == expected[key]
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ class Rspec::RequestSnapshot::Handlers::Text < Rspec::RequestSnapshot::Handlers::Base
2
+ def compare(actual, expected)
3
+ actual == expected
4
+ end
5
+
6
+ def comparable(str)
7
+ str
8
+ end
9
+
10
+ def writable(str)
11
+ str
12
+ end
13
+ end
@@ -1,5 +1,3 @@
1
- require "json"
2
-
3
1
  module Rspec::RequestSnapshot
4
2
  RSpec::Matchers.define :match_snapshot do |snapshot_name, options|
5
3
  attr_reader :actual, :expected, :options
@@ -14,11 +12,12 @@ module Rspec::RequestSnapshot
14
12
  FileUtils.mkdir_p(File.dirname(snapshot_file_path)) unless Dir.exist?(File.dirname(snapshot_file_path))
15
13
 
16
14
  if File.exist?(snapshot_file_path) && !(ENV["REPLACE_SNAPSHOTS"] == "true")
17
- @actual = comparable(replace_dynamic_attributes(actual))
18
- @expected = comparable(replace_dynamic_attributes(File.read(snapshot_file_path)))
19
- @actual == @expected
15
+ @actual = handler.comparable(actual)
16
+ @expected = handler.comparable(File.read(snapshot_file_path))
17
+
18
+ handler.compare(@actual, @expected)
20
19
  else
21
- File.write(snapshot_file_path, writable(comparable(@actual)))
20
+ File.write(snapshot_file_path, handler.writable(actual))
22
21
  true
23
22
  end
24
23
  end
@@ -30,38 +29,20 @@ module Rspec::RequestSnapshot
30
29
  ].join("\n")
31
30
  end
32
31
 
33
- def comparable(str)
34
- case format
35
- when :text
36
- str
37
- when :json
38
- JSON.parse(str)
39
- end
40
- end
41
-
42
- def writable(str)
43
- case format
44
- when :text
45
- str
46
- when :json
47
- JSON.pretty_generate(str)
48
- end
49
- end
50
-
51
32
  def format
52
33
  @options[:format]&.to_sym || :json
53
34
  end
54
35
 
55
- def replace_dynamic_attributes(json)
56
- dynamic_attributes.each do |attribute|
57
- json.gsub!(/\"#{attribute}\":\s*(\".*?\"|\d+|\w+)/, "\"#{attribute}\":\"REPLACED\"")
36
+ def handler
37
+ @handler ||= begin
38
+ handler_class = case format
39
+ when :text
40
+ Handlers::Text
41
+ when :json
42
+ Handlers::JSON
43
+ end
44
+ handler_class.new(@options)
58
45
  end
59
- json
60
- end
61
-
62
- def dynamic_attributes
63
- @dynamic_attributes ||= RSpec.configuration.request_snapshots_dynamic_attributes |
64
- Array(options[:dynamic_attributes])
65
46
  end
66
47
  end
67
48
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module RequestSnapshot
3
- VERSION = "0.1.2".freeze
3
+ VERSION = "0.2.0".freeze
4
4
  end
5
5
  end
@@ -2,3 +2,6 @@ require "rspec"
2
2
  require "rspec/request_snapshot/version"
3
3
  require "rspec/request_snapshot/config"
4
4
  require "rspec/request_snapshot/matcher"
5
+ require "rspec/request_snapshot/handlers/base"
6
+ require "rspec/request_snapshot/handlers/json"
7
+ require "rspec/request_snapshot/handlers/text"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-request_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Campos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-03 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -87,6 +87,9 @@ files:
87
87
  - bin/setup
88
88
  - lib/rspec/request_snapshot.rb
89
89
  - lib/rspec/request_snapshot/config.rb
90
+ - lib/rspec/request_snapshot/handlers/base.rb
91
+ - lib/rspec/request_snapshot/handlers/json.rb
92
+ - lib/rspec/request_snapshot/handlers/text.rb
90
93
  - lib/rspec/request_snapshot/matcher.rb
91
94
  - lib/rspec/request_snapshot/version.rb
92
95
  - rspec-request_snapshot.gemspec