def test_multiple_structs(self): source_text = """ struct A { int a; }; struct B { long b; }; """ parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') struct_symbol = self.get_symbol(parse_tree.scope, 'A') self.assertEqual(struct_symbol.value, 'A', 'Value should be "A"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') self.assertRangesEqual(struct_symbol.range, Range((1, 7), (1, 8))) symbol = self.get_symbol(struct_symbol.inner_scope, 'a') self.assertEqual(symbol.value, 'a', 'Value should be "a"') self.assertEqual(symbol.type.value, 'int', 'Type should be "int"') self.assertRangesEqual(symbol.range, Range((2, 8), (2, 9))) struct_symbol = self.get_symbol(parse_tree.scope, 'B') self.assertEqual(struct_symbol.value, 'B', 'Value should be "B"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') self.assertRangesEqual(struct_symbol.range, Range((5, 7), (5, 8))) symbol = self.get_symbol(struct_symbol.inner_scope, 'b') self.assertEqual(symbol.value, 'b', 'Value should be "b"') self.assertEqual(symbol.type.value, 'long', 'Type should be "long"') self.assertRangesEqual(symbol.range, Range((6, 9), (6, 10)))
def test_multiple_comma_separated_char_members(self): source_text = 'struct multi { char x, y; };' parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') struct_symbol = self.get_symbol(parse_tree.scope, 'multi') self.assertEqual(struct_symbol.value, 'multi', 'Value should be "multi"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') symbol = self.get_symbol(struct_symbol.inner_scope, 'x') self.assertEqual(symbol.value, 'x', 'Value should be "x"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((0, 20), (0, 21))) symbol = self.get_symbol(struct_symbol.inner_scope, 'y') self.assertEqual(symbol.value, 'y', 'Value should be "y"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((0, 23), (0, 24)))
def test_typedef_alias_only(self): source_text = """ typedef struct { char name[32]; }A;""" parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') struct_symbol = self.get_symbol(parse_tree.scope, 'A') self.assertEqual(struct_symbol.value, 'A', 'Value should be "A"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') self.assertRangesEqual(struct_symbol.range, Range((3, 1), (3, 2))) symbol = self.get_symbol(struct_symbol.inner_scope, 'name') self.assertEqual(symbol.value, 'name', 'Value should be "name"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((2, 9), (2, 13))) self.assertIsNotNone(symbol.dimension, 'Dimension should not be None') self.assertEqual(symbol.dimension.value, '32', 'Dimension should be "32"')
def test_empty_struct(self): source_text = 'struct empty;' parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') symbol = self.get_symbol(parse_tree.scope, 'empty') self.assertEqual(symbol.value, 'empty', 'Value should be "empty"') self.assertEqual(symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNone(symbol.inner_scope, 'Inner scope should be "None"') self.assertRangesEqual(symbol.range, Range((0, 7), (0, 12)))
def test_single_bool_member(self): source_text = 'struct single { bool x; };' parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') symbol = self.get_symbol(parse_tree.scope, 'single') self.assertEqual(symbol.value, 'single', 'Value should be "single"') self.assertEqual(symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(symbol.inner_scope, 'Inner scope should not be None') symbol = self.get_symbol(symbol.inner_scope, 'x') self.assertEqual(symbol.value, 'x', 'Value should be "x"') self.assertEqual(symbol.type.value, 'bool', 'Type should be "bool"') self.assertRangesEqual(symbol.range, Range((0, 21), (0, 22)))
def test_single_unsigned_char_member(self): source_text = 'struct single { unsigned char x; };' parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') symbol = self.get_symbol(parse_tree.scope, 'single') self.assertEqual(symbol.value, 'single', 'Value should be "single"') self.assertEqual(symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(symbol.inner_scope, 'Inner scope should not be None') symbol = self.get_symbol(symbol.inner_scope, 'x') self.assertEqual(symbol.value, 'x', 'Value should be "x"') self.assertEqual(symbol.type.value, 'unsigned char', 'Type should be "unsigned "char') self.assertRangesEqual(symbol.range, Range((0, 30), (0, 31)))
def test_typedef_struct(self): source_text = 'typedef struct td { char x; char y; }alias;' parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') struct_symbol = self.get_symbol(parse_tree.scope, 'td') self.assertEqual(struct_symbol.value, 'td', 'Value should be "td"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') symbol = self.get_symbol(struct_symbol.inner_scope, 'x') self.assertEqual(symbol.value, 'x', 'Value should be "x"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((0, 25), (0, 26))) symbol = self.get_symbol(struct_symbol.inner_scope, 'y') self.assertEqual(symbol.value, 'y', 'Value should be "y"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((0, 33), (0, 34))) alias = struct_symbol.alias self.assertEqual(alias.value, 'alias', 'Alias value should be "alias"') self.assertEqual(alias.type.value, 'struct', 'Type should be "struct"')
def test_comments(self): source_text = """ /* Description of A */ struct A { // Description of a int a; // This is a description // of b. It is two lines char b; }; """ parse_tree = parse(source_text) self.assertFalse(parse_tree.errors, 'Errors during parsing') struct_symbol = self.get_symbol(parse_tree.scope, 'A') self.assertEqual(struct_symbol.value, 'A', 'Value should be "A"') self.assertEqual(struct_symbol.type.value, 'struct', 'Type should be "struct"') self.assertIsNotNone(struct_symbol.inner_scope, 'Inner scope should not be None') self.assertRangesEqual(struct_symbol.range, Range((2, 7), (2, 8))) symbol = self.get_symbol(struct_symbol.inner_scope, 'a') self.assertEqual(symbol.value, 'a', 'Value should be "a"') self.assertEqual(symbol.type.value, 'int', 'Type should be "int"') self.assertRangesEqual(symbol.range, Range((4, 8), (4, 9))) symbol = self.get_symbol(struct_symbol.inner_scope, 'b') self.assertEqual(symbol.value, 'b', 'Value should be "b"') self.assertEqual(symbol.type.value, 'char', 'Type should be "char"') self.assertRangesEqual(symbol.range, Range((8, 9), (8, 10))) comment = parse_tree.comments[0] self.assertEqual(comment.value, '/* Description of A */', 'Comment text should not change') self.assertRangesEqual(comment.range, Range((1, 0), (1, 22))) comment = parse_tree.comments[1] self.assertEqual(comment.value, '// Description of a', 'Comment text should not change') self.assertRangesEqual(comment.range, Range((3, 4), (3, 23))) comment = parse_tree.comments[2] self.assertEqual(comment.value, '// This is a description\n // of b. It is two lines') self.assertRangesEqual(comment.range, Range((6, 4), (7, 28)))