iruby 0.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ubuntu.yml +62 -0
- data/CHANGES.md +203 -0
- data/Gemfile +3 -1
- data/LICENSE +1 -1
- data/README.md +137 -87
- data/Rakefile +36 -10
- data/ci/Dockerfile.base.erb +41 -0
- data/ci/Dockerfile.main.erb +7 -0
- data/ci/requirements.txt +1 -0
- data/docker/setup.sh +15 -0
- data/docker/test.sh +7 -0
- data/iruby.gemspec +14 -18
- data/lib/iruby.rb +14 -8
- data/lib/iruby/backend.rb +38 -10
- data/lib/iruby/command.rb +67 -15
- data/lib/iruby/display.rb +77 -41
- data/lib/iruby/event_manager.rb +40 -0
- data/lib/iruby/formatter.rb +3 -3
- data/lib/iruby/input.rb +6 -6
- data/lib/iruby/input/README.md +299 -0
- data/lib/iruby/input/autoload.rb +1 -1
- data/lib/iruby/input/builder.rb +4 -4
- data/lib/iruby/input/button.rb +2 -2
- data/lib/iruby/input/cancel.rb +1 -1
- data/lib/iruby/input/checkbox.rb +3 -3
- data/lib/iruby/input/date.rb +3 -3
- data/lib/iruby/input/field.rb +2 -2
- data/lib/iruby/input/file.rb +3 -3
- data/lib/iruby/input/form.rb +6 -6
- data/lib/iruby/input/label.rb +4 -4
- data/lib/iruby/input/multiple.rb +10 -10
- data/lib/iruby/input/popup.rb +2 -2
- data/lib/iruby/input/radio.rb +6 -6
- data/lib/iruby/input/select.rb +8 -8
- data/lib/iruby/input/textarea.rb +1 -1
- data/lib/iruby/input/widget.rb +2 -2
- data/lib/iruby/jupyter.rb +77 -0
- data/lib/iruby/kernel.rb +204 -36
- data/lib/iruby/ostream.rb +29 -8
- data/lib/iruby/session.rb +117 -0
- data/lib/iruby/session/cztop.rb +4 -0
- data/lib/iruby/session_adapter.rb +72 -0
- data/lib/iruby/session_adapter/cztop_adapter.rb +45 -0
- data/lib/iruby/session_adapter/ffirzmq_adapter.rb +55 -0
- data/lib/iruby/session_adapter/pyzmq_adapter.rb +77 -0
- data/lib/iruby/session_adapter/test_adapter.rb +49 -0
- data/lib/iruby/utils.rb +13 -2
- data/lib/iruby/version.rb +1 -1
- data/run-test.sh +12 -0
- data/tasks/ci.rake +65 -0
- data/test/helper.rb +136 -0
- data/test/integration_test.rb +22 -11
- data/test/iruby/backend_test.rb +37 -0
- data/test/iruby/command_test.rb +207 -0
- data/test/iruby/event_manager_test.rb +92 -0
- data/test/iruby/jupyter_test.rb +27 -0
- data/test/iruby/kernel_test.rb +185 -0
- data/test/iruby/mime_test.rb +50 -0
- data/test/iruby/multi_logger_test.rb +1 -5
- data/test/iruby/session_adapter/cztop_adapter_test.rb +20 -0
- data/test/iruby/session_adapter/ffirzmq_adapter_test.rb +20 -0
- data/test/iruby/session_adapter/session_adapter_test_base.rb +27 -0
- data/test/iruby/session_adapter_test.rb +91 -0
- data/test/iruby/session_test.rb +48 -0
- data/test/run-test.rb +19 -0
- metadata +120 -50
- data/.travis.yml +0 -16
- data/CHANGES +0 -143
- data/CONTRIBUTORS +0 -19
- data/lib/iruby/session/rbczmq.rb +0 -68
- data/test/test_helper.rb +0 -5
data/test/helper.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require "iruby"
|
2
|
+
require "json"
|
3
|
+
require 'multi_json'
|
4
|
+
require "pathname"
|
5
|
+
require "test/unit"
|
6
|
+
require "test/unit/rr"
|
7
|
+
require "tmpdir"
|
8
|
+
|
9
|
+
|
10
|
+
IRuby.logger = IRuby::MultiLogger.new(*Logger.new(STDERR, level: Logger::Severity::INFO))
|
11
|
+
|
12
|
+
module IRubyTest
|
13
|
+
class TestBase < Test::Unit::TestCase
|
14
|
+
def self.startup
|
15
|
+
@__config_dir = Dir.mktmpdir("iruby-test")
|
16
|
+
@__config_path = Pathname.new(@__config_dir) + "config.json"
|
17
|
+
File.write(@__config_path, {
|
18
|
+
control_port: 50160,
|
19
|
+
shell_port: 57503,
|
20
|
+
transport: "tcp",
|
21
|
+
signature_scheme: "hmac-sha256",
|
22
|
+
stdin_port: 52597,
|
23
|
+
hb_port: 42540,
|
24
|
+
ip: "127.0.0.1",
|
25
|
+
iopub_port: 40885,
|
26
|
+
key: "a0436f6c-1916-498b-8eb9-e81ab9368e84"
|
27
|
+
}.to_json)
|
28
|
+
|
29
|
+
@__original_kernel_instance = IRuby::Kernel.instance
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.shutdown
|
33
|
+
FileUtils.remove_entry_secure(@__config_dir)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.test_config_filename
|
37
|
+
@__config_path.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.restore_kernel
|
41
|
+
IRuby::Kernel.instance = @__original_kernel_instance
|
42
|
+
end
|
43
|
+
|
44
|
+
def teardown
|
45
|
+
self.class.restore_kernel
|
46
|
+
end
|
47
|
+
|
48
|
+
def with_session_adapter(session_adapter_name)
|
49
|
+
IRuby::Kernel.new(self.class.test_config_filename, session_adapter_name)
|
50
|
+
$stdout = STDOUT
|
51
|
+
$stderr = STDERR
|
52
|
+
end
|
53
|
+
|
54
|
+
def assert_output(stdout=nil, stderr=nil)
|
55
|
+
flunk "assert_output requires a block to capture output." unless block_given?
|
56
|
+
|
57
|
+
out, err = capture_io do
|
58
|
+
yield
|
59
|
+
end
|
60
|
+
|
61
|
+
y = check_assert_output_result(stderr, err, "stderr")
|
62
|
+
x = check_assert_output_result(stdout, out, "stdout")
|
63
|
+
|
64
|
+
(!stdout || x) && (!stderr || y)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def capture_io
|
70
|
+
captured_stdout = StringIO.new
|
71
|
+
captured_stderr = StringIO.new
|
72
|
+
|
73
|
+
orig_stdout, $stdout = $stdout, captured_stdout
|
74
|
+
orig_stderr, $stderr = $stderr, captured_stderr
|
75
|
+
|
76
|
+
yield
|
77
|
+
|
78
|
+
return captured_stdout.string, captured_stderr.string
|
79
|
+
ensure
|
80
|
+
$stdout = orig_stdout
|
81
|
+
$stderr = orig_stderr
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_assert_output_result(expected, actual, name)
|
85
|
+
if expected
|
86
|
+
message = "In #{name}"
|
87
|
+
case expected
|
88
|
+
when Regexp
|
89
|
+
assert_match(expected, actual, message)
|
90
|
+
else
|
91
|
+
assert_equal(expected, actual, message)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def ignore_warning
|
97
|
+
saved, $VERBOSE = $VERBOSE , nil
|
98
|
+
yield
|
99
|
+
ensure
|
100
|
+
$VERBOSE = saved
|
101
|
+
end
|
102
|
+
|
103
|
+
def with_env(env)
|
104
|
+
keys = env.keys
|
105
|
+
saved_values = ENV.values_at(*keys)
|
106
|
+
ENV.update(env)
|
107
|
+
yield
|
108
|
+
ensure
|
109
|
+
if keys && saved_values
|
110
|
+
keys.zip(saved_values) do |k, v|
|
111
|
+
ENV[k] = v
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def windows_only
|
117
|
+
omit('windows only test') unless windows?
|
118
|
+
end
|
119
|
+
|
120
|
+
def apple_only
|
121
|
+
omit('apple only test') unless windows?
|
122
|
+
end
|
123
|
+
|
124
|
+
def unix_only
|
125
|
+
omit('unix only test') if windows? || apple?
|
126
|
+
end
|
127
|
+
|
128
|
+
def windows?
|
129
|
+
/mingw|mswin/ =~ RUBY_PLATFORM
|
130
|
+
end
|
131
|
+
|
132
|
+
def apple?
|
133
|
+
/darwin/ =~ RUBY_PLATFORM
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/test/integration_test.rb
CHANGED
@@ -1,40 +1,51 @@
|
|
1
|
-
require '
|
2
|
-
require 'open3'
|
1
|
+
require 'pty'
|
3
2
|
require 'expect'
|
4
3
|
|
5
|
-
class IntegrationTest < IRubyTest
|
4
|
+
class IRubyTest::IntegrationTest < IRubyTest::TestBase
|
6
5
|
def setup
|
7
|
-
|
6
|
+
$expect_verbose = false # make true if you want to dump the output of iruby console
|
7
|
+
|
8
|
+
@in, @out, pid = PTY.spawn('bin/iruby --simple-prompt')
|
9
|
+
@waiter = Thread.start { Process.waitpid(pid) }
|
8
10
|
expect 'In [', 30
|
9
11
|
expect '1'
|
10
12
|
expect ']:'
|
11
13
|
end
|
12
14
|
|
13
15
|
def teardown
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@process.kill
|
16
|
+
@in.close
|
17
|
+
@out.close
|
18
|
+
@waiter.join
|
18
19
|
end
|
19
20
|
|
20
21
|
def write(input)
|
21
|
-
@
|
22
|
+
@out.puts input
|
22
23
|
end
|
23
24
|
|
24
|
-
def expect(pattern, timeout =
|
25
|
-
assert @
|
25
|
+
def expect(pattern, timeout = 10)
|
26
|
+
assert @in.expect(pattern, timeout), "#{pattern} expected, but timeout"
|
27
|
+
end
|
28
|
+
|
29
|
+
def wait_prompt
|
30
|
+
expect 'In ['
|
31
|
+
expect ']:'
|
26
32
|
end
|
27
33
|
|
28
34
|
def test_interaction
|
35
|
+
omit("This test too much unstable")
|
36
|
+
|
29
37
|
write '"Hello, world!"'
|
30
38
|
expect '"Hello, world!"'
|
31
39
|
|
40
|
+
wait_prompt
|
32
41
|
write 'puts "Hello!"'
|
33
42
|
expect 'Hello!'
|
34
43
|
|
44
|
+
wait_prompt
|
35
45
|
write '12 + 12'
|
36
46
|
expect '24'
|
37
47
|
|
48
|
+
wait_prompt
|
38
49
|
write 'ls'
|
39
50
|
expect 'self.methods'
|
40
51
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module IRubyTest
|
2
|
+
class PlainBackendTest < IRubyTest::TestBase
|
3
|
+
def setup
|
4
|
+
@plainbackend = IRuby::PlainBackend.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_eval_one_plus_two
|
8
|
+
assert_equal 3, @plainbackend.eval('1+2', false)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_complete_req
|
12
|
+
assert_includes @plainbackend.complete('req'), 'require'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_complete_2_dot
|
16
|
+
assert_includes @plainbackend.complete('2.'), '2.even?'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class PryBackendTest < IRubyTest::TestBase
|
21
|
+
def setup
|
22
|
+
@prybackend = IRuby::PryBackend.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_eval_one_plus_two
|
26
|
+
assert_equal 3, @prybackend.eval('1+2', false)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_complete_req
|
30
|
+
assert_includes @prybackend.complete('req'), 'require'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_complete_2_dot
|
34
|
+
assert_includes @prybackend.complete('2.'), '2.even?'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'iruby/command'
|
2
|
+
|
3
|
+
module IRubyTest
|
4
|
+
class CommandTest < TestBase
|
5
|
+
def test_with_empty_argv
|
6
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
7
|
+
'IPYTHONDIR' => nil) do
|
8
|
+
assert_output(nil, /\A\z/) do
|
9
|
+
@command = IRuby::Command.new([])
|
10
|
+
kernel_dir = File.join(IRuby::Jupyter.kernelspec_dir, 'ruby')
|
11
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
12
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_with_JUPYTER_DATA_DIR
|
18
|
+
Dir.mktmpdir do |tmpdir|
|
19
|
+
with_env('JUPYTER_DATA_DIR' => tmpdir,
|
20
|
+
'IPYTHONDIR' => nil) do
|
21
|
+
assert_output(nil, /\A\z/) do
|
22
|
+
@command = IRuby::Command.new([])
|
23
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
24
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
25
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_with_IPYTHONDIR
|
32
|
+
Dir.mktmpdir do |tmpdir|
|
33
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
34
|
+
'IPYTHONDIR' => tmpdir) do
|
35
|
+
assert_output(nil, /IPYTHONDIR is deprecated\. Use JUPYTER_DATA_DIR instead\./) do
|
36
|
+
@command = IRuby::Command.new([])
|
37
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
38
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
39
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_with_JUPYTER_DATA_DIR_and_IPYTHONDIR
|
46
|
+
Dir.mktmpdir do |tmpdir|
|
47
|
+
Dir.mktmpdir do |tmpdir2|
|
48
|
+
with_env('JUPYTER_DATA_DIR' => tmpdir,
|
49
|
+
'IPYTHONDIR' => tmpdir2) do
|
50
|
+
assert_output(nil, /both JUPYTER_DATA_DIR and IPYTHONDIR are supplied; IPYTHONDIR is ignored\./) do
|
51
|
+
@command = IRuby::Command.new([])
|
52
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
53
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
54
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_with_ipython_dir_option
|
62
|
+
Dir.mktmpdir do |tmpdir|
|
63
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
64
|
+
'IPYTHONDIR' => nil) do
|
65
|
+
assert_output(nil, /--ipython-dir is deprecated\. Use JUPYTER_DATA_DIR environment variable instead\./) do
|
66
|
+
@command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir}])
|
67
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
68
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
69
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_with_JUPYTER_DATA_DIR_and_ipython_dir_option
|
76
|
+
Dir.mktmpdir do |tmpdir|
|
77
|
+
Dir.mktmpdir do |tmpdir2|
|
78
|
+
with_env('JUPYTER_DATA_DIR' => tmpdir,
|
79
|
+
'IPYTHONDIR' => nil) do
|
80
|
+
assert_output(nil, /both JUPYTER_DATA_DIR and --ipython-dir are supplied; --ipython-dir is ignored\./) do
|
81
|
+
@command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir2}])
|
82
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
83
|
+
assert_equal(kernel_dir, @command.kernel_dir)
|
84
|
+
assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_register_when_there_is_kernel_in_ipython_dir
|
92
|
+
Dir.mktmpdir do |tmpdir|
|
93
|
+
Dir.mktmpdir do |tmpdir2|
|
94
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
95
|
+
'IPYTHONDIR' => nil,
|
96
|
+
'HOME' => tmpdir2) do
|
97
|
+
ignore_warning do
|
98
|
+
@command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
|
99
|
+
assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
|
100
|
+
@command.run
|
101
|
+
assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
106
|
+
'IPYTHONDIR' => nil,
|
107
|
+
'HOME' => tmpdir2) do
|
108
|
+
@command = IRuby::Command.new(["register"])
|
109
|
+
assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
|
110
|
+
@command.run
|
111
|
+
end
|
112
|
+
assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
|
113
|
+
refute(File.file?(@command.kernel_file))
|
114
|
+
|
115
|
+
@command = IRuby::Command.new(["register", "--force"])
|
116
|
+
assert_output(nil, nil) do
|
117
|
+
@command.run
|
118
|
+
end
|
119
|
+
refute(File.exist?(@command.ipython_kernel_dir))
|
120
|
+
assert(File.file?(@command.kernel_file))
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_register_and_unregister_with_JUPYTER_DATA_DIR
|
127
|
+
Dir.mktmpdir do |tmpdir|
|
128
|
+
Dir.mktmpdir do |tmpdir2|
|
129
|
+
with_env('JUPYTER_DATA_DIR' => tmpdir,
|
130
|
+
'IPYTHONDIR' => nil,
|
131
|
+
'HOME' => tmpdir2) do
|
132
|
+
assert_output(nil, nil) do
|
133
|
+
@command = IRuby::Command.new(['register'])
|
134
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
135
|
+
kernel_file = File.join(kernel_dir, 'kernel.json')
|
136
|
+
assert(!File.file?(kernel_file))
|
137
|
+
|
138
|
+
@command.run
|
139
|
+
assert(File.file?(kernel_file))
|
140
|
+
|
141
|
+
@command = IRuby::Command.new(['unregister'])
|
142
|
+
@command.run
|
143
|
+
assert(!File.file?(kernel_file))
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_register_and_unregister_with_JUPYTER_DATA_DIR_when_there_is_kernel_in_ipython_dir
|
151
|
+
Dir.mktmpdir do |tmpdir|
|
152
|
+
Dir.mktmpdir do |tmpdir2|
|
153
|
+
with_env('HOME' => tmpdir2) do
|
154
|
+
ignore_warning do
|
155
|
+
@command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
|
156
|
+
assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
|
157
|
+
@command.run
|
158
|
+
assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
with_env('JUPYTER_DATA_DIR' => tmpdir,
|
163
|
+
'IPYTHONDIR' => nil,
|
164
|
+
'HOME' => tmpdir2) do
|
165
|
+
@command = IRuby::Command.new(["register"])
|
166
|
+
assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
|
167
|
+
@command.run
|
168
|
+
end
|
169
|
+
assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
|
170
|
+
refute(File.file?(@command.kernel_file))
|
171
|
+
|
172
|
+
@command = IRuby::Command.new(["register", "--force"])
|
173
|
+
assert_output(nil, nil) do
|
174
|
+
@command.run
|
175
|
+
end
|
176
|
+
refute(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
|
177
|
+
assert(File.file?(@command.kernel_file))
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_register_and_unregister_with_IPYTHONDIR
|
184
|
+
Dir.mktmpdir do |tmpdir|
|
185
|
+
Dir.mktmpdir do |tmpdir2|
|
186
|
+
with_env('JUPYTER_DATA_DIR' => nil,
|
187
|
+
'IPYTHONDIR' => tmpdir,
|
188
|
+
'HOME' => tmpdir2) do
|
189
|
+
ignore_warning do
|
190
|
+
@command = IRuby::Command.new(['register'])
|
191
|
+
kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
|
192
|
+
kernel_file = File.join(kernel_dir, 'kernel.json')
|
193
|
+
assert(!File.file?(kernel_file))
|
194
|
+
|
195
|
+
@command.run
|
196
|
+
assert(File.file?(kernel_file))
|
197
|
+
|
198
|
+
@command = IRuby::Command.new(['unregister'])
|
199
|
+
@command.run
|
200
|
+
assert(!File.file?(kernel_file))
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module IRubyTest
|
2
|
+
class EventManagerTest < TestBase
|
3
|
+
def setup
|
4
|
+
@man = IRuby::EventManager.new([:foo, :bar])
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_available_events
|
8
|
+
assert_equal([:foo, :bar],
|
9
|
+
@man.available_events)
|
10
|
+
end
|
11
|
+
|
12
|
+
sub_test_case("#register") do
|
13
|
+
sub_test_case("known event name") do
|
14
|
+
def test_register
|
15
|
+
fn = ->() {}
|
16
|
+
assert_equal(fn,
|
17
|
+
@man.register(:foo, &fn))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
sub_test_case("unknown event name") do
|
22
|
+
def test_register
|
23
|
+
assert_raise_message("Unknown event name: baz") do
|
24
|
+
@man.register(:baz) {}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
sub_test_case("#unregister") do
|
31
|
+
sub_test_case("no event is registered") do
|
32
|
+
def test_unregister
|
33
|
+
fn = ->() {}
|
34
|
+
assert_raise_message("Given callable object #{fn} is not registered as a foo callback") do
|
35
|
+
@man.unregister(:foo, fn)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
sub_test_case("the registered callable is given") do
|
41
|
+
def test_unregister
|
42
|
+
results = { values: [] }
|
43
|
+
fn = ->(a) { values << a }
|
44
|
+
|
45
|
+
@man.register(:foo, &fn)
|
46
|
+
|
47
|
+
results[:retval] = @man.unregister(:foo, fn)
|
48
|
+
|
49
|
+
@man.trigger(:foo, 42)
|
50
|
+
|
51
|
+
assert_equal({
|
52
|
+
values: [],
|
53
|
+
retval: fn
|
54
|
+
},
|
55
|
+
results)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
sub_test_case("#trigger") do
|
61
|
+
sub_test_case("no event is registered") do
|
62
|
+
def test_trigger
|
63
|
+
assert_nothing_raised do
|
64
|
+
@man.trigger(:foo)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
sub_test_case("some events are registered") do
|
70
|
+
def test_trigger
|
71
|
+
values = []
|
72
|
+
@man.register(:foo) {|a| values << a }
|
73
|
+
@man.register(:foo) {|a| values << 10*a }
|
74
|
+
@man.register(:foo) {|a| values << 100+a }
|
75
|
+
|
76
|
+
@man.trigger(:foo, 5)
|
77
|
+
|
78
|
+
assert_equal([5, 50, 105],
|
79
|
+
values)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
sub_test_case("unknown event name") do
|
84
|
+
def test_trigger
|
85
|
+
assert_raise_message("Unknown event name: baz") do
|
86
|
+
@man.trigger(:baz, 100)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|