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


38
39
40
41
42
43
# File 'lib/assert/utils.rb', line 38

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


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

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.


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
# 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


32
33
34
35
# File 'lib/assert/utils.rb', line 32

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


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

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


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

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