Module: WCC::JsonAPI::RSpec::SnapshotHelper
- Extended by:
- ActiveSupport::Concern, RSpec::Matchers::DSL
- Defined in:
- lib/wcc/jsonapi/rspec/snapshot_helper.rb
Constant Summary collapse
- MATCH_INLINE_SNAPSHOT_REGEXP =
/match\_inline\_snapshot(\(([\'\"\s]+|<<~\w+)?\))?\s*$/
Class Method Summary collapse
- .clear_write_queue! ⇒ Object
- .find_inline_call_spot(called_from) ⇒ Object
- .parse_actual(actual) ⇒ Object
- .write_queue ⇒ Object
Instance Method Summary collapse
- #differ ⇒ Object
- #load_snapshot(file_name) ⇒ Object
- #queue_write_inline_snapshot(filename, line_num, actual) ⇒ Object
- #snapshot_full_path(file_name) ⇒ Object
- #update_snapshots? ⇒ Boolean
- #write_all_inline_snapshots ⇒ Object
- #write_snapshot(file_name, actual) ⇒ Object
Class Method Details
.clear_write_queue! ⇒ Object
133 134 135 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 133 def clear_write_queue! @write_queue = {} end |
.find_inline_call_spot(called_from) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 137 def find_inline_call_spot(called_from) called_from.each do |line| next unless /\_spec\.rb/.match?(line) filename, line_num = line.split(':') line_num = line_num&.to_i next unless filename && line_num return [filename, line_num] end end |
.parse_actual(actual) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 149 def parse_actual(actual) case actual when String actual else JSON.pretty_generate(actual).strip end end |
.write_queue ⇒ Object
129 130 131 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 129 def write_queue @write_queue ||= {} end |
Instance Method Details
#differ ⇒ Object
68 69 70 71 72 73 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 68 def differ RSpec::Support::Differ.new( object_preparer: ->(object) { RSpec::Matchers::Composable.surface_descriptions_in(object) }, color: RSpec::Matchers.configuration.color?, ) end |
#load_snapshot(file_name) ⇒ Object
83 84 85 86 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 83 def load_snapshot(file_name) file = snapshot_full_path(file_name) return File.read(file) if File.exist?(file) end |
#queue_write_inline_snapshot(filename, line_num, actual) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 98 def queue_write_inline_snapshot(filename, line_num, actual) to_insert = actual.split("\n") # enqueue to the filename (WCC::JsonAPI::RSpec::SnapshotHelper.write_queue[filename] ||= []) << [line_num, to_insert] end |
#snapshot_full_path(file_name) ⇒ Object
79 80 81 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 79 def snapshot_full_path(file_name) File.join('spec/snapshots', file_name) end |
#update_snapshots? ⇒ Boolean
75 76 77 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 75 def update_snapshots? ActiveModel::Type::Boolean.new.cast(ENV['UPDATE_SNAPSHOTS']) end |
#write_all_inline_snapshots ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 106 def write_all_inline_snapshots WCC::JsonAPI::RSpec::SnapshotHelper.write_queue.each do |filename, queue| # rewrite in reverse order to preserve correct line numbers queue = queue.sort_by(&:first).reverse lines = File.readlines(filename) queue.each do |line_num, to_insert| # rewrite the lines idx = line_num - 1 lines[idx] = lines[idx].sub(MATCH_INLINE_SNAPSHOT_REGEXP, 'match_inline_snapshot <<~SNAP') lines = lines[0..idx] + to_insert + ['SNAP'] + lines[line_num..-1] end puts "SnapshotHelper: Writing new inline snapshot in #{filename}" File.open(filename, 'w') do |f| lines.each { |line| f.puts(line) } end end WCC::JsonAPI::RSpec::SnapshotHelper.clear_write_queue! end |
#write_snapshot(file_name, actual) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/wcc/jsonapi/rspec/snapshot_helper.rb', line 88 def write_snapshot(file_name, actual) file = snapshot_full_path(file_name) FileUtils.mkdir_p(File.dirname(file)) puts "SnapshotHelper: Writing new snapshot in #{file_name}" File.write(file, actual) end |