Skip to content

Commit

Permalink
Remove withIO
Browse files Browse the repository at this point in the history
  • Loading branch information
drewish committed Nov 4, 2017
1 parent d4b5df3 commit 8e389b5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
14 changes: 6 additions & 8 deletions bin/scottkit
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,17 @@ exitval = 0
game = ScottKit::Game.new(options)
case mode
when :play_from_source
f = StringIO.new
withIO(nil, f) do
if !game.compile_to_stdout(ARGV[0])
$stderr.puts "#$0: compilation failed: not playing"
exitval = 1
end
compiled_game = StringIO.new
if !game.compile(compiled_game, ARGV[0])
$stderr.puts "#$0: compilation failed: not playing"
exitval = 1
end
if exitval == 0
game.load(f.string)
game.load(compiled_game.string)
play(game)
end
when :compile
if !game.compile_to_stdout(ARGV[0])
if !game.compile($stdout, ARGV[0])
$stderr.puts "#$0: compilation failed"
exitval = 1
end
Expand Down
12 changes: 6 additions & 6 deletions lib/scottkit/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class Game
attr_reader :input, :output

# Creates a new game, with no room, items or actions -- load must
# be called to make the game ready for playing, or
# compile_to_stdout can be called to generate a new game-file.
# be called to make the game ready for playing, or compile can be
# called to generate a new game-file.
#
# The options hash affects various aspects of how the game will be
# loaded, played and compiled. The following symbols are
# recognised as keys in the options hash:
Expand Down Expand Up @@ -304,12 +305,11 @@ def restore(name)
# function is that its behaviour is influenced by the game's
# options.)
#
def compile_to_stdout(filename, fh = nil)
compiler = ScottKit::Game::Compiler.new(self, filename, fh)
compiler.compile_to($stdout)
def compile(out, filename, fh = nil)
ScottKit::Game::Compiler.new(self, filename, fh).compile_to(out)
end

public :load, :compile_to_stdout # Must be visible to driver program
public :load, :compile # Must be visible to driver program
public :roomname, :itemname # Needed by Condition.render()
public :dirname # Needed by compiler
public :dark_flag= # Invoked from Instruction.execute()
Expand Down
14 changes: 6 additions & 8 deletions test/test_canonicalise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_1canonicalise_tutorials
def test_2canonicalise_crystal
canonicalise("crystal/crystal.sck", :source)
end

def test_3canonicalise_adams
%w{
adv01
Expand All @@ -43,7 +43,7 @@ def test_3canonicalise_adams
x[0] = ""
options[:bug_tolerant] = true
end
canonicalise("adams/#{x}.dat", :object, options)
canonicalise("adams/#{x}.dat", :object, options)
end
end

Expand Down Expand Up @@ -75,12 +75,10 @@ def canonicalise(name, type, options = {})

def compile(source, options)
game = ScottKit::Game.new(options)
f = StringIO.new
withIO(nil, f) do
game.compile_to_stdout(nil, StringIO.new(source)) or
raise "couldn't compile"
end
f.string
output = StringIO.new
game.compile(output, nil, StringIO.new(source)) or
raise "couldn't compile"
output.string
end

def decompile(object, options)
Expand Down
10 changes: 4 additions & 6 deletions test/test_compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ def test_parser

def test_code_generator
game = ScottKit::Game.new({})
f = StringIO.new
withIO(nil, f) do
game.compile_to_stdout("games/test/crystal.sck") or
raise "couldn't compile crystal.sck"
end
assert_equal(f.string, File.read("games/test/crystal.sao"))
compiled_game = StringIO.new
game.compile(compiled_game, "games/test/crystal.sck") or
raise "couldn't compile crystal.sck"
assert_equal(compiled_game.string, File.read("games/test/crystal.sao"))
end
end
11 changes: 5 additions & 6 deletions test/test_playadams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ def play(name)
gamefile = "games/adams/#{name}.dat"

if(File::readable?(gamefile))
game = ScottKit::Game.new({ :read_file =>
"games/test/adams/#{name}.solution",
:random_seed => 12368, :no_wait => true })
game = ScottKit::Game.new(output: StringIO.new,
read_file: "games/test/adams/#{name}.solution",
random_seed: 12368, no_wait: true )
game.load(IO.read gamefile)
f = StringIO.new
withIO(nil, f) { game.play }
digest = Digest::MD5.hexdigest(f.string)
game.play
digest = Digest::MD5.hexdigest(game.output.string)
expected = File.read("games/test/adams/#{name}.transcript.md5").chomp
assert_equal(expected, digest)
else
Expand Down
10 changes: 4 additions & 6 deletions test/test_save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ class TestSave < Test::Unit::TestCase #:nodoc:

def test_save_crystal
game = ScottKit::Game.new(random_seed: 12368, echo_input: true,
input: File.new("games/test/crystal.save-script"),
output: StringIO.new)
game.load(IO.read("games/test/crystal.sao"))
withIO(File.new("games/test/crystal.save-script"), $stdout) do
game.play()
end
game.play()
assert_equal(File.read("TMP"), File.read("games/test/crystal.save-file"))
File.unlink "TMP"
end

def test_resave_crystal
game = ScottKit::Game.new(random_seed: 12368, echo_input: true,
input: StringIO.new("save game\nTMP"),
output: StringIO.new,
restore_file: "games/test/crystal.save-file")
game.load(IO.read("games/test/crystal.sao"))
withIO(StringIO.new("save game\nTMP"), $stdout) do
game.play()
end
game.play()
assert_equal(File.read("TMP"), File.read("games/test/crystal.save-file"))
File.unlink "TMP"
end
Expand Down

0 comments on commit 8e389b5

Please sign in to comment.