require 'test/unit' require 'bit_struct/text' include BitStruct class TestTextField < Test::Unit::TestCase # Note: The TextField doesn't check the length. It is part of the # contract between the field and the slicer that only the expected # number of bytes (or less) are passed to the field :) def test_initialize TextField.new 'dummy', 8 TextField.new 'dummy', 16 TextField.new 'dummy', 24 assert_raise( RuntimeError ) { TextField.new 'dummy', 5 } end def test_read slicer = FakeSlicer.new 'toast'.bytes field = TextField.new 'dummy', 24 assert_equal 'tsaot', field.read( slicer ) end def test_write slicer = FakeSlicer.new field = TextField.new 'dummy', 24 field.write slicer, 'toast' assert_equal 'tsaot', slicer.get_bytes.stringify end end class FakeSlicer def initialize( data = '' ) @data = data end def get_bytes @data end def set_bytes( data ) @data = data end end