Package lepl :: Package bin :: Package _test :: Module literal
[hide private]
[frames] | no frames]

Source Code for Module lepl.bin._test.literal

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Tests for the lepl.bin.literal module. 
 32  ''' 
 33   
 34  if bytes is str: 
 35      print('Binary parsing unsupported in this Python version') 
 36  else: 
 37       
 38      #from logging import basicConfig, DEBUG 
 39      from unittest import TestCase 
 40       
 41      from lepl.bin import * 
 42      from lepl.support.node import Node 
 43       
 44       
 45      # pylint: disable-msg=C0103, C0111, C0301 
 46      # (dude this is just a test) 
 47   
 48   
49 - class ParseTest(TestCase):
50 ''' 51 Test whether we correctly parse a spec. 52 ''' 53
54 - def bassert(self, value, expected, length=None):
55 x = BitString.from_int(expected, length) 56 assert value == x, (value, x)
57
58 - def test_simple(self):
59 b = parse('(0/1)') 60 assert isinstance(b, Node), type(b) 61 assert len(b) == 1, len(b) 62 assert isinstance(b[0], BitString), type(b[0]) 63 assert len(b[0]) == 1, len(b[0]) 64 assert b[0].zero() 65 b = parse('(0/1, 1/1)') 66 assert isinstance(b, Node), type(b) 67 assert len(b) == 2, len(b) 68 assert isinstance(b[1], BitString), type(b[1]) 69 assert len(b[1]) == 1, len(b[1]) 70 assert not b[1].zero() 71 b = parse('(a=0/1)') 72 assert isinstance(b, Node), type(b) 73 assert len(b) == 1, len(b) 74 assert isinstance(b.a[0], BitString), type(b.a[0]) 75 assert len(b.a[0]) == 1, len(b.a[0]) 76 assert b.a[0].zero() 77 b = parse('(a=(0/1))') 78 assert isinstance(b, Node), type(b) 79 assert len(b) == 1, len(b) 80 assert isinstance(b.a[0], Node), type(b.a[0]) 81 assert len(b.a[0]) == 1, len(b.a[0]) 82 assert isinstance(b.a[0][0], BitString), type(b.a[0][0]) 83 assert len(b.a[0][0]) == 1, len(b.a[0][0]) 84 assert b.a[0][0].zero() 85 b = parse('(0)') 86 assert isinstance(b, Node), type(b) 87 assert len(b) == 1, len(b) 88 assert isinstance(b[0], BitString), type(b[0]) 89 assert len(b[0]) == 32, len(b[0]) 90 assert b[0].zero()
91
92 - def test_single(self):
93 b = parse('''(123/8, foo=0x123/2.0,\nbar = 1111100010001000b0)''') 94 self.bassert(b[0], '0x7b') 95 self.bassert(b[1], '0x123', 16) 96 self.bassert(b.foo[0], '0x123', 16) 97 self.bassert(b[2], '1111100010001000b0') 98 self.bassert(b.bar[0], '1111100010001000b0')
99
100 - def test_nested(self):
101 b = parse('(123, (foo=123x0/2.))') 102 self.bassert(b[0], 123) 103 assert isinstance(b[1], Node), str(b) 104 self.bassert(b[1].foo[0], 0x2301, 16)
105
106 - def test_named(self):
107 #basicConfig(level=DEBUG) 108 b = parse('A(B(1), B(2))') 109 self.bassert(b.B[0][0], 1) 110 self.bassert(b.B[1][0], 2)
111
112 - def test_repeat(self):
113 b = parse('(1*3)') 114 self.bassert(b[0], '010000000100000001000000x0') 115 b = parse('(a=0x1234 * 3)') 116 self.bassert(b.a[0], '341234123412x0')
117