forked from kaitai-io/kaitai_struct_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_ruby_to_php
executable file
·52 lines (38 loc) · 1.11 KB
/
spec_ruby_to_php
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
#!/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!(/\)\.to eq\((.*?)\)$/, ").to eq \\1")
s.gsub!(/expect\((.*?)\)\.to eq (.*)$/) {
act = $1
exp = $2
c = act.split(/\./)
c.map! { |x| "#{underscore_to_lower_camel(x)}" }
act = c.join('->').gsub(/^r/, '$r')
"$this->assertEquals(#{exp}, #{act});"
}
s.gsub!(/^\s*end\n/, '')
puts <<__EOF__
<?php
namespace Kaitai\\Struct\\Tests;
class #{class_name}Test extends TestCase {
public function test#{class_name}() {
$r = #{class_name}::fromFile(self::SRC_DIR_PATH . "/#{bin_name}");
__EOF__
print s
puts ' }'
puts '}'