require 'test/unit' require 'scottkit/game' require 'stringio' require 'scottkit/withio' # The idea here is that we can start with a source file and compile it # once. Decompiling the result will of course yield a different # source file (for example, comments will be discarded) but # recompiling the decompiled source should yield identical output to # that generated by the initial compilation. class TestCanonicalise < Test::Unit::TestCase #:nodoc: def test_1canonicalise_tutorials 1.upto(7) { |i| canonicalise("tutorial/t#{i}.sck", :source) } end def test_2canonicalise_crystal canonicalise("crystal/crystal.sck", :source) end def test_3canonicalise_adams %w{ adv01 adv02 adv03 adv04 adv05 adv06 adv07 adv08 adv09 adv10 adv11 adv12 adv13 adv14a *adv14b quest1 quest2 sampler1 }.each do |x| options = {} if x[0] == "*" x[0] = "" options[:bug_tolerant] = true end canonicalise("adams/#{x}.dat", :object, options) end end def canonicalise(name, type, options = {}) #puts "#{name}: options=#{options}" filename = "games/#{name}" if !File::readable?(filename) if self.respond_to? :skip skip "no game file #{filename}" else # Crappy older version of Test::Unit puts "skipping '#{name}' because no filename" end return end if type == :source source1 = IO.read(filename) else object0 = IO.read(filename) source1 = decompile(object0, options) end object1 = compile(source1, options) source2 = decompile(object1, options) object2 = compile(source2, options) assert_equal(object2, object1) end 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 end def decompile(object, options) game = ScottKit::Game.new(options) game.load(object) f = StringIO.new() game.decompile(f) f.string end end