def test_simpletest_strings_submsg(self):
        """Test we can/encode the address submsg bit
        of simpletest"""
        address = [u'123 Fake Street', u'Some City',
                  u'P0S T4L', u'Country']

        message = Message()
        for line, ordinal in zip(address, range(len(address))):
            message.add(line, ordinal=ordinal)

        writer = cStringIO.StringIO()
        message.encode(writer)
        bytes = writer.getvalue()
        m = Message.decode(bytes)
    def test_simple_message(self):
        """A Very simple message - a single indicator field"""
        message = Message()
        message.encode(self._output)
        self.assertOutput('')

        self._output.reset()
        message.add(INDICATOR)
        message.encode(self._output)
        self.assertOutput('\x80\x00')

        m = Message.decode('\x80\x00')
        self.assertEquals(1, len(m.fields))
        f = m.fields[0]
        self.assertEquals(INDICATOR, f.value)
    def test_submsg(self):
        sub1 = Message()
        sub2 = Message()

        m = Message()
        sub1.add(u"fibble", name=u"bibble")
        sub1.add(u"Blibble", ordinal=827)

        sub2.add(9837438, name=u"bibble9")
        sub2.add(82.769997, ordinal=828)

        m.add(sub1, name="sub1")
        m.add(sub2, name="sub2")

        e = Envelope(m)
        writer = cStringIO.StringIO()
        e.encode(writer)
        bytes = writer.getvalue()

        foo = open('tests/data/subMsg.dat', 'r')
        expected = foo.read()
        foo.close()
        self.assertEquals(len(expected), len(bytes))
        self.assertEquals(expected, bytes)
    def test_simpletest(self):
        """Equivalent to the examples/simpletest"""
        MSG_TYPE = registry.DEFAULT_REGISTRY[types.FUDGEMSG_TYPE_ID]
        message = Message()

        message.add(MY_NAME, name=u"name")
        message.add(19801231L, ordinal=4, name=u"dob" )

        submsg = Message()
        for line, ordinal in zip(ADDRESS, range(len(ADDRESS))):
            submsg.add(line, ordinal=ordinal)
        message.add(submsg, name=u"address")
        e = Envelope(message)

        writer = cStringIO.StringIO()
        e.encode(writer)
        bytes = writer.getvalue()

        self.assertEquals(110, len(bytes))

        returned = Envelope.decode(bytes)

        self.assertEquals(0, returned.schema_version)
        self.assertEquals(0, returned.directives)

        returned_message = returned.message
        self.assertEquals(3, len(returned_message.fields))

        f0 = returned_message.fields[0]
        self.assertEquals(u'name', f0.name)
        self.assertEquals(None, f0.ordinal)
        self.assertEquals(MY_NAME, f0.value)

        f1 = returned_message.fields[1]
        self.assertEquals(u'dob', f1.name)
        self.assertEquals(4, f1.ordinal)
        self.assertEquals(19801231L, f1.value)

        submsg = returned_message.fields[2]
        self.assertEquals(u'address', submsg.name)
        self.assertEquals(None, submsg.ordinal)
        self.assertEquals(4, len(submsg.value.fields))
    def test_envelope_with_multi_fields(self):
        """Check the encoding of a message with a few fields both as
        an envelope.

        """

        encoded_mess = '\x80\x00\x90\x00\x00\x02\x80\x01\x01'
        encoded_env =  '\x00\x00\x00\x00\x00\x00\x00\x11' + encoded_mess

        message = Message()
        message.add(INDICATOR)
        message.add(INDICATOR, ordinal=2)
        message.add(True, classname='bool')

        e = Envelope(message)
        e.encode(self._output)
        self.assertOutput(encoded_env)
    def test_message_with_multi_fields(self):
        """Check the encoding of a message with a few fields as
        a message .

        """
        encoded_mess = '\x80\x00\x90\x00\x00\x02\x80\x01\x01'
        encoded_env =  '\x00\x00\x00\x00\x00\x00\x00\x11' + encoded_mess

        message = Message()
        message.add(INDICATOR)
        message.add(INDICATOR, ordinal=2)
        message.add(True, classname='bool')
        message.encode(self._output)
        self.assertOutput(encoded_mess)

        m = Message.decode(encoded_mess)
        self.assertEquals(3, len(m.fields))
    def test_deeper_submsg(self):
        # comparison arrays
        bytes = ''.join([chr(x%256) for x in range(512)] )
        empty = [0] * 128
        shorts = range(16)
        doubles  = [x/10.0 for x in range(16)]

        m = Message()
        m.add(types.INDICATOR, name=u"Indicator")
        m.add(True, name=u"Boolean")

        m.add(128, name=u"Byte")  # Huh - in the C code it's -128 which isn't a byte!
        m.add(-32768, name=u"Short")
        m.add(2147483647, name=u"Int")
        m.add(9223372036854775807L, name=u"Long")
        m.add(1.23456, name=u"Float")
        m.add(1.2345678, name=u"Double", type_=registry.DEFAULT_REGISTRY.type_by_id(types.DOUBLE_TYPE_ID))

        byte_message= Message()
        for size in (4, 8, 16, 20, 32, 64, 128, 256, 512):
            byte_message.add(bytes[:size], ordinal=size)
        m.add(byte_message, name=u'ByteArrays')

        m.add(u'', name=u'Empty String')
        m.add(u'This is a string.', name=u'String')

        fp_message = Message()
        fp_message.add(doubles[:0], name=u'Float[0]', \
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.FLOATARRAY_TYPE_ID))
        fp_message.add(empty[:15], name=u'Float[15]', \
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.FLOATARRAY_TYPE_ID))
        fp_message.add(doubles[:0], name=u'Double[0]', \
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.DOUBLEARRAY_TYPE_ID))
        fp_message.add(doubles[:15], name=u'Double[15]', \
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.DOUBLEARRAY_TYPE_ID))

        array_message = Message()
        array_message.add(bytes[:0], name=u'Byte[0]')
        array_message.add(bytes[:15], name=u'Byte[15]')
        array_message.add(fp_message, name=u'FP Arrays')
        array_message.add(empty[:0], name=u'Short[0]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.SHORTARRAY_TYPE_ID))
        array_message.add(shorts[:15], name=u'Short[15]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.SHORTARRAY_TYPE_ID))
        array_message.add(empty[:0], name=u'Int[0]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.INTARRAY_TYPE_ID))
        array_message.add(empty[:15], name=u'Int[15]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.INTARRAY_TYPE_ID))
        array_message.add(empty[:0], name=u'Long[0]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.LONGARRAY_TYPE_ID))
        array_message.add(empty[:15], name=u'Long[15]',
                type_=registry.DEFAULT_REGISTRY.type_by_id(types.LONGARRAY_TYPE_ID))
        m.add(array_message, name=u'Arrays')

        empty_message = Message()
        m.add(empty_message, name=u'Null Message')

        e = Envelope(m)
        writer = cStringIO.StringIO()
        e.encode(writer)
        bytes = writer.getvalue()

        foo = open('tests/data/deeper_fudge_msg.dat', 'r')
        expected = foo.read()
        foo.close()
        self.assertEquals(len(expected), len(bytes))
        self.assertEquals(expected, bytes)