Module: Kaisoku::Snapshot::Inline
- Defined in:
- lib/kaisoku/snapshot/inline.rb
Class Method Summary collapse
- .matches?(actual, expected, location:) ⇒ Boolean
- .replace_argument(line, replacement) ⇒ Object
- .replace_heredoc(lines, index, terminator, actual) ⇒ Object
- .replace_in_lines(lines, index, actual) ⇒ Object
- .update(actual, location) ⇒ Object
- .update? ⇒ Boolean
Class Method Details
.matches?(actual, expected, location:) ⇒ Boolean
8 9 10 11 12 |
# File 'lib/kaisoku/snapshot/inline.rb', line 8 def matches?(actual, expected, location:) return update(actual, location) if update? actual == expected end |
.replace_argument(line, replacement) ⇒ Object
28 29 30 |
# File 'lib/kaisoku/snapshot/inline.rb', line 28 def replace_argument(line, replacement) line.sub(/match_inline_snapshot\((.*)\)/, "match_inline_snapshot(#{replacement})") end |
.replace_heredoc(lines, index, terminator, actual) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/kaisoku/snapshot/inline.rb', line 41 def replace_heredoc(lines, index, terminator, actual) finish = ((index + 1)...lines.length).find { |line_index| lines[line_index].strip == terminator } return unless finish indent = lines[index + 1][/^\s*/] || '' replacement = actual.lines.map { |line| "#{indent}#{line.chomp}\n" } lines[(index + 1)...finish] = replacement end |
.replace_in_lines(lines, index, actual) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/kaisoku/snapshot/inline.rb', line 32 def replace_in_lines(lines, index, actual) line = lines[index] if (match = line.match(/match_inline_snapshot\(<<~?['"]?([A-Z_]+)['"]?\)/)) replace_heredoc(lines, index, match[1], actual.to_s) else lines[index] = replace_argument(line, actual.inspect) end end |
.update(actual, location) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/kaisoku/snapshot/inline.rb', line 14 def update(actual, location) return true unless location && File.file?(location.path) lines = File.readlines(location.path) index = location.lineno - 1 replace_in_lines(lines, index, actual) File.write(location.path, lines.join) true end |
.update? ⇒ Boolean
24 25 26 |
# File 'lib/kaisoku/snapshot/inline.rb', line 24 def update? ENV['KAISOKU_UPDATE_SNAPSHOTS'] == '1' end |