# 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/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.entry as ent
import bdec.field as fld
import bdec.output.xmlout as xml
import bdec.sequence as seq
import bdec.sequenceof as sof
import bdec.expression as expr
from bdec.constraints import Equals
from bdec.expression import parse
        text = xml.to_string(spec.decode(dt.Data('\x00')), verbose=True)
        self.assertEqual('<blah>\n    <_hidden><!-- hex (1 bytes): 00 --></_hidden>\n</blah>\n', text)

    def test_field_with_expected_value(self):
        a = fld.Field('a', 8, fld.Field.INTEGER, constraints=[Equals(0)])
        spec = seq.Sequence('blah', [a])
        text = xml.to_string(spec.decode(dt.Data('\x00')))
        self.assertEqual('<blah>\n    <a></a>\n</blah>\n', text)

    def test_different_child_name(self):
        digit = fld.Field('digit:', length=8)
        number = seq.Sequence('number', [digit], value=expr.compile("${digit:} - 48") )
        header = seq.Sequence('header', [ent.Child('length', number), fld.Field('data', length=expr.compile('${length} * 8'), format=fld.Field.TEXT)])
        text = xml.to_string(header.decode(dt.Data('5abcde')))
        self.assertEqual('<header>\n    <length>5</length>\n    <data>abcde</data>\n</header>\n', text)

    def test_sequence_with_children_and_value(self):
        a = seq.Sequence('a', [fld.Field('b', length=8, format=fld.Field.INTEGER)], value=expr.compile('11'))
        text = xml.to_string(a, dt.Data('\xff'))
        self.assertEqual('<a>\n    <b>255</b>\n    11\n</a>\n', text)

    def test_sequence_with_expected_value(self):
        a = seq.Sequence('a', [], value=expr.compile('1'), constraints=[Equals(1)])
        text = xml.to_string(a, dt.Data())
        self.assertEqual('<a></a>\n', text)

    def test_sequence_with_expected_value(self):
        text = '<a><c/></a>'
        a = seq.Sequence('a', [
            fld.Field('b:', 8),

# 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/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/testxml.py
import unittest
import bdec.choice as chc
import bdec.data as dt
import bdec.entry as ent
import bdec.field as fld
import bdec.output.xmlout as xml
import bdec.sequence as seq
import bdec.sequenceof as sof
import bdec.expression as expr
from bdec.constraints import Equals
from bdec.expression import parse

        text = xml.to_string(spec.decode(dt.Data('\x00')), verbose=True)
        self.assertEqual('<blah>\n    <_hidden><!-- hex (1 bytes): 00 --></_hidden>\n</blah>\n', text)

    def test_field_with_expected_value(self):
        a = fld.Field('a', 8, fld.Field.INTEGER, constraints=[Equals(0)])
        spec = seq.Sequence('blah', [a])
        text = xml.to_string(spec.decode(dt.Data('\x00')))
        self.assertEqual('<blah>\n    <a></a>\n</blah>\n', text)

    def test_different_child_name(self):
        digit = fld.Field('digit:', length=8)
        number = seq.Sequence('number', [digit], value=expr.compile("${digit:} - 48") )
        header = seq.Sequence('header', [ent.Child('length', number), fld.Field('data', length=expr.compile('${length} * 8'), format=fld.Field.TEXT)])
        text = xml.to_string(header.decode(dt.Data('5abcde')))
        self.assertEqual('<header>\n    <length>5</length>\n    <data>abcde</data>\n</header>\n', text)

    def test_sequence_with_children_and_value(self):
        a = seq.Sequence('a', [fld.Field('b', length=8, format=fld.Field.INTEGER)], value=expr.compile('11'))
        text = xml.to_string(a, dt.Data('\xff'))
        self.assertEqual('<a>\n    <b>255</b>\n    11\n</a>\n', text)

    def test_sequence_with_expected_value(self):
        a = seq.Sequence('a', [], value=expr.compile('1'), constraints=[Equals(1)])
        text = xml.to_string(a, dt.Data())
        self.assertEqual('<a></a>\n', text)

    def test_sequence_with_expected_value(self):
        text = '<a><c/></a>'
        a = seq.Sequence('a', [
            fld.Field('b:', 8),

            seq.Sequence('c', [], value=parse('${b:}'), constraints=[Equals(5)])])