forked from kaitai-io/kaitai_struct_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_ruby_to_java
executable file
·58 lines (43 loc) · 1.26 KB
/
spec_ruby_to_java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby
s = $stdin.read
def underscore_to_lower_camel(s)
w = s.split(/_/)
first = w.shift
w.map! { |x| x.capitalize }
w.unshift(first)
w.join
end
raise "Unable to detect source name" unless s =~ /require ['"](.*?)['"]/
src_name = $1
raise "Unable to detect class name" unless s =~ /RSpec\.describe (.*?) do/
class_name = $1
raise "Unable to detect binary file name" unless s =~ /r = .*\.from_file\(['"]src\/(.*?)['"]\)/
bin_name = $1
s.gsub!(/^.*it .parses test properly. do\n/m, '')
s.gsub!(/^ *r = .*\.from_file\(.*\n\n/, '')
s.gsub!(/^( +)/) { $1 * 2 }
s.gsub!(/expect\((.*?)\)/) {
ex = $1
c = ex.split(/\./)
first = c.shift
c.map! { |x| "#{underscore_to_lower_camel(x)}()" }
c.unshift(first)
ex = c.join('.')
"assertEquals(#{ex})"
}
s.gsub!(/\)\.to eq ?\(?(.*?)\)?$/) { ", #{$1});" }
s.gsub!(/'/, '"')
s.gsub!(/^\s*end\n/, '')
puts <<__EOF__
package io.kaitai.struct.spec;
import io.kaitai.struct.testformats.#{class_name};
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class Test#{class_name} extends CommonSpec {
@Test
public void test#{class_name}() throws Exception {
#{class_name} r = #{class_name}.fromFile(SRC_DIR + "#{bin_name}");
__EOF__
print s
puts ' }'
puts '}'