# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# Path: bdec/entry.py
# class Child(object):
#     """ A child is embedded in another entry."""
#     def __init__(self, name, entry):
#         self.name = name
#         self.entry = entry
# 
#     def __repr__(self):
#         return "'%s' %s" % (self.entry, self.name)
# 
# Path: bdec/expression.py
# def parse(text):
#     """
#     Compile a length expression into an integer convertible object.
# 
#     :param name_lookup: A object to call to query a named integer 
#         convertible object.
#     return -- An Expression instance
#     """
#     from pyparsing import StringEnd, ParseException
#     complete = _int_expression() + StringEnd()
#     try:
#         return complete.parseString(text)[0]
#     except ParseException, ex:
#         raise ExpressionError(ex)


import unittest
import bdec.choice as chc
import bdec.data as dt
import bdec.field as fld
import bdec.sequence as seq
import bdec.sequenceof as sof
import bdec.output.instance as inst
from bdec.constraints import Equals
from bdec.entry import Child
from bdec.expression import parse
        data = inst.decode(b, dt.Data('\x33'))
        self.assertEqual(0x33, int(data.c))

    def test_sequence_value(self):
        a = seq.Sequence('a', [seq.Sequence('hidden:', [])], value=parse('5'))
        data = inst.decode(a, dt.Data())
        self.assertEqual(5, data)

    def test_sequence_with_children_and_value(self):
        a = seq.Sequence('a', [fld.Field('b', length=8, format=fld.Field.INTEGER)], value=parse('7'))
        data = inst.decode(a, dt.Data('\x33'))
        self.assertEqual(0x33, data.b)
        self.assertEqual(7, int(data))

    def test_exception_when_seequence_is_not_an_int(self):
        a = seq.Sequence('a', [])
        data = inst.decode(a, dt.Data())
        self.assertRaises(TypeError, int, data)

    def test_sequence_to_string(self):
        a = seq.Sequence('a', [fld.Field('b', 8, format=fld.Field.INTEGER)])
        data = inst.decode(a, dt.Data('\x09'))
        self.assertEqual("{'b': 9}", str(data))

    def test_sequenceof_to_string(self):
        a = sof.SequenceOf('a', fld.Field('b', 8, format=fld.Field.INTEGER), count=parse('3'))
        data = inst.decode(a, dt.Data('\x09\x01\x0a'))
        self.assertEqual("[9, 1, 10]", str(data))

    def test_sequence_with_hidden_field_to_string(self):

# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# Path: bdec/entry.py
# class Child(object):
#     """ A child is embedded in another entry."""
#     def __init__(self, name, entry):
#         self.name = name
#         self.entry = entry
# 
#     def __repr__(self):
#         return "'%s' %s" % (self.entry, self.name)
# 
# Path: bdec/expression.py
# def parse(text):
#     """
#     Compile a length expression into an integer convertible object.
# 
#     :param name_lookup: A object to call to query a named integer 
#         convertible object.
#     return -- An Expression instance
#     """
#     from pyparsing import StringEnd, ParseException
#     complete = _int_expression() + StringEnd()
#     try:
#         return complete.parseString(text)[0]
#     except ParseException, ex:
#         raise ExpressionError(ex)


# Path: bdec/output/test/testinstance.py
import unittest
import bdec.choice as chc
import bdec.data as dt
import bdec.field as fld
import bdec.sequence as seq
import bdec.sequenceof as sof
import bdec.output.instance as inst
from bdec.constraints import Equals
from bdec.entry import Child
from bdec.expression import parse

        data = inst.decode(b, dt.Data('\x33'))
        self.assertEqual(0x33, int(data.c))

    def test_sequence_value(self):
        a = seq.Sequence('a', [seq.Sequence('hidden:', [])], value=parse('5'))
        data = inst.decode(a, dt.Data())
        self.assertEqual(5, data)

    def test_sequence_with_children_and_value(self):
        a = seq.Sequence('a', [fld.Field('b', length=8, format=fld.Field.INTEGER)], value=parse('7'))
        data = inst.decode(a, dt.Data('\x33'))
        self.assertEqual(0x33, data.b)
        self.assertEqual(7, int(data))

    def test_exception_when_seequence_is_not_an_int(self):
        a = seq.Sequence('a', [])
        data = inst.decode(a, dt.Data())
        self.assertRaises(TypeError, int, data)

    def test_sequence_to_string(self):
        a = seq.Sequence('a', [fld.Field('b', 8, format=fld.Field.INTEGER)])
        data = inst.decode(a, dt.Data('\x09'))
        self.assertEqual("{'b': 9}", str(data))

    def test_sequenceof_to_string(self):
        a = sof.SequenceOf('a', fld.Field('b', 8, format=fld.Field.INTEGER), count=parse('3'))
        data = inst.decode(a, dt.Data('\x09\x01\x0a'))
        self.assertEqual("[9, 1, 10]", str(data))

    def test_sequence_with_hidden_field_to_string(self):

        a = seq.Sequence('a', [fld.Field('length:', 8), fld.Field('b', format=fld.Field.TEXT, length=parse('${length:} * 8'))])