Created
January 1, 2024 13:33
-
-
Save tompng/215d11c9cf2c825755960b9b934a7167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dump(s) | |
dumped = s == eval("'#{s}'") && s !~ /[\C-a-\C-z]/ ? "'#{s}'" : s.dump.gsub(/\\x\d\d/){ | |
"\\C-"+('a'.ord + _1[2..].to_i(16) - 1).chr | |
} | |
if dumped =~ /\u3099/ | |
dumped.gsub!("\u3099", "\\u3099") | |
dumped = ?" + dumped[1...-1] + ?" if dumped[0] == ?' | |
end | |
dumped | |
end | |
ARGV.each do |file| | |
code = File.read(file) | |
code.gsub!(/( +(assert_line|assert_byte_pointer_size|assert_cursor|assert_cursor_max)\(.+\)\n){2,}/) do |test| | |
tline = test.match(/assert_line\((['"].*['"])\)/)&.[]1 | |
tbp = test.match(/assert_byte_pointer_size\((['"].*['"])\)/)&.[]1 | |
cursor = test.match(/assert_cursor\((.+)\)/) | |
cursor_max = test.match(/assert_cursor_max\((.+)\)/) | |
unless tline && tbp | |
tline = tbp = tline || tbp if cursor && cursor_max && cursor[1] == cursor_max[1] | |
end | |
unless tline && tbp | |
test.gsub!(/ +assert_cursor_max\((.+)\)\n/, '') if tline && cursor_max | |
test.gsub!(/ +assert_cursor\((.+)\)\n/, '') if tbp && cursor | |
next test | |
end | |
s = eval(tline) | |
bp = eval(tbp).bytesize | |
a = s.byteslice(0, bp) | |
b = s.byteslice(bp..) | |
test[/\A +/] + "assert_cursor_line(#{dump(a)}, #{dump(b)})\n" | |
rescue Exception=> e | |
binding.irb | |
end | |
File.write file, code | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment