Class: Insta::Diff
- Inherits:
-
Object
- Object
- Insta::Diff
- Defined in:
- lib/insta/diff.rb,
sig/insta/diff.rbs
Class Method Summary collapse
-
.caller_file_location(caller_line) ⇒ String
: (String) -> String.
-
.diff(expected, actual) ⇒ String
: (String, String) -> String.
-
.diff_with_language(expected, actual, file_extension: nil) ⇒ String
: (String, String, ?file_extension: String?) -> String.
-
.differ ⇒ Difftastic::Differ
: () -> Difftastic::Differ.
-
.failure_message(expected, actual, test_name, snapshot_path = nil, line = nil, file_extension: nil) ⇒ String
: (String, String, String, ?String?, ?String?, ?file_extension: String?) -> String.
-
.minitest_cli? ⇒ Boolean
: () -> bool.
-
.mtest? ⇒ Boolean
: () -> bool.
-
.rails? ⇒ Boolean
: () -> bool.
-
.rspec? ⇒ Boolean
: () -> bool.
-
.run_test_command(caller_line) ⇒ String
: (String) -> String.
-
.terminal_width ⇒ Integer
: () -> Integer.
Class Method Details
.caller_file_location(caller_line) ⇒ String
: (String) -> String
68 69 70 71 72 |
# File 'lib/insta/diff.rb', line 68 def self.caller_file_location(caller_line) file, lineno = caller_line.split(":", 3).first(2) file && lineno ? "#{file}:#{lineno}" : caller_line end |
.diff(expected, actual) ⇒ String
: (String, String) -> String
21 22 23 |
# File 'lib/insta/diff.rb', line 21 def self.diff(expected, actual) differ.diff_strings(expected, actual) end |
.diff_with_language(expected, actual, file_extension: nil) ⇒ String
: (String, String, ?file_extension: String?) -> String
26 27 28 29 30 31 32 |
# File 'lib/insta/diff.rb', line 26 def self.diff_with_language(expected, actual, file_extension: nil) if file_extension differ.diff_strings(expected, actual, file_extension: file_extension) else differ.diff_strings(expected, actual) end end |
.differ ⇒ Difftastic::Differ
: () -> Difftastic::Differ
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/insta/diff.rb', line 8 def self.differ config = Insta.configuration Difftastic::Differ.new( color: config.resolved_diff_color.to_sym, display: config.resolved_diff_display, width: config.diff_width, left_label: "Expected", right_label: "Actual" ) end |
.failure_message(expected, actual, test_name, snapshot_path = nil, line = nil, file_extension: nil) ⇒ String
: (String, String, String, ?String?, ?String?, ?file_extension: String?) -> String
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/insta/diff.rb', line 35 def self.(expected, actual, test_name, snapshot_path = nil, line = nil, file_extension: nil) dim = "\e[2m" cyan = "\e[36m" reset = "\e[0m" divider = "#{dim}#{"─" * terminal_width}#{reset}" = +"" << "#{reset}\n" << "Snapshot for #{cyan}\"#{test_name}\"#{reset} didn't match.\n" << "\n" << " #{dim}Snapshot:#{reset} #{snapshot_path}\n" if snapshot_path << " #{dim}Test:#{reset} #{caller_file_location(line)}\n" if line << "\n" << "#{divider}\n" << diff_with_language(expected, actual, file_extension: file_extension) << "\n#{divider}\n" << "\n" if line << " #{dim}Update snapshot and re-run this test:#{reset}\n" << " #{cyan}INSTA_UPDATE=force #{run_test_command(line)}#{reset}\n" << "\n" end << " #{dim}Interactively review all pending snapshots:#{reset}\n" << " #{cyan}bundle exec insta review#{reset}\n" << "\n#{divider}\n" << reset end |
.minitest_cli? ⇒ Boolean
: () -> bool
106 107 108 109 110 |
# File 'lib/insta/diff.rb', line 106 def self.minitest_cli? return @minitest_cli if defined?(@minitest_cli) @minitest_cli = !!(defined?(::Minitest::VERSION) && Gem::Version.new(::Minitest::VERSION) >= Gem::Version.new("6.0")) end |
.mtest? ⇒ Boolean
: () -> bool
113 114 115 116 117 118 119 |
# File 'lib/insta/diff.rb', line 113 def self.mtest? return @mtest if defined?(@mtest) spec = Gem.loaded_specs["maxitest"] @mtest = !minitest_cli? && spec.is_a?(Gem::Specification) && spec.version < Gem::Version.new("7.0") end |
.rails? ⇒ Boolean
: () -> bool
99 100 101 102 103 |
# File 'lib/insta/diff.rb', line 99 def self.rails? return @rails if defined?(@rails) @rails = !!(defined?(::Rails) && defined?(::ActiveSupport::TestCase)) end |
.rspec? ⇒ Boolean
: () -> bool
92 93 94 95 96 |
# File 'lib/insta/diff.rb', line 92 def self.rspec? return @rspec if defined?(@rspec) @rspec = !!defined?(::RSpec) end |
.run_test_command(caller_line) ⇒ String
: (String) -> String
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/insta/diff.rb', line 75 def self.run_test_command(caller_line) file, lineno = caller_line.split(":", 3).first(2) if rspec? "bundle exec rspec #{file}:#{lineno}" elsif rails? "bin/rails test #{file}:#{lineno}" elsif minitest_cli? "bundle exec minitest #{file}:#{lineno}" elsif mtest? "bundle exec mtest #{file}:#{lineno}" else "bundle exec ruby -Itest #{file}" end end |
.terminal_width ⇒ Integer
: () -> Integer
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/insta/diff.rb', line 122 def self.terminal_width if $stdout.tty? begin `tput cols`.strip.to_i rescue StandardError 80 end else 80 end end |