Module: Assert::Utils

Defined in:
lib/assert/utils.rb

Class Method Summary collapse

Class Method Details

.default_use_diff_procObject

Return true if if either show output has newlines or is bigger than 29 chars.



42
43
44
45
46
47
# File 'lib/assert/utils.rb', line 42

def self.default_use_diff_proc
  Proc.new do |exp_show_output, act_show_output|
    exp_show_output.include?("\n") || exp_show_output.size > 29 ||
    act_show_output.include?("\n") || act_show_output.size > 29
  end
end

.git_changed_procObject

use git to determine which files have changes



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/assert/utils.rb', line 66

def self.git_changed_proc
  Proc.new do |config, test_paths|
    files = []
    cmd =
      [
        # changed files
        "git diff --no-ext-diff --name-only #{config.changed_ref}",
        # added files
        "git ls-files --others --exclude-standard",
      ]
        .map{ |c| "#{c} -- #{test_paths.join(" ")}" }
        .join(" && ")
    Assert::CLI.bench("Load only changed files") do
      files = `#{cmd}`.split("\n")
    end
    puts Assert::CLI.debug_msg("  `#{cmd}`") if config.debug
    files
  end
end

.show(obj, config) ⇒ Object

show objects in a human-readable manner. Either inspects or pretty-prints them depending on settings.



9
10
11
12
13
# File 'lib/assert/utils.rb', line 9

def self.show(obj, config)
  out = config.pp_objects ? config.pp_proc.call(obj) : obj.inspect
  out = out.encode(Encoding.default_external) if defined?(Encoding)
  out
end

.show_for_diff(obj, config) ⇒ Object

show objects in a human-readable manner and make the output diff-able. This expands on the basic ‘show` util by escaping newlines and making object id hex-values generic.



18
19
20
21
22
# File 'lib/assert/utils.rb', line 18

def self.show_for_diff(obj, config)
  show(obj, config)
    .gsub(/\\n/, "\n")
    .gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
end

.stdlib_pp_proc(width = nil) ⇒ Object

Get a proc that uses stdlib ‘PP.pp` to pretty print objects



35
36
37
38
# File 'lib/assert/utils.rb', line 35

def self.stdlib_pp_proc(width = nil)
  require "pp"
  Proc.new{ |obj| PP.pp(obj, +"", width || 79).strip }
end

.syscmd_diff_proc(syscmd = "diff --unified=-1") ⇒ Object

use ‘diff` system cmd to show exp/act show output



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/assert/utils.rb', line 50

def self.syscmd_diff_proc(syscmd = "diff --unified=-1")
  Proc.new do |exp_show_output, act_show_output|
    result = ""
    tempfile("exp_show_output", exp_show_output) do |a|
      tempfile("act_show_output", act_show_output) do |b|
        result = `#{syscmd} #{a.path} #{b.path}`.strip
        result.sub!(/^\-\-\- .+/, "--- expected")
        result.sub!(/^\+\+\+ .+/, "+++ actual")
        result = "--- expected\n+++ actual" if result.empty?
      end
    end
    result
  end
end

.tempfile(name, content) ⇒ Object

open a tempfile and yield it



25
26
27
28
29
30
31
32
# File 'lib/assert/utils.rb', line 25

def self.tempfile(name, content)
  require "tempfile"
  Tempfile.open(name) do |tmpfile|
    tmpfile.puts(content)
    tmpfile.flush
    yield tmpfile if block_given?
  end
end