Module: Oktest::Util

Defined in:
lib/oktest.rb

Class Method Summary collapse

Class Method Details

._text2lines(text, no_newline_msg = nil) ⇒ Object



2137
2138
2139
2140
2141
2142
# File 'lib/oktest.rb', line 2137

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



2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
# File 'lib/oktest.rb', line 2171

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



2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
# File 'lib/oktest.rb', line 2073

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



2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
# File 'lib/oktest.rb', line 2120

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



2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
# File 'lib/oktest.rb', line 2090

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



2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
# File 'lib/oktest.rb', line 2103

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



2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
# File 'lib/oktest.rb', line 2146

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