def test_register(self): namespace = calligra.namespace() name = 'test' primary_type1 = calligra.PrimaryType(namespace, name, register=False) self.assertEqual(namespace.register(name, primary_type1), primary_type1) self.assertEqual(namespace.get(name), primary_type1) primary_type2 = calligra.PrimaryType(namespace, name, register=False) with self.assertRaises(RuntimeError): namespace.register(name, primary_type2)
def test_anonymous(self): namespace = calligra.namespace(calligra.stdlib.namespace) anonymous = calligra.struct(namespace, '') anonymous.add( calligra.declaration(namespace, namespace.get('size_t'), 'member1')) anonymous.add( calligra.declaration(namespace, namespace.get('char'), 'member2')) code_re = re.compile( r'^struct\s*{\s*size_t\s+member1;\s*char\s+member2;\s*}\s*$') self.assertTrue(code_re.match(anonymous.code())) with self.assertRaises(KeyError) as cm: calligra.stdlib.listnames(namespace, anonymous.type().name())
def test_simple(self): namespace = calligra.namespace(calligra.stdlib.namespace) simple = calligra.struct(namespace, 'simple') simple.add( calligra.declaration(namespace, namespace.get('size_t'), 'member1')) simple.add( calligra.declaration(namespace, namespace.get('char'), 'member2')) code_re = re.compile( r'^struct\s+simple\s*{\s*size_t\s+member1;\s*char\s+member2;\s*}\s*$' ) self.assertTrue(code_re.match(simple.code())) names = calligra.stdlib.listnames(namespace, simple.type().name()) self.assertEqual(names, ['size_t', 'char', 'struct simple'])
def setUp(self): self.namespace = calligra.namespace()
elif str_arg[-1].array: size = str(str_arg[-1].array - 1) else: size = self._char.strlen.call(str_arg) c_arg = calligra.declaration(namespace, self._namespace.get('uint8_t'), '0') n_arg = calligra.declaration(namespace, self._namespace.get('size_t'), size) return super().call(str_arg, (c_arg, ), (n_arg, )) ######################################## types = [] namespace = calligra.namespace() types.append( calligra.PrimaryType(namespace, 'void', imported=True).type().name()) types.append( calligra.IntegerType(namespace, 'size_t', min_value='0', imported=True).type().name()) types.append(boolean(namespace, '_Bool', imported=True).type().name()) types.append(boolean(namespace, 'bool', imported='stdbool.h').type().name()) types.append( calligra.IntegerType(namespace, 'uint8_t', min_value='0', max_value='UINT8_MAX', imported='stdint.h').type().name()) types.append(
) ) self.add( calligra.declaration( namespace, namespace.get('char'), 'authority', pointer = True ) ) self.add( calligra.declaration( namespace, namespace.get('char'), 'path', pointer = True ) ) self.add( calligra.declaration( namespace, namespace.get('char'), 'query', pointer = True ) ) self.add( calligra.declaration( namespace, namespace.get('char'), 'fragment', pointer = True ) ) if __name__ == '__main__': namespace = calligra.namespace(calligra.stdlib.namespace) names = calligra.stdlib.listnames(namespace, url(namespace).type().name()) types = {name: dict(namespace.get(name)) for name in names} code = calligra.stdlib.code(namespace, typesdict = types) print(code)