Class: Insta::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/insta/diff.rb,
sig/insta/diff.rbs

Class Method Summary collapse

Class Method Details

.caller_file_location(caller_line) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (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

Parameters:

  • (String)
  • (String)

Returns:

  • (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

Parameters:

  • (String)
  • (String)
  • file_extension: (String, nil) (defaults to: nil)

Returns:

  • (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

.differDifftastic::Differ

: () -> Difftastic::Differ

Returns:



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

Parameters:

  • (String)
  • (String)
  • (String)
  • (String, nil)
  • (String, nil)
  • file_extension: (String, nil) (defaults to: nil)

Returns:

  • (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.failure_message(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}"

  message = +""
  message << "#{reset}\n"
  message << "Snapshot for #{cyan}\"#{test_name}\"#{reset} didn't match.\n"
  message << "\n"
  message << "  #{dim}Snapshot:#{reset} #{snapshot_path}\n" if snapshot_path
  message << "  #{dim}Test:#{reset}     #{caller_file_location(line)}\n" if line
  message << "\n"
  message << "#{divider}\n"
  message << diff_with_language(expected, actual, file_extension: file_extension)
  message << "\n#{divider}\n"
  message << "\n"

  if line
    message << "  #{dim}Update snapshot and re-run this test:#{reset}\n"
    message << "  #{cyan}INSTA_UPDATE=force #{run_test_command(line)}#{reset}\n"
    message << "\n"
  end

  message << "  #{dim}Interactively review all pending snapshots:#{reset}\n"
  message << "  #{cyan}bundle exec insta review#{reset}\n"
  message << "\n#{divider}\n"
  message << reset

  message
end

.minitest_cli?Boolean

: () -> bool

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Parameters:

  • (String)

Returns:

  • (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_widthInteger

: () -> Integer

Returns:

  • (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