# 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
#         names of its contributors may be used to endorse or promote products
#         derived from this software without specific prior written permission.
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#   DISCLAIMED. IN NO EVENT SHALL PRESENSE Technologies GmbH BE LIABLE FOR ANY
#   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#!/usr/bin/env python


class _Inst():
    pass

class TestInstance(unittest.TestCase):
    def test_field(self):
        field = fld.Field("bob", 8, fld.Field.INTEGER)
        data = inst.decode(field, dt.Data.from_hex('6e'))
        self.assertEqual(110, data)

    def test_sequence(self):
        sequence = seq.Sequence("bob", [
            fld.Field("cat", 8, fld.Field.INTEGER),
            fld.Field("dog", 24, fld.Field.TEXT)])
        data = inst.decode(sequence, dt.Data.from_hex('6e7a6970'))

# 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

#         names of its contributors may be used to endorse or promote products
#         derived from this software without specific prior written permission.
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#   DISCLAIMED. IN NO EVENT SHALL PRESENSE Technologies GmbH BE LIABLE FOR ANY
#   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#!/usr/bin/env python


class _Inst():
    pass

class TestInstance(unittest.TestCase):
    def test_field(self):
        field = fld.Field("bob", 8, fld.Field.INTEGER)
        data = inst.decode(field, dt.Data.from_hex('6e'))
        self.assertEqual(110, data)

    def test_sequence(self):
        sequence = seq.Sequence("bob", [
            fld.Field("cat", 8, fld.Field.INTEGER),
            fld.Field("dog", 24, fld.Field.TEXT)])
        data = inst.decode(sequence, dt.Data.from_hex('6e7a6970'))

        self.assertEqual(110, data.cat)