module Oktest::Util
Public Instance Methods
diff_unified(text_old, text_new, label="--- old\n+++ new\n", context=3)
click to toggle source
platform depend, but not require extra library
# File lib/oktest.rb, line 2172 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
Also aliased as: unified_diff
file_line(filename, linenum)
click to toggle source
# File lib/oktest.rb, line 2074 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)
click to toggle source
# File lib/oktest.rb, line 2121 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)
click to toggle source
# File lib/oktest.rb, line 2091 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='...')
click to toggle source
# File lib/oktest.rb, line 2104 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)
click to toggle source
platform independent, but requires 'diff-lcs' gem
# File lib/oktest.rb, line 2147 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
Also aliased as: _unified_diff
Private Instance Methods
_text2lines(text, no_newline_msg=nil)
click to toggle source
# File lib/oktest.rb, line 2138 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