| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251 |
- #!/usr/bin/env python
- #
- # Copyright 2021 Espressif Systems (Shanghai) CO LTD
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- import os
- import sys
- import tempfile
- import unittest
- from io import StringIO
- from pyparsing import ParseException, ParseFatalException, Word, alphanums
- try:
- from fragments import FRAGMENT_TYPES, Fragment, FragmentFile, Mapping
- from sdkconfig import SDKConfig
- except ImportError:
- sys.path.append('../')
- from fragments import FRAGMENT_TYPES, Fragment, FragmentFile, Mapping
- from sdkconfig import SDKConfig
- class SampleFragment(Fragment):
- grammars = {
- 'key_1': Fragment.KeyValue(Word(alphanums + '_').setResultsName('value'), 0, None, True),
- 'key_2': Fragment.KeyValue(Word(alphanums + '_').setResultsName('value'), 0, None, False),
- 'key_3': Fragment.KeyValue(Word(alphanums + '_').setResultsName('value'), 3, 5, False)
- }
- def set_key_value(self, key, parse_results):
- if key == 'key_1':
- self.key_1 = list()
- for result in parse_results:
- self.key_1.append(result['value'])
- elif key == 'key_2':
- self.key_2 = list()
- for result in parse_results:
- self.key_2.append(result['value'])
- def get_key_grammars(self):
- return self.__class__.grammars
- FRAGMENT_TYPES['test'] = SampleFragment
- class FragmentTest(unittest.TestCase):
- def setUp(self):
- with tempfile.NamedTemporaryFile(delete=False) as f:
- self.kconfigs_source_file = os.path.join(tempfile.gettempdir(), f.name)
- with tempfile.NamedTemporaryFile(delete=False) as f:
- self.kconfig_projbuilds_source_file = os.path.join(tempfile.gettempdir(), f.name)
- os.environ['COMPONENT_KCONFIGS_SOURCE_FILE'] = self.kconfigs_source_file
- os.environ['COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE'] = self.kconfig_projbuilds_source_file
- os.environ['COMPONENT_KCONFIGS'] = ''
- os.environ['COMPONENT_KCONFIGS_PROJBUILD'] = ''
- # prepare_kconfig_files.py doesn't have to be called because COMPONENT_KCONFIGS and
- # COMPONENT_KCONFIGS_PROJBUILD are empty
- self.sdkconfig = SDKConfig('data/Kconfig', 'data/sdkconfig')
- def tearDown(self):
- try:
- os.remove(self.kconfigs_source_file)
- os.remove(self.kconfig_projbuilds_source_file)
- except Exception:
- pass
- @staticmethod
- def create_fragment_file(contents, name='test_fragment.lf'):
- f = StringIO(contents)
- f.name = name
- return f
- def test_basic(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- value_2 # comments should be ignored
- value_3
- # this is a comment as well
- key_2: value_a
- # this is the last comment
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments[0].key_1), 3)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[0].key_1[1], 'value_2')
- self.assertEqual(fragment_file.fragments[0].key_1[2], 'value_3')
- self.assertEqual(len(fragment_file.fragments[0].key_2), 1)
- self.assertEqual(fragment_file.fragments[0].key_2[0], 'value_a')
- def test_duplicate_keys(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1: value_1
- key_1: value_a
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_empty_key(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_conditional(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- if A = y:
- value_2
- value_3
- if A = n:
- value_4
- if B = n:
- value_5
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[0].key_1[1], 'value_2')
- self.assertEqual(fragment_file.fragments[0].key_1[2], 'value_3')
- self.assertEqual(fragment_file.fragments[0].key_1[3], 'value_5')
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- if B = y:
- value_2
- elif C = y:
- value_3
- elif A = y:
- value_4
- else:
- value_5
- value_6
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[0].key_1[1], 'value_3')
- self.assertEqual(fragment_file.fragments[0].key_1[2], 'value_6')
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- if A = y:
- value_2
- if B = y:
- value_3
- else:
- value_4
- if C = y:
- value_5
- value_6
- value_7
- key_2:
- value_a
- if B != y:
- value_b
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[0].key_1[1], 'value_2')
- self.assertEqual(fragment_file.fragments[0].key_1[2], 'value_4')
- self.assertEqual(fragment_file.fragments[0].key_1[3], 'value_5')
- self.assertEqual(fragment_file.fragments[0].key_1[4], 'value_6')
- self.assertEqual(fragment_file.fragments[0].key_1[5], 'value_7')
- self.assertEqual(fragment_file.fragments[0].key_2[0], 'value_a')
- self.assertEqual(fragment_file.fragments[0].key_2[1], 'value_b')
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- if A = n:
- value_2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments[0].key_1), 0)
- def test_empty_file(self):
- test_fragment = self.create_fragment_file(u"""
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments), 0)
- def test_setting_indent(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- value_2
- value_3
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments[0].key_1), 3)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[0].key_1[1], 'value_2')
- self.assertEqual(fragment_file.fragments[0].key_1[2], 'value_3')
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_1
- value_2 # first element dictates indent
- value_3
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_values_num_limit(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_3:
- value_1
- value_2
- value_3
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_3:
- value_1
- value_2
- value_3
- value_4
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments), 1)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_3:
- value_1
- value_2
- value_3
- value_4
- value_5
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments), 1)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_3:
- value_1
- value_2
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_3:
- value_1
- value_2
- value_3
- value_4
- value_5
- value_6
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_unsupported_key(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- value_a
- key_4:
- value_1
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_empty_fragment(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_empty_conditional(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- if B = y:
- else:
- value_1
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- if B = y:
- value_1
- else B = y:
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- if B = y:
- value_1
- elif B = y:
- else:
- value_2
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_out_of_order_conditional(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- elif B = y:
- value_1
- else:
- value_2
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_1:
- else:
- value_2
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_required_keys(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test]
- key_2:
- value_1
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_multiple_fragments(self):
- test_fragment = self.create_fragment_file(u"""
- [test:test1]
- key_1:
- value_1
- [test:test2]
- key_1:
- value_2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments), 2)
- self.assertEqual(fragment_file.fragments[0].key_1[0], 'value_1')
- self.assertEqual(fragment_file.fragments[1].key_1[0], 'value_2')
- def test_whole_conditional_fragment(self):
- test_fragment = self.create_fragment_file(u"""
- if B = y:
- [test:test1]
- key_1:
- value_1
- else:
- [test:test2]
- key_1:
- value_2
- if A = y:
- [test:test3]
- key_1:
- value_3
- if C = y:
- value_6
- [test:test4]
- key_1:
- value_4
- [test:test5]
- key_1:
- value_5
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(len(fragment_file.fragments), 4)
- self.assertEqual(fragment_file.fragments[0].name, 'test2')
- self.assertEqual(fragment_file.fragments[1].name, 'test3')
- self.assertEqual(fragment_file.fragments[1].key_1[1], 'value_6')
- self.assertEqual(fragment_file.fragments[2].name, 'test4')
- self.assertEqual(fragment_file.fragments[3].name, 'test5')
- def test_equivalent_conditional_fragment(self):
- test_fragment1 = self.create_fragment_file(u"""
- if A = y:
- [test:test1]
- key_1:
- value_1
- else:
- [test:test2]
- key_1:
- value_2
- """)
- fragment_file1 = FragmentFile(test_fragment1, self.sdkconfig)
- self.assertEqual(len(fragment_file1.fragments), 1)
- self.assertEqual(fragment_file1.fragments[0].key_1[0], 'value_1')
- test_fragment2 = self.create_fragment_file(u"""
- [test:test1]
- key_1:
- if A = y:
- value_1
- else:
- value_2
- """)
- fragment_file2 = FragmentFile(test_fragment2, self.sdkconfig)
- self.assertEqual(len(fragment_file2.fragments), 1)
- self.assertEqual(fragment_file2.fragments[0].key_1[0], 'value_1')
- class SectionsTest(FragmentTest):
- def test_basic(self):
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- .section1
- .section2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries, {'.section1', '.section2'})
- def test_duplicate_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- .section1
- .section2
- .section3
- .section2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries, {'.section1', '.section2', '.section3'})
- def test_empty_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- if B = y:
- .section1
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_entries_grammar(self):
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- _valid1
- valid2.
- .valid3_-
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries,
- {'_valid1', 'valid2.', '.valid3_-'})
- # invalid starting char
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- 1invalid
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- -invalid
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- # + notation
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- valid+
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries,
- {'valid+'})
- test_fragment = self.create_fragment_file(u"""
- [sections:test]
- entries:
- inva+lid+
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- class SchemeTest(FragmentTest):
- def test_basic(self):
- test_fragment = self.create_fragment_file(u"""
- [scheme:test]
- entries:
- sections1 -> target1
- sections2 -> target2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries,
- {('sections1', 'target1'),
- ('sections2', 'target2')})
- def test_duplicate_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [scheme:test]
- entries:
- sections1 -> target1
- sections2 -> target2
- sections2 -> target2
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(fragment_file.fragments[0].entries,
- {('sections1', 'target1'),
- ('sections2', 'target2')})
- def test_empty_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [scheme:test]
- entries:
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [scheme:test]
- entries:
- if B = y:
- sections1 -> target1
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_improper_grammar(self):
- test_fragment = self.create_fragment_file(u"""
- [scheme:test]
- entries:
- sections1, target1 # improper separator
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- class MappingTest(FragmentTest):
- def test_basic(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- obj:symbol (noflash)
- obj (noflash)
- obj:symbol_2 (noflash)
- obj_2 (noflash)
- * (noflash)
- """)
- expected = {('obj', 'symbol', 'noflash'),
- ('obj', None, 'noflash'),
- ('obj', 'symbol_2', 'noflash'),
- ('obj_2', None, 'noflash'),
- ('*', None, 'noflash')}
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_archive(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- entries:
- * (default)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- lib1.a
- lib2.a
- entries:
- * (default)
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_empty_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- lib.a
- entries:
- if B = y:
- * (noflash) # if condition is false, then no 'entries' key value
- """)
- expected = set()
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- lib.a
- entries:
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_duplicate_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- lib.a
- entries:
- obj:symbol (noflash)
- obj:symbol (noflash)
- """)
- expected = {('obj', 'symbol', 'noflash')}
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_invalid_grammar(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive:
- lib.a
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- entries:
- * (default)
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- obj: (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- obj: ()
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- obj:symbol
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- obj:* (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- :symbol (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- test_fragment = self.create_fragment_file(u"""
- [mapping:test]
- archive: lib.a
- entries:
- *:symbol (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_keep_flag(self):
- # Test parsing combinations and orders of flags
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text KEEP(),
- rodata->flash_rodata KEEP() KEEP()
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Keep()]),
- ('rodata', 'flash_rodata', [Mapping.Keep(), Mapping.Keep()])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- def test_align_flag(self):
- # Test parsing combinations and orders of flags
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text ALIGN(8),
- rodata->flash_rodata ALIGN(8, pre),
- data->dram0_data ALIGN(8, pre, post),
- bss->dram0_bss ALIGN(8, post),
- common->dram0_bss ALIGN(8, pre, post) ALIGN(8)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Align(8, True, False)]),
- ('rodata', 'flash_rodata', [Mapping.Align(8, True, False)]),
- ('data', 'dram0_data', [Mapping.Align(8, True, True)]),
- ('bss', 'dram0_bss', [Mapping.Align(8, False, True)]),
- ('common', 'dram0_bss', [Mapping.Align(8, True, True), Mapping.Align(8, True, False)])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- # Wrong post, pre order
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (noflash)
- text->iram0_text ALIGN(8, post, pre)
- """)
- with self.assertRaises(ParseFatalException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_sort_flag(self):
- # Test parsing combinations and orders of flags
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text SORT(name),
- rodata->flash_rodata SORT(alignment),
- data->dram0_data SORT(init_priority),
- bss->dram0_bss SORT(name, alignment),
- common->dram0_bss SORT(alignment, name),
- iram->iram0_text SORT(name, name),
- dram->dram0_data SORT(alignment, alignment)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Sort('name')]),
- ('rodata', 'flash_rodata', [Mapping.Sort('alignment')]),
- ('data', 'dram0_data', [Mapping.Sort('init_priority')]),
- ('bss', 'dram0_bss', [Mapping.Sort('name', 'alignment')]),
- ('common', 'dram0_bss', [Mapping.Sort('alignment', 'name')]),
- ('iram', 'iram0_text', [Mapping.Sort('name', 'name')]),
- ('dram', 'dram0_data', [Mapping.Sort('alignment', 'alignment')])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default)
- text->iram0_text SORT(name) SORT(alignment)
- """)
- def test_surround_flag(self):
- # Test parsing combinations and orders of flags
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text SURROUND(sym1)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Surround('sym1')])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- def test_flag_order(self):
- # Test that the order in which the flags are specified is retained
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text ALIGN(4) KEEP() SURROUND(sym1) ALIGN(8) SORT(name),
- rodata->flash_rodata KEEP() ALIGN(4) KEEP() SURROUND(sym1) ALIGN(8) ALIGN(4) SORT(name)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Align(8, True, False),
- Mapping.Sort('name')]),
- ('rodata', 'flash_rodata', [Mapping.Keep(),
- Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Align(8, True, False),
- Mapping.Align(4, True, False),
- Mapping.Sort('name')])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- def test_flags_entries_multiple_flags(self):
- # Not an error, generation step handles this, since
- # it that step has a more complete information
- # about all mappings.
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text ALIGN(4) KEEP() SURROUND(sym1) SORT(name),
- text->flash_text ALIGN(4) KEEP() SURROUND(sym1) SORT(name)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Sort('name')]),
- ('text', 'flash_text', [Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Sort('name')])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- def test_flags_entries_multiple_flags_and_entries(self):
- # Not an error, generation step handles this, since
- # it that step has a more complete information
- # about all mappings. This can happen across multiple
- # mapping fragments.
- test_fragment = self.create_fragment_file(u"""
- [mapping:map]
- archive: libmain.a
- entries:
- obj1 (default);
- text->flash_text ALIGN(4) KEEP() SURROUND(sym1) SORT(name)
- obj1 (default);
- text->flash_text ALIGN(4) KEEP() SURROUND(sym1) SORT(name)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- fragment = fragment_file.fragments[0]
- expected = [('text', 'flash_text', [Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Sort('name')]),
- ('text', 'flash_text', [Mapping.Align(4, True, False),
- Mapping.Keep(),
- Mapping.Surround('sym1'),
- Mapping.Sort('name')])]
- actual = fragment.flags[('obj1', None, 'default')]
- self.assertEqual(expected, actual)
- class DeprecatedMappingTest(FragmentTest):
- def test_valid_grammar(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- obj:symbol (noflash)
- # Comments should not matter
- obj (noflash)
- # Nor should whitespace
- obj : symbol_2 ( noflash )
- obj_2 ( noflash )
- * (noflash)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual('lib.a', fragment_file.fragments[0].archive)
- self.assertEqual('lib_a', fragment_file.fragments[0].name)
- expected = {('obj', 'symbol', 'noflash'),
- ('obj', None, 'noflash'),
- ('obj', 'symbol_2', 'noflash'),
- ('obj_2', None, 'noflash'),
- ('*', None, 'noflash')
- }
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_explicit_blank_default_w_others(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = n
- obj_a (noflash)
- : default
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('*', None, 'default')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_implicit_blank_default_w_others(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = n
- obj_a (noflash)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('*', None, 'default')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_explicit_blank_default(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : default
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('*', None, 'default')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_implicit_blank_default(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : default
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('*', None, 'default')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_multiple_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = n
- obj_a1 (noflash)
- obj_a2 (noflash)
- : B = n
- obj_b1 (noflash)
- obj_b2 (noflash)
- obj_b3 (noflash)
- : C = n
- obj_c1 (noflash)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('obj_b1', None, 'noflash'),
- ('obj_b2', None, 'noflash'),
- ('obj_b3', None, 'noflash')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_blank_entries(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = n
- obj_a (noflash)
- : B = n
- : C = n
- obj_c (noflash)
- : default
- obj (noflash)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- expected = {('*', None, 'default')}
- self.assertEqual(expected, fragment_file.fragments[0].entries)
- def test_blank_first_condition(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- obj_a (noflash)
- : CONFIG_B = y
- obj_b (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_nonlast_default_1(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : default
- obj_a (noflash)
- : CONFIG_A = y
- obj_A (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_nonlast_default_2(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = y
- obj_A (noflash)
- : default
- obj_a (noflash)
- : B = y
- obj_B (noflash
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_nonlast_default_3(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = y
- obj_A (noflash)
- :
- obj_a (noflash)
- : B = y
- obj_B (noflash
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_duplicate_default_1(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : CONFIG_A = y
- obj_A (noflash)
- : default
- obj_a (noflash)
- : CONFIG_B = y
- obj_B (noflash)
- : default
- obj_a (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_duplicate_default_2(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : CONFIG_A = y
- obj_A (noflash)
- : CONFIG_B = y
- obj_a (noflash)
- : default
- obj_B (noflash)
- :
- obj_a (noflash)
- """)
- with self.assertRaises(ParseException):
- FragmentFile(test_fragment, self.sdkconfig)
- def test_mixed_deprecated_mapping(self):
- test_fragment = self.create_fragment_file(u"""
- [mapping]
- archive: lib.a
- entries:
- : A = n
- obj_A (noflash)
- : default
- obj_B (noflash)
- [mapping:test]
- archive: lib.a
- entries:
- if A = n:
- obj_A (noflash)
- else:
- obj_B (noflash)
- """)
- fragment_file = FragmentFile(test_fragment, self.sdkconfig)
- self.assertEqual(2, len(fragment_file.fragments))
- self.assertEqual(fragment_file.fragments[0].entries,
- fragment_file.fragments[1].entries)
- if __name__ == '__main__':
- unittest.main()
|