This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
/
gomruby_test.go
136 lines (113 loc) · 4.52 KB
/
gomruby_test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gomruby_test
import (
. "."
"errors"
"fmt"
"io/ioutil"
. "launchpad.net/gocheck"
"math"
"testing"
)
func TestGoMRuby(t *testing.T) { TestingT(t) }
type F struct {
m *MRuby
c *LoadContext
}
var _ = Suite(&F{})
func (f *F) SetUpTest(c *C) {
f.m = New()
f.c = f.m.NewLoadContext("test.rb")
}
func (f *F) TearDownTest(c *C) {
f.c.Delete()
f.m.Delete()
}
func (f *F) TestLoad(c *C) {
must := func(res interface{}, err error) interface{} {
c.Check(err, IsNil)
return res
}
c.Check(must(f.c.Load(`nil`)), Equals, nil)
c.Check(must(f.c.Load(`true`)), Equals, true)
c.Check(must(f.c.Load(`false`)), Equals, false)
c.Check(must(f.c.Load(`1 + 1`)), Equals, 2)
c.Check(must(f.c.Load(`1 - 1`)), Equals, 0)
c.Check(must(f.c.Load(`1 - 2`)), Equals, -1)
c.Check(must(f.c.Load(`3.14 + 42`)), Equals, 45.14)
c.Check(must(f.c.Load(fmt.Sprintf("%d", int8(math.MaxInt8)))), Equals, math.MaxInt8)
c.Check(must(f.c.Load(fmt.Sprintf("%d", uint8(math.MaxUint8)))), Equals, math.MaxUint8)
c.Check(must(f.c.Load(fmt.Sprintf("%d", int16(math.MaxInt16)))), Equals, math.MaxInt16)
c.Check(must(f.c.Load(fmt.Sprintf("%d", uint16(math.MaxUint16)))), Equals, math.MaxUint16)
// assumes typedef int32_t mrb_int
c.Check(must(f.c.Load(fmt.Sprintf("%d", int32(math.MaxInt32)))), Equals, math.MaxInt32)
c.Check(must(f.c.Load(fmt.Sprintf("%d", uint32(math.MaxUint32)))), Equals, float64(math.MaxUint32))
c.Check(must(f.c.Load(fmt.Sprintf("%d", int64(math.MaxInt64)))), Equals, float64(math.MaxInt64)+2048)
c.Check(must(f.c.Load(fmt.Sprintf("%d", uint64(math.MaxUint64)))), Equals, float64(math.MaxUint64))
// FIXME c.Check(must(f.c.Load(fmt.Sprintf("%f", math.SmallestNonzeroFloat32))), Equals, float64(math.SmallestNonzeroFloat32))
c.Check(must(f.c.Load(fmt.Sprintf("%f", math.MaxFloat32))), Equals, float64(math.MaxFloat32))
// FIXME c.Check(must(f.c.Load(fmt.Sprintf("%f", math.SmallestNonzeroFloat64))), Equals, float64(math.SmallestNonzeroFloat64))
c.Check(must(f.c.Load(fmt.Sprintf("%f", math.MaxFloat64))), Equals, float64(math.MaxFloat64))
c.Check(must(f.c.Load(`domain = "express" + "42" + ".com"`)), Equals, "express42.com")
c.Check(must(f.c.Load(`domain`)), Equals, "express42.com")
c.Check(must(f.c.Load(`""`)), Equals, "")
c.Check(must(f.c.Load(`:symbol`)), Equals, Symbol("symbol"))
slice := []interface{}{1, 3.14, "foo"}
hash := map[interface{}]interface{}{"foo": 1, 3.14: "bar"}
mix := []interface{}{42, map[interface{}]interface{}{3.14: []interface{}{"bar"}, "foo": 1}}
c.Check(must(f.c.Load(`[1, 3.14, "foo"]`)), DeepEquals, slice)
c.Check(must(f.c.Load(`{3.14=>"bar", "foo"=>1}`)), DeepEquals, hash)
c.Check(must(f.c.Load(`[42, {3.14=>["bar"], "foo"=>1}]`)), DeepEquals, mix)
c.Check(must(f.c.Load("ARGV.inspect")), Equals, `[]`)
c.Check(must(f.c.Load("ARGV.inspect", nil, true, false)), Equals, `[nil, true, false]`)
c.Check(must(f.c.Load("ARGV.inspect", 1, 3.14, "foo")), Equals, `[1, 3.14, "foo"]`)
c.Check(must(f.c.Load("ARGV.inspect", slice)), Equals, `[[1, 3.14, "foo"]]`)
c.Check(must(f.c.Load("ARGV.inspect", hash)), Equals, `[{3.14=>"bar", "foo"=>1}]`)
c.Check(must(f.c.Load("ARGV.inspect", mix)), Equals, `[[42, {3.14=>["bar"], "foo"=>1}]]`)
c.Check(must(f.c.Load("ARGV.inspect", Symbol("symbol"))), Equals, `[:symbol]`)
res, err := f.c.Load(`foo`)
c.Check(res, Equals, nil)
c.Check(err, DeepEquals, errors.New(`test.rb:1: undefined method 'foo' for main (NoMethodError)`))
res, err = f.c.Load(`
begin
foo
rescue => e
e.inspect
e.inspect
`)
c.Check(res, Equals, nil)
c.Check(err, DeepEquals, errors.New("SyntaxError: syntax error"))
}
func (f *F) TestLoadMore(c *C) {
res, err := f.c.Load("ARGV.map { |x| x * x }", 1, 2, 3)
c.Check(err, IsNil)
c.Check(res, DeepEquals, []interface{}{1, 4, 9})
}
func (f *F) TestLoadContext(c *C) {
res, err := f.c.Load(`$global = 1; local = 2`)
c.Check(res, Equals, 2)
c.Check(err, IsNil)
// state is preserved
res, err = f.c.Load(`$global`)
c.Check(res, Equals, 1)
c.Check(err, IsNil)
res, err = f.c.Load(`local`)
c.Check(res, Equals, 2)
c.Check(err, IsNil)
c2 := f.m.NewLoadContext("test2.rb")
defer c2.Delete()
// global variable is accessible from other context
res, err = c2.Load(`$global`)
c.Check(res, Equals, 1)
c.Check(err, IsNil)
// local is not
res, err = c2.Load(`local`)
c.Check(res, Equals, nil)
c.Check(err, DeepEquals, errors.New("test2.rb:1: undefined method 'local' for main (NoMethodError)"))
}
func (f *F) TestBugRb(c *C) {
b, err := ioutil.ReadFile("test.rb")
c.Assert(err, IsNil)
res, err := f.c.Load(string(b))
c.Logf("%#v", res)
c.Check(err, IsNil)
}