Module: Oktest::Util

Defined in:
lib/oktest.rb

Class Method Summary collapse

Class Method Details

._text2lines(text, no_newline_msg = nil) ⇒ Object



1774
1775
1776
1777
1778
1779
# File 'lib/oktest.rb', line 1774

def _text2lines(text, no_newline_msg=nil)
  lines = []
  text.each_line {|line| line.chomp!; lines << line }
  lines[-1] << no_newline_msg if no_newline_msg && text[-1] && text[-1] != ?\n
  return lines
end

.diff_unified(text_old, text_new, label = "--- old\n+++ new\n", context = 3) ⇒ Object

platform depend, but not require extra library



1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
# File 'lib/oktest.rb', line 1808

def diff_unified(text_old, text_new, label="--- old\n+++ new\n", context=3)
  #; [!ulyq5] returns unified diff string of two text strings.
  #; [!6tgum] detects whether char at end of file is newline or not.
  tmp_old = "_tmp.old.#{rand()}"
  tmp_new = "_tmp.new.#{rand()}"
  File.open(tmp_old, 'w') {|f| f.write(text_old) }
  File.open(tmp_new, 'w') {|f| f.write(text_new) }
  begin
    #diff = `diff -u #{tmp_old} #{tmp_new}`
    diff = `diff --unified=#{context} #{tmp_old} #{tmp_new}`
  ensure
    File.unlink(tmp_old)
    File.unlink(tmp_new)
  end
  diff.sub!(/\A\-\-\-.*\n\+\+\+.*\n/, label.to_s)
  return diff
end

.file_line(filename, linenum) ⇒ Object



1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/oktest.rb', line 1710

def file_line(filename, linenum)
  #; [!4z65g] returns nil if file not exist or not a file.
  return nil unless File.file?(filename)
  #; [!4a2ji] caches recent file content for performance reason.
  @__cache ||= [nil, []]
  if @__cache[0] != filename
    #; [!wtrl5] recreates cache data if other file requested.
    @__cache[0] = filename
    @__cache[1].clear
    @__cache[1] = lines = File.open(filename, 'rb') {|f| f.to_a }
  else
    lines = @__cache[1]
  end
  #; [!162e1] returns line string.
  return lines[linenum-1]
end

.hhmmss(n) ⇒ Object



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
# File 'lib/oktest.rb', line 1757

def hhmmss(n)
  h, n = n.divmod(60*60)
  m, s = n.divmod(60)
  #; [!shyl1] converts 400953.444 into '111:22:33.4'.
  #; [!vyi2v] converts 5025.678 into '1:23:45.7'.
  return "%d:%02d:%04.1f" % [h, m, s] if h > 0
  #; [!pm4xf] converts 754.888 into '12:34.9'.
  #; [!lwewr] converts 83.444 into '1:23.4'.
  return "%d:%04.1f" % [m, s]         if m > 0
  #; [!ijx52] converts 56.8888 into '56.9'.
  return "%.1f" % s                   if s >= 10
  #; [!2kra2] converts 9.777 into '9.78'.
  return "%.2f" % s                   if s >= 1
  #; [!4aomb] converts 0.7777 into '0.778'.
  return "%.3f" % s
end

.required_param_names_of_block(block) ⇒ Object



1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
# File 'lib/oktest.rb', line 1727

def required_param_names_of_block(block)
  #; [!a9n46] returns nil if argument is nil.
  return nil unless block
  #; [!7m81p] returns empty array if block has no parameters.
  n = block.arity
  n = - n - 1 if n < 0
  return [] if n == 0
  #; [!n3g63] returns parameter names of block.
  #; [!d5kym] collects only normal parameter names.
  param_names = block.parameters[0...n].collect {|pair| pair[1] }
  return param_names
end

.strfold(str, width = 80, mark = '...') ⇒ Object



1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
# File 'lib/oktest.rb', line 1740

def strfold(str, width=80, mark='...')
  #; [!wb7m8] returns string as it is if string is not long.
  return str if str.bytesize <= width
  #; [!a2igb] shorten string if it is enough long.
  return str[0, width - mark.length] + mark if str.ascii_only?
  #; [!0gjye] supports non-ascii characters.
  limit = width - mark.length
  w = len = 0
  str.each_char do |ch|
    w += ch.bytesize == 1 ? 1 : 2
    break if w >= limit
    len += 1
  end
  str = str[0, len] + mark if w >= limit
  return str
end

.unified_diff(text_old, text_new, label = "--- old\n+++ new\n", context = 3) ⇒ Object

platform independent, but requires ‘diff-lcs’ gem



1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
# File 'lib/oktest.rb', line 1783

def unified_diff(text_old, text_new, label="--- old\n+++ new\n", context=3)
  #; [!rnx4f] checks whether text string ends with newline char.
  msg = "\\ No newline at end of string"
  lines_old = _text2lines(text_old, msg)
  lines_new = _text2lines(text_new, msg)
  #; [!wf4ns] calculates unified diff from two text strings.
  buf = [label]
  len = 0
  prevhunk = hunk = nil
  diffs = Diff::LCS.diff(lines_old, lines_new)
  diffs.each do |diff|
    hunk = Diff::LCS::Hunk.new(lines_old, lines_new, diff, context, len)
    if hunk.overlaps?(prevhunk)
      hunk.unshift(prevhunk)
    else
      buf << prevhunk.diff(:unified) << "\n"
    end if prevhunk
    len = hunk.file_length_difference
    prevhunk = hunk
  end
  buf << prevhunk.diff(:unified) << "\n" if prevhunk
  return buf.join()
end