From 8e389b55ef2424428cc8a264c4b78badd86adb49 Mon Sep 17 00:00:00 2001 From: andrew morton Date: Sat, 4 Nov 2017 13:22:58 -0600 Subject: [PATCH] Remove withIO --- bin/scottkit | 14 ++++++-------- lib/scottkit/game.rb | 12 ++++++------ test/test_canonicalise.rb | 14 ++++++-------- test/test_compile.rb | 10 ++++------ test/test_playadams.rb | 11 +++++------ test/test_save.rb | 10 ++++------ 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/bin/scottkit b/bin/scottkit index 48bf2fd..5da3529 100755 --- a/bin/scottkit +++ b/bin/scottkit @@ -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 diff --git a/lib/scottkit/game.rb b/lib/scottkit/game.rb index 991127f..c5a0db1 100644 --- a/lib/scottkit/game.rb +++ b/lib/scottkit/game.rb @@ -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: @@ -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() diff --git a/test/test_canonicalise.rb b/test/test_canonicalise.rb index 33aa777..c6c5438 100644 --- a/test/test_canonicalise.rb +++ b/test/test_canonicalise.rb @@ -16,7 +16,7 @@ def test_1canonicalise_tutorials def test_2canonicalise_crystal canonicalise("crystal/crystal.sck", :source) end - + def test_3canonicalise_adams %w{ adv01 @@ -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 @@ -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) diff --git a/test/test_compile.rb b/test/test_compile.rb index 129f26a..5560103 100644 --- a/test/test_compile.rb +++ b/test/test_compile.rb @@ -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 diff --git a/test/test_playadams.rb b/test/test_playadams.rb index 773c501..4efbcd5 100644 --- a/test/test_playadams.rb +++ b/test/test_playadams.rb @@ -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 diff --git a/test/test_save.rb b/test/test_save.rb index fa7f645..a6e174e 100644 --- a/test/test_save.rb +++ b/test/test_save.rb @@ -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