def test_encode_atom(self):
        """ Try an atom 'hello' """
        data1 = etf.term_to_binary(Term.Atom('hello', encoding="latin-1"))
        expected1 = bytes([131, ord('d'), 0, 5, 104, 101, 108, 108, 111])
        self.assertEqual(data1, expected1)

        data2 = etf.term_to_binary(Term.Atom('hello', encoding="utf8"))
        expected2 = bytes([131, ord('v'), 0, 5, 104, 101, 108, 108, 111])
        self.assertEqual(data2, expected2)
Esempio n. 2
0
    def _control_message(self, ctrl, msg):
        """ Pack a control message and a regular message (can be None) together
            and send them over the connection
        """
        if msg is None:
            packet = b'p' + etf.term_to_binary(ctrl)
        else:
            packet = b'p' + etf.term_to_binary(ctrl) + etf.term_to_binary(msg)

        self._send_packet4(packet)
 def test_binary(self):
     """ Encode and decode binary immediately and compare results """
     data1 = bytes([131, 109, 0, 0, 0, 1, 34])
     (val1, tail) = etf.binary_to_term(data1)
     data2 = etf.term_to_binary(val1)
     self.assertEqual(data1, data2)
     self.assertEqual(tail, b'')
 def test_float(self):
     """ Encode and decode immediately and compare results """
     val1 = 1.234567901
     data1 = etf.term_to_binary(val1)
     (val2, tail) = etf.binary_to_term(data1)
     self.assertEqual(val1, val2)
     self.assertEqual(tail, b'')
 def test_encode_decode_ref(self):
     data1 = bytes([
         131, 114, 0, 3, 100, 0, 13, 101, 114, 108, 64, 49, 50, 55, 46, 48,
         46, 48, 46, 49, 1, 0, 0, 1, 58, 0, 0, 0, 2, 0, 0, 0, 0
     ])
     (val1, tail) = etf.binary_to_term(data1)
     data2 = etf.term_to_binary(val1)
     self.assertEqual(data1, data2)
 def test_binary_bits(self):
     """ Encode and decode binary bits immediately and compare results.
         Erlang value is: <<3:2>>
     """
     data1 = bytes([131, 77, 0, 0, 0, 1, 2, 192])
     ((val1, lbbits1), tail) = etf.binary_to_term(data1)
     data2 = etf.term_to_binary(BitString(val1, lbbits1))
     self.assertEqual(data1, data2)
     self.assertEqual(tail, b'')
 def test_encode_map(self):
     """ Try encode a map #{1 => 2} """
     data = etf.term_to_binary({1: 2})
     expected = bytes([131, 116, 0, 0, 0, 1, 97, 1, 97, 2])
     self.assertEqual(data, expected)