2 # This file contain unit test for CommentParsing
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 import Logger
.Log
as Logger
11 from Library
.CommentParsing
import ParseHeaderCommentSection
, \
12 ParseGenericComment
, \
13 ParseDecPcdGenericComment
, \
14 ParseDecPcdTailComment
15 from Library
.CommentParsing
import _IsCopyrightLine
16 from Library
.StringUtils
import GetSplitValueList
17 from Library
.DataType
import TAB_SPACE_SPLIT
18 from Library
.DataType
import TAB_LANGUAGE_EN_US
21 # Test ParseHeaderCommentSection
23 class ParseHeaderCommentSectionTest(unittest
.TestCase
):
31 # Normal case1: have license/copyright/license above @file
33 def testNormalCase1(self
):
43 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
48 CommentList
= GetSplitValueList(TestCommentLines1
, "\n")
50 TestCommentLinesList
= []
51 for Comment
in CommentList
:
53 TestCommentLinesList
.append((Comment
, LineNum
))
55 Abstract
, Description
, Copyright
, License
= \
56 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
58 ExpectedAbstract
= 'example abstract'
59 self
.assertEqual(Abstract
, ExpectedAbstract
)
61 ExpectedDescription
= 'example description'
62 self
.assertEqual(Description
, ExpectedDescription
)
65 'Copyright (c) 2007 - 2010,'\
66 ' Intel Corporation. All rights reserved.<BR>'
67 self
.assertEqual(Copyright
, ExpectedCopyright
)
69 ExpectedLicense
= 'License1\nLicense2\n\nLicense3'
70 self
.assertEqual(License
, ExpectedLicense
)
73 # Normal case2: have license/copyright above @file, but no copyright after
75 def testNormalCase2(self
):
85 #Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
89 CommentList
= GetSplitValueList(TestCommentLines2
, "\n")
91 TestCommentLinesList
= []
92 for Comment
in CommentList
:
94 TestCommentLinesList
.append((Comment
, LineNum
))
96 Abstract
, Description
, Copyright
, License
= \
97 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
99 ExpectedAbstract
= 'example abstract'
100 self
.assertEqual(Abstract
, ExpectedAbstract
)
102 ExpectedDescription
= 'example description'
103 self
.assertEqual(Description
, ExpectedDescription
)
105 ExpectedCopyright
= \
106 'Copyright (c) 2007 - 2018, Intel Corporation.'\
107 ' All rights reserved.<BR>'
108 self
.assertEqual(Copyright
, ExpectedCopyright
)
110 ExpectedLicense
= 'License1\nLicense2'
111 self
.assertEqual(License
, ExpectedLicense
)
115 # Normal case2: have license/copyright/license above @file,
116 # but no abstract/description
118 def testNormalCase3(self
):
119 TestCommentLines3
= \
124 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
130 CommentList
= GetSplitValueList(TestCommentLines3
, "\n")
132 TestCommentLinesList
= []
133 for Comment
in CommentList
:
135 TestCommentLinesList
.append((Comment
, LineNum
))
137 Abstract
, Description
, Copyright
, License
= \
138 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
140 ExpectedAbstract
= ''
141 self
.assertEqual(Abstract
, ExpectedAbstract
)
143 ExpectedDescription
= ''
144 self
.assertEqual(Description
, ExpectedDescription
)
146 ExpectedCopyright
= \
147 'Copyright (c) 2007 - 2010,'\
148 ' Intel Corporation. All rights reserved.<BR>'
149 self
.assertEqual(Copyright
, ExpectedCopyright
)
156 self
.assertEqual(License
, ExpectedLicense
)
159 # Normal case4: format example in spec
161 def testNormalCase4(self
):
169 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
175 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
177 TestCommentLinesList
= []
178 for Comment
in CommentList
:
180 TestCommentLinesList
.append((Comment
, LineNum
))
182 Abstract
, Description
, Copyright
, License
= \
183 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
185 ExpectedAbstract
= 'Abstract'
186 self
.assertEqual(Abstract
, ExpectedAbstract
)
188 ExpectedDescription
= 'Description'
189 self
.assertEqual(Description
, ExpectedDescription
)
191 ExpectedCopyright
= \
192 'Copyright (c) 2007 - 2018, Intel Corporation.'\
193 ' All rights reserved.<BR>'
194 self
.assertEqual(Copyright
, ExpectedCopyright
)
198 self
.assertEqual(License
, ExpectedLicense
)
201 # Normal case5: other line between copyright
203 def testNormalCase5(self
):
211 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
213 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
219 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
221 TestCommentLinesList
= []
222 for Comment
in CommentList
:
224 TestCommentLinesList
.append((Comment
, LineNum
))
226 Abstract
, Description
, Copyright
, License
= \
227 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
229 ExpectedAbstract
= 'Abstract'
230 self
.assertEqual(Abstract
, ExpectedAbstract
)
232 ExpectedDescription
= 'Description'
233 self
.assertEqual(Description
, ExpectedDescription
)
235 ExpectedCopyright
= \
236 'Copyright (c) 2007 - 2018, Intel Corporation.'\
237 ' All rights reserved.<BR>\n'\
238 'Copyright (c) 2007 - 2018, Intel Corporation.'\
239 ' All rights reserved.<BR>'
240 self
.assertEqual(Copyright
, ExpectedCopyright
)
244 self
.assertEqual(License
, ExpectedLicense
)
247 # Normal case6: multiple lines of copyright
249 def testNormalCase6(self
):
257 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
258 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>
259 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>
265 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
267 TestCommentLinesList
= []
268 for Comment
in CommentList
:
270 TestCommentLinesList
.append((Comment
, LineNum
))
272 Abstract
, Description
, Copyright
, License
= \
273 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
275 ExpectedAbstract
= 'Abstract'
276 self
.assertEqual(Abstract
, ExpectedAbstract
)
278 ExpectedDescription
= 'Description'
279 self
.assertEqual(Description
, ExpectedDescription
)
281 ExpectedCopyright
= \
282 'Copyright (c) 2007 - 2018, Intel Corporation.'\
283 ' All rights reserved.<BR>\n'\
284 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\
285 ' All rights reserved.<BR>\n'\
286 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\
287 ' All rights reserved.<BR>'
288 self
.assertEqual(Copyright
, ExpectedCopyright
)
292 self
.assertEqual(License
, ExpectedLicense
)
295 # Normal case7: Abstract not present
297 def testNormalCase7(self
):
304 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
305 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>
306 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>
312 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
314 TestCommentLinesList
= []
315 for Comment
in CommentList
:
317 TestCommentLinesList
.append((Comment
, LineNum
))
319 Abstract
, Description
, Copyright
, License
= \
320 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
322 ExpectedAbstract
= ''
323 self
.assertEqual(Abstract
, ExpectedAbstract
)
325 ExpectedDescription
= 'Description'
326 self
.assertEqual(Description
, ExpectedDescription
)
328 ExpectedCopyright
= \
329 'Copyright (c) 2007 - 2018, Intel Corporation.'\
330 ' All rights reserved.<BR>\n'\
331 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\
332 ' All rights reserved.<BR>\n'\
333 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\
334 ' All rights reserved.<BR>'
335 self
.assertEqual(Copyright
, ExpectedCopyright
)
339 self
.assertEqual(License
, ExpectedLicense
)
342 # Normal case8: Description not present
344 def testNormalCase8(self
):
350 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
356 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
358 TestCommentLinesList
= []
359 for Comment
in CommentList
:
361 TestCommentLinesList
.append((Comment
, LineNum
))
363 Abstract
, Description
, Copyright
, License
= \
364 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
366 ExpectedAbstract
= 'Abstact'
367 self
.assertEqual(Abstract
, ExpectedAbstract
)
369 ExpectedDescription
= ''
370 self
.assertEqual(Description
, ExpectedDescription
)
372 ExpectedCopyright
= \
373 'Copyright (c) 2007 - 2018, Intel Corporation.'\
374 ' All rights reserved.<BR>'
375 self
.assertEqual(Copyright
, ExpectedCopyright
)
379 self
.assertEqual(License
, ExpectedLicense
)
382 # Error case1: No copyright found
384 def testErrorCase1(self
):
396 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
398 TestCommentLinesList
= []
399 for Comment
in CommentList
:
401 TestCommentLinesList
.append((Comment
, LineNum
))
403 self
.assertRaises(Logger
.FatalError
,
404 ParseHeaderCommentSection
,
405 TestCommentLinesList
,
409 # Error case2: non-empty non-comment lines passed in
411 def testErrorCase2(self
):
420 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
425 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
427 TestCommentLinesList
= []
428 for Comment
in CommentList
:
430 TestCommentLinesList
.append((Comment
, LineNum
))
432 self
.assertRaises(Logger
.FatalError
,
433 ParseHeaderCommentSection
,
434 TestCommentLinesList
,
438 # Test ParseGenericComment
440 class ParseGenericCommentTest(unittest
.TestCase
):
448 # Normal case1: one line of comment
450 def testNormalCase1(self
):
454 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
456 TestCommentLinesList
= []
457 for Comment
in CommentList
:
459 TestCommentLinesList
.append((Comment
, LineNum
))
461 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase1')
462 self
.failIf(not HelptxtObj
)
463 self
.assertEqual(HelptxtObj
.GetString(), 'hello world')
464 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
467 # Normal case2: multiple lines of comment
469 def testNormalCase2(self
):
474 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
476 TestCommentLinesList
= []
477 for Comment
in CommentList
:
479 TestCommentLinesList
.append((Comment
, LineNum
))
481 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase2')
482 self
.failIf(not HelptxtObj
)
483 self
.assertEqual(HelptxtObj
.GetString(),
484 'hello world\n' + 'second line')
485 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
488 # Normal case3: multiple lines of comment, non comment lines will be skipped
490 def testNormalCase3(self
):
493 This is not comment line'''
495 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
497 TestCommentLinesList
= []
498 for Comment
in CommentList
:
500 TestCommentLinesList
.append((Comment
, LineNum
))
502 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase3')
503 self
.failIf(not HelptxtObj
)
504 self
.assertEqual(HelptxtObj
.GetString(),
506 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
509 # Test ParseDecPcdGenericComment
511 class ParseDecPcdGenericCommentTest(unittest
.TestCase
):
519 # Normal case1: comments with no special comment
521 def testNormalCase1(self
):
526 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
528 TestCommentLinesList
= []
529 for Comment
in CommentList
:
531 TestCommentLinesList
.append((Comment
, LineNum
))
533 (HelpTxt
, PcdErr
) = \
534 ParseDecPcdGenericComment(TestCommentLinesList
, 'testNormalCase1')
535 self
.failIf(not HelpTxt
)
537 self
.assertEqual(HelpTxt
,
538 'hello world\n' + 'second line')
542 # Normal case2: comments with valid list
544 def testNormalCase2(self
):
551 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
553 TestCommentLinesList
= []
554 for Comment
in CommentList
:
556 TestCommentLinesList
.append((Comment
, LineNum
))
558 (HelpTxt
, PcdErr
) = \
559 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
560 self
.failIf(not HelpTxt
)
561 self
.failIf(not PcdErr
)
562 self
.assertEqual(HelpTxt
,
563 'hello world\n' + 'second line\n' + 'other line')
564 ExpectedList
= GetSplitValueList('1 2 3', TAB_SPACE_SPLIT
)
565 ActualList
= [item
for item
in \
566 GetSplitValueList(PcdErr
.GetValidValue(), TAB_SPACE_SPLIT
) if item
]
567 self
.assertEqual(ExpectedList
, ActualList
)
568 self
.failIf(PcdErr
.GetExpression())
569 self
.failIf(PcdErr
.GetValidValueRange())
572 # Normal case3: comments with valid range
574 def testNormalCase3(self
):
578 # @ValidRange LT 1 AND GT 2
581 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
583 TestCommentLinesList
= []
584 for Comment
in CommentList
:
586 TestCommentLinesList
.append((Comment
, LineNum
))
588 (HelpTxt
, PcdErr
) = \
589 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
590 self
.failIf(not HelpTxt
)
591 self
.failIf(not PcdErr
)
592 self
.assertEqual(HelpTxt
,
593 'hello world\n' + 'second line\n' + 'other line')
594 self
.assertEqual(PcdErr
.GetValidValueRange().strip(), 'LT 1 AND GT 2')
595 self
.failIf(PcdErr
.GetExpression())
596 self
.failIf(PcdErr
.GetValidValue())
599 # Normal case4: comments with valid expression
601 def testNormalCase4(self
):
605 # @Expression LT 1 AND GT 2
608 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
610 TestCommentLinesList
= []
611 for Comment
in CommentList
:
613 TestCommentLinesList
.append((Comment
, LineNum
))
615 (HelpTxt
, PcdErr
) = \
616 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
617 self
.failIf(not HelpTxt
)
618 self
.failIf(not PcdErr
)
619 self
.assertEqual(HelpTxt
,
620 'hello world\n' + 'second line\n' + 'other line')
621 self
.assertEqual(PcdErr
.GetExpression().strip(), 'LT 1 AND GT 2')
622 self
.failIf(PcdErr
.GetValidValueRange())
623 self
.failIf(PcdErr
.GetValidValue())
626 # Normal case5: comments with valid expression and no generic comment
628 def testNormalCase5(self
):
630 '''# @Expression LT 1 AND GT 2'''
632 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
634 TestCommentLinesList
= []
635 for Comment
in CommentList
:
637 TestCommentLinesList
.append((Comment
, LineNum
))
639 (HelpTxt
, PcdErr
) = \
640 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
642 self
.failIf(not PcdErr
)
643 self
.assertEqual(PcdErr
.GetExpression().strip(), 'LT 1 AND GT 2')
644 self
.failIf(PcdErr
.GetValidValueRange())
645 self
.failIf(PcdErr
.GetValidValue())
648 # Normal case6: comments with only generic help text
650 def testNormalCase6(self
):
654 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
656 TestCommentLinesList
= []
657 for Comment
in CommentList
:
659 TestCommentLinesList
.append((Comment
, LineNum
))
661 (HelpTxt
, PcdErr
) = \
662 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
663 self
.assertEqual(HelpTxt
, '\n')
669 # Error case1: comments with both expression and valid list, use later
670 # ignore the former and with a warning message
672 def testErrorCase1(self
):
677 # @Expression LT 1 AND GT 2
680 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
682 TestCommentLinesList
= []
683 for Comment
in CommentList
:
685 TestCommentLinesList
.append((Comment
, LineNum
))
688 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
689 except Logger
.FatalError
:
693 # Test ParseDecPcdTailComment
695 class ParseDecPcdTailCommentTest(unittest
.TestCase
):
703 # Normal case1: comments with no SupModeList
705 def testNormalCase1(self
):
707 '''## #hello world'''
709 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
711 TestCommentLinesList
= []
712 for Comment
in CommentList
:
714 TestCommentLinesList
.append((Comment
, LineNum
))
716 (SupModeList
, HelpStr
) = \
717 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
718 self
.failIf(not HelpStr
)
719 self
.failIf(SupModeList
)
720 self
.assertEqual(HelpStr
,
724 # Normal case2: comments with one SupMode
726 def testNormalCase2(self
):
728 '''## BASE #hello world'''
730 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
732 TestCommentLinesList
= []
733 for Comment
in CommentList
:
735 TestCommentLinesList
.append((Comment
, LineNum
))
737 (SupModeList
, HelpStr
) = \
738 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
739 self
.failIf(not HelpStr
)
740 self
.failIf(not SupModeList
)
741 self
.assertEqual(HelpStr
,
743 self
.assertEqual(SupModeList
,
747 # Normal case3: comments with more than one SupMode
749 def testNormalCase3(self
):
751 '''## BASE UEFI_APPLICATION #hello world'''
753 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
755 TestCommentLinesList
= []
756 for Comment
in CommentList
:
758 TestCommentLinesList
.append((Comment
, LineNum
))
760 (SupModeList
, HelpStr
) = \
761 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
762 self
.failIf(not HelpStr
)
763 self
.failIf(not SupModeList
)
764 self
.assertEqual(HelpStr
,
766 self
.assertEqual(SupModeList
,
767 ['BASE', 'UEFI_APPLICATION'])
770 # Normal case4: comments with more than one SupMode, no help text
772 def testNormalCase4(self
):
774 '''## BASE UEFI_APPLICATION'''
776 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
778 TestCommentLinesList
= []
779 for Comment
in CommentList
:
781 TestCommentLinesList
.append((Comment
, LineNum
))
783 (SupModeList
, HelpStr
) = \
784 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
786 self
.failIf(not SupModeList
)
787 self
.assertEqual(SupModeList
,
788 ['BASE', 'UEFI_APPLICATION'])
791 # Normal case5: general comments with no supModList, extract from real case
793 def testNormalCase5(self
):
795 ''' # 1 = 128MB, 2 = 256MB, 3 = MAX'''
797 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
799 TestCommentLinesList
= []
800 for Comment
in CommentList
:
802 TestCommentLinesList
.append((Comment
, LineNum
))
804 (SupModeList
, HelpStr
) = \
805 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
806 self
.failIf(not HelpStr
)
807 self
.assertEqual(HelpStr
,
808 '1 = 128MB, 2 = 256MB, 3 = MAX')
809 self
.failIf(SupModeList
)
813 # Error case2: comments with supModList contains valid and invalid
816 def testErrorCase2(self
):
818 '''## BASE INVALID_MODULE_TYPE #hello world'''
820 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
822 TestCommentLinesList
= []
823 for Comment
in CommentList
:
825 TestCommentLinesList
.append((Comment
, LineNum
))
828 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
829 except Logger
.FatalError
:
834 # Test _IsCopyrightLine
836 class _IsCopyrightLineTest(unittest
.TestCase
):
847 Line
= 'this is a copyright ( line'
848 Result
= _IsCopyrightLine(Line
)
849 self
.failIf(not Result
)
855 Line
= 'this is a Copyright ( line'
856 Result
= _IsCopyrightLine(Line
)
857 self
.failIf(not Result
)
863 Line
= 'this is not aCopyright ( line'
864 Result
= _IsCopyrightLine(Line
)
871 Line
= 'this is Copyright( line'
872 Result
= _IsCopyrightLine(Line
)
873 self
.failIf(not Result
)
879 Line
= 'this is Copyright (line'
880 Result
= _IsCopyrightLine(Line
)
881 self
.failIf(not Result
)
887 Line
= 'this is not Copyright line'
888 Result
= _IsCopyrightLine(Line
)
895 Line
= 'Copyright (c) line'
896 Result
= _IsCopyrightLine(Line
)
897 self
.failIf(not Result
)
903 Line
= ' Copyright (c) line'
904 Result
= _IsCopyrightLine(Line
)
905 self
.failIf(not Result
)
911 Line
= 'not a Copyright '
912 Result
= _IsCopyrightLine(Line
)
915 if __name__
== '__main__':