Skip to content

Commit b2fa928

Browse files
Split tests and remove duplicated tests
1 parent 0c329b1 commit b2fa928

File tree

11 files changed

+222
-274
lines changed

11 files changed

+222
-274
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { initRubyVM } from "./init";
2+
3+
describe("Ruby code evaluation", () => {
4+
test("empty expression", async () => {
5+
const vm = await initRubyVM();
6+
const result = vm.eval("");
7+
expect(result.toString()).toBe("");
8+
});
9+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { RbValue } from "../src/index";
2+
import { initRubyVM } from "./init";
3+
4+
describe("GC integration", () => {
5+
test("Wrapped Ruby object should live until wrapper will be released", async () => {
6+
const vm = await initRubyVM();
7+
const run = vm.eval(`
8+
require "js"
9+
proc do |imports|
10+
imports.call(:mark_js_object_live, JS::Object.wrap(Object.new))
11+
end
12+
`);
13+
const livingObjects = new Set<RbValue>();
14+
run.call(
15+
"call",
16+
vm.wrap({
17+
mark_js_object_live: (object: RbValue) => {
18+
livingObjects.add(object);
19+
},
20+
})
21+
);
22+
vm.eval("GC.start");
23+
for (const object of livingObjects) {
24+
// Ensure that all objects are still alive
25+
object.call("itself");
26+
}
27+
});
28+
29+
test("protect exported Ruby objects", async () => {
30+
function dropRbValue(value: RbValue) {
31+
(value as any).inner.drop();
32+
}
33+
const vm = await initRubyVM();
34+
const initialGCCount = Number(vm.eval("GC.count").toString());
35+
const robj = vm.eval("$x = Object.new");
36+
const robjId = robj.call("object_id").toString();
37+
expect(robjId).not.toEqual("");
38+
39+
vm.eval("GC.start");
40+
expect(robj.call("object_id").toString()).toBe(robjId);
41+
expect(Number(vm.eval("GC.count").toString())).toEqual(initialGCCount + 1);
42+
43+
const robj2 = vm.eval("$x");
44+
vm.eval("GC.start");
45+
expect(robj2.call("object_id").toString()).toBe(robjId);
46+
47+
const robj3 = robj2.call("itself");
48+
vm.eval("GC.start");
49+
expect(robj3.call("object_id").toString()).toBe(robjId);
50+
51+
dropRbValue(robj);
52+
expect(robj2.call("object_id").toString()).toBe(robjId);
53+
expect(robj3.call("object_id").toString()).toBe(robjId);
54+
55+
vm.eval("GC.start");
56+
expect(robj2.call("object_id").toString()).toBe(robjId);
57+
expect(robj3.call("object_id").toString()).toBe(robjId);
58+
59+
dropRbValue(robj2);
60+
dropRbValue(robj3);
61+
62+
vm.eval("GC.start");
63+
});
64+
65+
test("protect objects having same hash values from GC", async () => {
66+
const vm = await initRubyVM();
67+
vm.eval(`
68+
class X
69+
def hash
70+
42
71+
end
72+
def eql?(other)
73+
true
74+
end
75+
end
76+
`);
77+
78+
const o1 = vm.eval(`X.new`);
79+
const o2 = vm.eval(`X.new`);
80+
const o3 = vm.eval(`X.new`);
81+
vm.eval(`GC.start`);
82+
expect(o1.call("hash").toString()).toBe(o2.call("hash").toString());
83+
expect(o2.call("hash").toString()).toBe(o3.call("hash").toString());
84+
});
85+
});

packages/npm-packages/ruby-wasm-wasi/test/js_from_rb.test.ts

Lines changed: 0 additions & 193 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require_relative "./unit/test_js"
22
require_relative "./unit/test_object"
3+
require_relative "./unit/test_object_wrap"
34
require_relative "./unit/test_error"
45
require_relative "./unit/test_async"
56
require_relative "./unit/test_float"
7+
require_relative "./unit/test_proc"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { initRubyVM } from "./init";
2+
3+
describe("RbValue#toJS", () => {
4+
test.each([
5+
{ expr: `1`, result: 1 },
6+
{ expr: `true`, result: true },
7+
{ expr: `false`, result: false },
8+
{ expr: `"x"`, result: "x" },
9+
{ expr: `"x\\0x"`, result: "x\0x" },
10+
])(`Primitive Ruby object`, async (props) => {
11+
const vm = await initRubyVM();
12+
vm.eval(`require "js"; JS`);
13+
const result = vm.eval(props.expr);
14+
expect(result.toJS()).toBe(props.result);
15+
});
16+
17+
test.each([
18+
{ expr: "", result: undefined },
19+
{ expr: "return undefined", result: undefined },
20+
{ expr: "return null", result: null },
21+
{ expr: "return Object", result: Object },
22+
{ expr: "return 1", result: 1 },
23+
{ expr: "return 'x'", result: "x" },
24+
{ expr: "return 'x\\\\0'", result: "x\0" },
25+
])(`Primitive JS object`, async (props) => {
26+
const vm = await initRubyVM();
27+
const JS = vm.eval(`require "js"; JS`);
28+
// TODO(katei): Use RubyVM#toRbValue instead of RubyVM#eval.
29+
const result = JS.call("eval", vm.eval(`"${props.expr}"`));
30+
expect(result.toJS()).toBe(props.result);
31+
});
32+
33+
});

packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ def test_eval
2323
}
2424
JS
2525
end
26+
27+
def test_try_convert
28+
assert_nil JS.try_convert(Object.new)
29+
end
2630
end

packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@ def test_inspect
7373
assert_equal "str", JS.eval("return 'str';").to_s
7474
assert_equal "24", JS.eval("return 24;").inspect
7575
assert_equal "true", JS.eval("return true;").inspect
76-
assert_equal "null", JS.eval("return null;").inspect
77-
assert_equal "undefined", JS.eval("return undefined;").inspect
76+
assert_equal "null", JS::Null.inspect
77+
assert_equal "undefined", JS::Undefined.inspect
78+
assert_equal "[object Object]", JS.eval("return {}").inspect
79+
assert_equal "[object X class]", JS.eval(<<~JS).inspect
80+
class X {
81+
get [Symbol.toStringTag]() {
82+
return 'X class';
83+
}
84+
}
85+
return new X();
86+
JS
7887
end
7988

8089
def test_to_i_from_number
@@ -151,6 +160,18 @@ def test_call
151160
assert_equal "1,2,3", JS.global.call(:Array, 1, 2, 3).to_s
152161
end
153162

163+
def test_call_with_undefined_method
164+
assert_raise("which is a undefined and not a function") do
165+
JS.global.call(:undefined_method)
166+
end
167+
end
168+
169+
def test_call_with_non_js_object
170+
assert_raise("argument 2 is not a JS::Object like object") do
171+
JS.global.call(:Object, Object.new)
172+
end
173+
end
174+
154175
def test_call_with_stress_gc
155176
obj = JS.eval(<<~JS)
156177
return { takeArg() {} }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "test-unit"
2+
require "js"
3+
4+
class JS::TestObjectWrap < Test::Unit::TestCase
5+
def test_identity
6+
intrinsics = JS.eval(<<-JS)
7+
return { identity(v) { return v } }
8+
JS
9+
o1 = Object.new
10+
o1_clone = intrinsics.call(:identity, JS::Object.wrap(o1))
11+
assert_equal o1.object_id, o1_clone.call("call", "object_id").call("toJS").to_i
12+
end
13+
end

0 commit comments

Comments
 (0)