-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1461bd9
commit f4a647c
Showing
6 changed files
with
127 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Iteration API by section | ||
============================= | ||
|
||
This is description for the end-user facing TRLC iteration API by section | ||
|
||
Function iter_record_objects_by_section() will yield | ||
all the information about record objects, sections and file locations:: | ||
|
||
def iter_record_objects_by_section(self): | ||
|
||
You need to input trlc files with requirements which contain | ||
sections or nested sections with record objects:: | ||
|
||
# Iterates over each record object in the trlc files | ||
# and yields the file location of trlc files | ||
for record_object in self.iter_record_objects(): | ||
file_name = record_object.location.file_name | ||
if location not in self.trlc_files: | ||
self.trlc_files.append(location) | ||
yield location | ||
|
||
# This code block checks section, if present | ||
# it will yield the section and level of section, | ||
# record object and level of record object | ||
if record_object.section: | ||
object_level = len(record_object.section) - 1 | ||
for level, section in enumerate(record_object.section): | ||
if section not in self.section_names: | ||
self.section_names.append(section) | ||
yield section.name, level | ||
yield record_object, object_level | ||
|
||
# If section is not present | ||
# then it will yield the record object and level of record object | ||
else: | ||
object_level = 0 | ||
yield record_object, object_level | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from trlc.ast import Symbol_Table | ||
|
||
class TestRecordObject: | ||
def __init__(self, location, section): | ||
self.location = location | ||
self.section = section | ||
|
||
class TestSection: | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
class TestIterRecordObjectsBySection(unittest.TestCase): | ||
|
||
@patch("trlc.ast.Symbol_Table.iter_record_objects") | ||
def test_iter_record_objects_by_section(self, mock_iter_record_objects): | ||
mock_location1 = MagicMock(file_name = 'file1') | ||
mock_section1 = TestSection('section1') | ||
mock_section2 = TestSection('section2') | ||
mock_location2 = MagicMock(file_name = 'file2') | ||
record1 = TestRecordObject(mock_location1, [mock_section1, mock_section2]) | ||
record2 = TestRecordObject(mock_location2, []) | ||
mock_iter_record_objects.return_value = [record1, record2] | ||
|
||
results = list(Symbol_Table().iter_record_objects_by_section()) | ||
|
||
expected_results = [ | ||
'file1', | ||
('section1', 0), | ||
('section2', 1), | ||
(record1, 1), | ||
'file2', | ||
(record2, 0) | ||
] | ||
|
||
self.assertEqual(results, expected_results) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters