forked from kaitai-io/kaitai_struct_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_ruby_to_go
executable file
·74 lines (59 loc) · 1.41 KB
/
spec_ruby_to_go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
s = $stdin.read
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\(['"](.*?)['"]\)/
bin_name = $1
s.gsub!(/^.*it .parses test properly. do\n/m, '')
s.gsub!(/^ *r = .*\.from_file\(.*\n/, '')
s.gsub!(/\.encode\('UTF-8'\)/, '')
s.gsub!(/expect\((.+)\)\.to eq(?: (.+)|\((.+)\))$/) {
ex = $1
to = $2 ? $2 : $3
to.gsub!(/^\[(0.+)\]\.pack\('C\*'\)$/, '[]byte{\1}')
to.gsub!(/^\[('.+')\]$/, '[]string{\1}')
to.gsub!(/^\[(0.+)\]$/, '[]uint{\1}')
c = ex.split(/\./)
first = c.shift
c.map! { |x| x.split('_').map{|y| y.capitalize}.join }
c.unshift(first)
if c.last == 'Size' then
c.pop
ex = "len(#{c.join('.')})"
else
ex = c.join('.')
end
"assert.EqualValues(t, #{to}, #{ex})"
}
s.gsub!(/'/, '"')
s.gsub!(/^\s*end\n/, '')
puts <<__EOF__
package spec
import (
"os"
"runtime/debug"
"testing"
"github.com/stretchr/testify/assert"
"github.com/kaitai-io/kaitai_struct_go_runtime/kaitai"
. "test_formats"
)
func Test#{class_name}(t *testing.T) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
t.Fatal("unexpected panic:", r)
}
}()
f, err := os.Open("../../#{bin_name}")
if err != nil {
t.Fatal(err)
}
s := kaitai.NewStream(f)
var r #{class_name}
err = r.Read(s, &r, &r)
if err != nil {
t.Fatal(err)
}
__EOF__
print s
puts "}"