Skip to content

Commit 0393322

Browse files
committed
add STDIO simulate for test
1 parent 2319137 commit 0393322

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

test/test_compiler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_numError
145145
flunk("This test is FAILURE but it's OK")
146146
msg = "数値は@とaで指定してください(a@aaa)"
147147
assert_raise_with_message(Typhon::Compiler::ProgramError, msg) do
148-
Typhon::Compiler.compile("aaa@aa@aaa\n")
148+
Typhon::Compiler.compile("aaa@aa@aaa\t")
149149
end
150150
end
151151
end

test/test_vm.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,21 @@ def test_out
129129
end
130130

131131
def test_char_in
132-
puts "input 'd5'"
133-
res = display_capture do
134-
Typhon::VM.run([[:push, 1], [:char_in], [:push, 1], [:heap_read], [:char_out], [:exit]])
132+
res = nil
133+
simulate_keyboard do
134+
res = display_capture do
135+
Typhon::VM.run([[:push, 1], [:char_in], [:push, 1], [:heap_read], [:char_out], [:exit]])
136+
end
135137
end
136138
assert_equal(["d"], res)
137139
end
138140

139141
def test_num_in
140-
res = display_capture do
141-
Typhon::VM.run([[:push, 1], [:num_in], [:push, 1], [:heap_read], [:num_out], [:exit]])
142+
res = nil
143+
simulate_keyboard do
144+
res = display_capture do
145+
Typhon::VM.run([[:push, 1], [:num_in], [:push, 1], [:heap_read], [:num_out], [:exit]])
146+
end
142147
end
143148
assert_equal([5], res)
144149
end

test/typhontest.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'test/unit'
22

33
module TyphonTest
4-
class Display
4+
class Display
55
attr_accessor :print
66

77
def initialize
@@ -12,9 +12,28 @@ def write(msg)
1212
@print.push(msg)
1313
end
1414
end
15+
16+
class Keyboard
17+
def getc
18+
c = "d"
19+
c[0]
20+
end
21+
22+
def gets
23+
5
24+
end
25+
end
1526
end
1627

1728
class Test::Unit::TestCase
29+
def simulate_keyboard
30+
keyboard = TyphonTest::Keyboard.new
31+
$stdin = keyboard
32+
yield
33+
$stdin = STDIN
34+
return true
35+
end
36+
1837
def display_capture
1938
disp = TyphonTest::Display.new
2039
$stdout = disp

0 commit comments

Comments
 (0)