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



36
37
38
39
40
41
# File 'lib/assert/utils.rb', line 36

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/assert/utils.rb', line 60

def self.git_changed_proc
  Proc.new do |config, test_paths|
    files = []
    cmd = [
      "git diff --no-ext-diff --name-only #{config.changed_ref}", # changed files
      "git ls-files --others --exclude-standard"                  # added files
    ].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.



7
8
9
10
11
# File 'lib/assert/utils.rb', line 7

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.



16
17
18
# File 'lib/assert/utils.rb', line 16

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



30
31
32
33
# File 'lib/assert/utils.rb', line 30

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/assert/utils.rb', line 44

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



21
22
23
24
25
26
27
# File 'lib/assert/utils.rb', line 21

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