Module: Oktest::Util
- Defined in:
- lib/oktest.rb
Defined Under Namespace
Classes: PartialRegexp
Constant Summary collapse
- PARTIAL_REGEXP_CACHE =
:nodoc:
{}
Class Method Summary collapse
- ._text2lines(text, no_newline_msg = nil) ⇒ Object
-
.diff_unified(text_old, text_new, label = "--- old\n+++ new\n", context = 3) ⇒ Object
platform depend, but not require extra library.
- .file_line(filename, linenum) ⇒ Object
- .hhmmss(n) ⇒ Object
- .keyword_param_names_of_block(block) ⇒ Object
- .partial_regexp(pattern, begin_ = '\A', end_ = '\z', mark = "{== ==}") ⇒ Object
- .partial_regexp!(pattern, begin_ = '\A', end_ = '\z', mark = "{== ==}") ⇒ Object
- .required_param_names_of_block(block) ⇒ Object
- .strfold(str, width = 80, mark = '...') ⇒ Object
-
.unified_diff(text_old, text_new, label = "--- old\n+++ new\n", context = 3) ⇒ Object
platform independent, but requires ‘diff-lcs’ gem.
Class Method Details
._text2lines(text, no_newline_msg = nil) ⇒ Object
2202 2203 2204 2205 2206 2207 |
# File 'lib/oktest.rb', line 2202 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
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 |
# File 'lib/oktest.rb', line 2236 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
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 |
# File 'lib/oktest.rb', line 2131 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
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 |
# File 'lib/oktest.rb', line 2185 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 |
.keyword_param_names_of_block(block) ⇒ Object
2161 2162 2163 2164 2165 2166 |
# File 'lib/oktest.rb', line 2161 def keyword_param_names_of_block(block) #; [!p6qqp] returns keyword param names of proc object. names = [] block.parameters.each {|kind, name| names << name if kind == :key } return names end |
.partial_regexp(pattern, begin_ = '\A', end_ = '\z', mark = "{== ==}") ⇒ Object
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 |
# File 'lib/oktest.rb', line 2302 def partial_regexp(pattern, begin_='\A', end_='\z', mark="{== ==}") mark_rexp = PARTIAL_REGEXP_CACHE[mark] if mark_rexp.nil? #; [!ostkw] raises error if mark has no space or has more than two spaces. pair = mark.split() pair.length == 2 or raise ArgumentError.new("#{mark.inspect}: mark should contain only one space (ex: `{== ==}`).") open = Regexp.escape(pair[0]) close = Regexp.escape(pair[1]) mark_rexp = Regexp.compile("#{open}(.*?)#{close}") PARTIAL_REGEXP_CACHE[mark] = mark_rexp end #; [!wn524] returns PartialRegexp object which inspect string is regexp literal style. pos = 0 buf = [] buf << begin_ if begin_ pattern.scan(mark_rexp) do m = Regexp.last_match text = pattern[pos, m.begin(0) - pos] buf << Regexp.escape(text) << $1.strip() pos = m.end(0) end rest = pos == 0 ? pattern : pattern[pos..-1] buf << Regexp.escape(rest) unless rest.empty? buf << end_ if end_ return PartialRegexp.compile(buf.join()) end |
.partial_regexp!(pattern, begin_ = '\A', end_ = '\z', mark = "{== ==}") ⇒ Object
2294 2295 2296 2297 2298 2299 2300 |
# File 'lib/oktest.rb', line 2294 def partial_regexp!(pattern, begin_='\A', end_='\z', mark="{== ==}") #; [!peyu4] returns PartialRegexp object which inspect string is function call styel. regexp = partial_regexp(pattern, begin_, end_, mark) regexp.pattern_string = pattern regexp.begin = begin_; regexp.end = end_; regexp.mark = mark return regexp end |
.required_param_names_of_block(block) ⇒ Object
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 |
# File 'lib/oktest.rb', line 2148 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
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 |
# File 'lib/oktest.rb', line 2168 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
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 |
# File 'lib/oktest.rb', line 2211 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 |