This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcoroutines_spec.rb
136 lines (110 loc) · 2.93 KB
/
coroutines_spec.rb
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
#
# Specifying rufus-lua
#
# Sat Mar 14 23:51:42 JST 2009
#
require 'spec_base'
describe Rufus::Lua::State do
describe 'a Lua coroutine' do
before do
@s = Rufus::Lua::State.new
end
after do
@s.close
end
it 'can be returned to Ruby' do
expect(@s.eval(
'return coroutine.create(function (x) end)'
).class).to eq(Rufus::Lua::Coroutine)
end
it 'has a status visible from Ruby' do
co = @s.eval(
'return coroutine.create(function (x) end)'
)
expect(co.status).to eq('suspended')
end
it 'can be resumed from Ruby' do
@s.eval(%{
co = coroutine.create(function (x)
while true do
coroutine.yield(x)
end
end)
})
expect(@s['co'].resume(7)).to eq [ true, 7.0 ]
expect(@s['co'].resume()).to eq [ true, 7.0 ]
end
it 'can be resumed from Ruby (and is not averse to \0 bytes)' do
@s.eval(%{
co = coroutine.create(function (x)
while true do
coroutine.yield(x)
end
end)
})
s = [ 0, 64, 0, 0, 65, 0 ].pack('c*')
expect(@s['co'].resume(s)).to eq [ true, s ]
expect(@s['co'].resume()).to eq [ true, s ]
end
# compressed version of the spec proposed by Nathanael Jones
# in https://github.com/nathanaeljones/rufus-lua/commit/179184aS
#
# for https://github.com/jmettraux/rufus-lua/issues/19
#
it 'yields the right value' do
@s.function :host_function do
'success'
end
r = @s.eval(%{
function routine()
coroutine.yield(host_function())
end
co = coroutine.create(routine)
a, b = coroutine.resume(co)
return { a, b }
}).to_ruby
expect(r).to eq([ true, 'success' ])
end
it 'executes a ruby function within a coroutine' do
run_count = 0
@s.function :host_function do
run_count += 1
end
r = @s.eval(%{
function routine()
host_function()
coroutine.yield()
host_function()
coroutine.yield()
end
co = coroutine.create(routine)
a, b = coroutine.resume(co)
a, b = coroutine.resume(co)
return { a, b }
}).to_ruby
expect(r).to eq([ true])
expect(run_count).to eq(2)
end
it 'executes a ruby function (with arguments) within a coroutine' do
run_count = 0
last_arguments = nil
@s.function :host_function, { :to_ruby => true } do |*args|
run_count += 1
last_arguments = args
0
end
r = @s.eval(%{
function routine()
host_function("hi")
coroutine.yield()
end
co = coroutine.create(routine)
a, b = coroutine.resume(co)
return { a, b }
}).to_ruby
expect(r).to eq([ true ])
expect(run_count).to eq(1)
expect(last_arguments).to eq([ "hi" ])
end
end
end