forked from mitchellh/go-mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_test.go
102 lines (81 loc) · 2.06 KB
/
class_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
package mruby
import (
"testing"
)
func TestClassDefineClassMethod(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
class.DefineClassMethod("foo", testCallback, ArgsNone())
value, err := mrb.LoadString("Hello.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
testCallbackResult(t, value)
}
func TestClassDefineConst(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
class.DefineConst("FOO", String("bar"))
value, err := mrb.LoadString("Hello::FOO")
if err != nil {
t.Fatalf("err: %s", err)
}
if value.String() != "bar" {
t.Fatalf("bad: %s", value)
}
}
func TestClassDefineMethod(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
class.DefineMethod("foo", testCallback, ArgsNone())
value, err := mrb.LoadString("Hello.new.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
testCallbackResult(t, value)
}
func TestClassNew(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
class.DefineMethod("foo", testCallback, ArgsNone())
instance, err := class.New()
if err != nil {
t.Fatalf("err: %s", err)
}
value, err := instance.Call("foo")
if err != nil {
t.Fatalf("err: %s", err)
}
testCallbackResult(t, value)
}
func TestClassNewException(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
class.DefineMethod("initialize", testCallbackException, ArgsNone())
_, err := class.New()
if err == nil {
t.Fatalf("expected exception")
}
// Verify exception is cleared
val, err := mrb.LoadString(`"test"`)
if err != nil {
t.Fatalf("unexpected exception: %#v", err)
}
if val.String() != "test" {
t.Fatalf("expected val 'test', got %#v", val)
}
}
func TestClassValue(t *testing.T) {
mrb := NewMrb()
defer mrb.Close()
class := mrb.DefineClass("Hello", mrb.ObjectClass())
value := class.MrbValue(mrb)
if value.Type() != TypeClass {
t.Fatalf("bad: %d", value.Type())
}
}