def testGenerateElementsMissingMandatoryField(self): schema = [ { 'field': 'f0', 'type': 'int' }, { 'field': 'f1', 'type': 'string' }, ] description = { 'int_variables': { 'a': -5, 'b': 5 }, 'elements': { 'elem0': { 'f0': 5 }, } } self.assertRaises( RuntimeError, lambda: GenerateElements('MyType', schema, description))
def testGenerateElements(self): schema = [{ 'field': 'f0', 'type': 'int', 'default': 1000, 'optional': True }, { 'field': 'f1', 'type': 'string' }, { 'field': 'f2', 'type': 'enum', 'ctype': 'QuasiBool', 'default': 'MAYBE', 'optional': True }, { 'field': 'f3', 'type': 'array', 'contents': { 'type': 'string16' }, 'optional': True }] description = { 'int_variables': { 'a': -5, 'b': 5 }, 'elements': { 'elem0': { 'f0': 5, 'f1': 'foo', 'f2': 'SURE' }, 'elem1': { 'f2': 'NOWAY', 'f0': -2, 'f1': 'bar' }, 'elem2': { 'f1': 'foo_bar', 'f3': [u'bar', u'foo'] } } } # Build the expected result stream based on the unpredicatble order the # dictionary element are listed in. int_variable_expected = { 'a': 'const int a = -5;\n', 'b': 'const int b = 5;\n', } elements_expected = { 'elem0': 'const MyType elem0 = {\n' + ' 5,\n' + ' "foo",\n' + ' SURE,\n' + ' NULL,\n' + ' 0,\n' '};\n', 'elem1': 'const MyType elem1 = {\n' + ' -2,\n' + ' "bar",\n' + ' NOWAY,\n' + ' NULL,\n' + ' 0,\n' '};\n', 'elem2': 'const wchar_t* const array_f3[] = {\n' + ' L"bar",\n' + ' L"foo",\n' + '};\n' + 'const MyType elem2 = {\n' + ' 1000,\n' + ' "foo_bar",\n' + ' MAYBE,\n' + ' array_f3,\n' + ' 2,\n' '};\n' } expected = '' for key, value in description['int_variables'].items(): expected += int_variable_expected[key] expected += '\n' elements = [] for key, value in description['elements'].items(): elements.append(elements_expected[key]) expected += '\n'.join(elements) result = GenerateElements('MyType', schema, description) self.assertEquals(expected, result)