2 # This file contain unit test for CommentParsing
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 import Logger
.Log
as Logger
17 from Library
.CommentParsing
import ParseHeaderCommentSection
, \
18 ParseGenericComment
, \
19 ParseDecPcdGenericComment
, \
20 ParseDecPcdTailComment
21 from Library
.CommentParsing
import _IsCopyrightLine
22 from Library
.StringUtils
import GetSplitValueList
23 from Library
.DataType
import TAB_SPACE_SPLIT
24 from Library
.DataType
import TAB_LANGUAGE_EN_US
27 # Test ParseHeaderCommentSection
29 class ParseHeaderCommentSectionTest(unittest
.TestCase
):
37 # Normal case1: have license/copyright/license above @file
39 def testNormalCase1(self
):
49 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
54 CommentList
= GetSplitValueList(TestCommentLines1
, "\n")
56 TestCommentLinesList
= []
57 for Comment
in CommentList
:
59 TestCommentLinesList
.append((Comment
, LineNum
))
61 Abstract
, Description
, Copyright
, License
= \
62 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
64 ExpectedAbstract
= 'example abstract'
65 self
.assertEqual(Abstract
, ExpectedAbstract
)
67 ExpectedDescription
= 'example description'
68 self
.assertEqual(Description
, ExpectedDescription
)
71 'Copyright (c) 2007 - 2010,'\
72 ' Intel Corporation. All rights reserved.<BR>'
73 self
.assertEqual(Copyright
, ExpectedCopyright
)
75 ExpectedLicense
= 'License1\nLicense2\n\nLicense3'
76 self
.assertEqual(License
, ExpectedLicense
)
79 # Normal case2: have license/copyright above @file, but no copyright after
81 def testNormalCase2(self
):
91 #Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
95 CommentList
= GetSplitValueList(TestCommentLines2
, "\n")
97 TestCommentLinesList
= []
98 for Comment
in CommentList
:
100 TestCommentLinesList
.append((Comment
, LineNum
))
102 Abstract
, Description
, Copyright
, License
= \
103 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
105 ExpectedAbstract
= 'example abstract'
106 self
.assertEqual(Abstract
, ExpectedAbstract
)
108 ExpectedDescription
= 'example description'
109 self
.assertEqual(Description
, ExpectedDescription
)
111 ExpectedCopyright
= \
112 'Copyright (c) 2007 - 2010, Intel Corporation.'\
113 ' All rights reserved.<BR>'
114 self
.assertEqual(Copyright
, ExpectedCopyright
)
116 ExpectedLicense
= 'License1\nLicense2'
117 self
.assertEqual(License
, ExpectedLicense
)
121 # Normal case2: have license/copyright/license above @file,
122 # but no abstract/description
124 def testNormalCase3(self
):
125 TestCommentLines3
= \
130 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
136 CommentList
= GetSplitValueList(TestCommentLines3
, "\n")
138 TestCommentLinesList
= []
139 for Comment
in CommentList
:
141 TestCommentLinesList
.append((Comment
, LineNum
))
143 Abstract
, Description
, Copyright
, License
= \
144 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
146 ExpectedAbstract
= ''
147 self
.assertEqual(Abstract
, ExpectedAbstract
)
149 ExpectedDescription
= ''
150 self
.assertEqual(Description
, ExpectedDescription
)
152 ExpectedCopyright
= \
153 'Copyright (c) 2007 - 2010,'\
154 ' Intel Corporation. All rights reserved.<BR>'
155 self
.assertEqual(Copyright
, ExpectedCopyright
)
162 self
.assertEqual(License
, ExpectedLicense
)
165 # Normal case4: format example in spec
167 def testNormalCase4(self
):
175 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
181 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
183 TestCommentLinesList
= []
184 for Comment
in CommentList
:
186 TestCommentLinesList
.append((Comment
, LineNum
))
188 Abstract
, Description
, Copyright
, License
= \
189 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
191 ExpectedAbstract
= 'Abstract'
192 self
.assertEqual(Abstract
, ExpectedAbstract
)
194 ExpectedDescription
= 'Description'
195 self
.assertEqual(Description
, ExpectedDescription
)
197 ExpectedCopyright
= \
198 'Copyright (c) 2007 - 2010, Intel Corporation.'\
199 ' All rights reserved.<BR>'
200 self
.assertEqual(Copyright
, ExpectedCopyright
)
204 self
.assertEqual(License
, ExpectedLicense
)
207 # Normal case5: other line between copyright
209 def testNormalCase5(self
):
217 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
219 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
225 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
227 TestCommentLinesList
= []
228 for Comment
in CommentList
:
230 TestCommentLinesList
.append((Comment
, LineNum
))
232 Abstract
, Description
, Copyright
, License
= \
233 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
235 ExpectedAbstract
= 'Abstract'
236 self
.assertEqual(Abstract
, ExpectedAbstract
)
238 ExpectedDescription
= 'Description'
239 self
.assertEqual(Description
, ExpectedDescription
)
241 ExpectedCopyright
= \
242 'Copyright (c) 2007 - 2010, Intel Corporation.'\
243 ' All rights reserved.<BR>\n'\
244 'Copyright (c) 2007 - 2010, Intel Corporation.'\
245 ' All rights reserved.<BR>'
246 self
.assertEqual(Copyright
, ExpectedCopyright
)
250 self
.assertEqual(License
, ExpectedLicense
)
253 # Normal case6: multiple lines of copyright
255 def testNormalCase6(self
):
263 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
264 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>
265 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>
271 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
273 TestCommentLinesList
= []
274 for Comment
in CommentList
:
276 TestCommentLinesList
.append((Comment
, LineNum
))
278 Abstract
, Description
, Copyright
, License
= \
279 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
281 ExpectedAbstract
= 'Abstract'
282 self
.assertEqual(Abstract
, ExpectedAbstract
)
284 ExpectedDescription
= 'Description'
285 self
.assertEqual(Description
, ExpectedDescription
)
287 ExpectedCopyright
= \
288 'Copyright (c) 2007 - 2010, Intel Corporation.'\
289 ' All rights reserved.<BR>\n'\
290 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\
291 ' All rights reserved.<BR>\n'\
292 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\
293 ' All rights reserved.<BR>'
294 self
.assertEqual(Copyright
, ExpectedCopyright
)
298 self
.assertEqual(License
, ExpectedLicense
)
301 # Normal case7: Abstract not present
303 def testNormalCase7(self
):
310 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
311 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>
312 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>
318 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
320 TestCommentLinesList
= []
321 for Comment
in CommentList
:
323 TestCommentLinesList
.append((Comment
, LineNum
))
325 Abstract
, Description
, Copyright
, License
= \
326 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
328 ExpectedAbstract
= ''
329 self
.assertEqual(Abstract
, ExpectedAbstract
)
331 ExpectedDescription
= 'Description'
332 self
.assertEqual(Description
, ExpectedDescription
)
334 ExpectedCopyright
= \
335 'Copyright (c) 2007 - 2010, Intel Corporation.'\
336 ' All rights reserved.<BR>\n'\
337 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\
338 ' All rights reserved.<BR>\n'\
339 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\
340 ' All rights reserved.<BR>'
341 self
.assertEqual(Copyright
, ExpectedCopyright
)
345 self
.assertEqual(License
, ExpectedLicense
)
348 # Normal case8: Description not present
350 def testNormalCase8(self
):
356 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
362 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
364 TestCommentLinesList
= []
365 for Comment
in CommentList
:
367 TestCommentLinesList
.append((Comment
, LineNum
))
369 Abstract
, Description
, Copyright
, License
= \
370 ParseHeaderCommentSection(TestCommentLinesList
, "PhonyFile")
372 ExpectedAbstract
= 'Abstact'
373 self
.assertEqual(Abstract
, ExpectedAbstract
)
375 ExpectedDescription
= ''
376 self
.assertEqual(Description
, ExpectedDescription
)
378 ExpectedCopyright
= \
379 'Copyright (c) 2007 - 2010, Intel Corporation.'\
380 ' All rights reserved.<BR>'
381 self
.assertEqual(Copyright
, ExpectedCopyright
)
385 self
.assertEqual(License
, ExpectedLicense
)
388 # Error case1: No copyright found
390 def testErrorCase1(self
):
402 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
404 TestCommentLinesList
= []
405 for Comment
in CommentList
:
407 TestCommentLinesList
.append((Comment
, LineNum
))
409 self
.assertRaises(Logger
.FatalError
,
410 ParseHeaderCommentSection
,
411 TestCommentLinesList
,
415 # Error case2: non-empty non-comment lines passed in
417 def testErrorCase2(self
):
426 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
431 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
433 TestCommentLinesList
= []
434 for Comment
in CommentList
:
436 TestCommentLinesList
.append((Comment
, LineNum
))
438 self
.assertRaises(Logger
.FatalError
,
439 ParseHeaderCommentSection
,
440 TestCommentLinesList
,
444 # Test ParseGenericComment
446 class ParseGenericCommentTest(unittest
.TestCase
):
454 # Normal case1: one line of comment
456 def testNormalCase1(self
):
460 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
462 TestCommentLinesList
= []
463 for Comment
in CommentList
:
465 TestCommentLinesList
.append((Comment
, LineNum
))
467 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase1')
468 self
.failIf(not HelptxtObj
)
469 self
.assertEqual(HelptxtObj
.GetString(), 'hello world')
470 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
473 # Normal case2: multiple lines of comment
475 def testNormalCase2(self
):
480 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
482 TestCommentLinesList
= []
483 for Comment
in CommentList
:
485 TestCommentLinesList
.append((Comment
, LineNum
))
487 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase2')
488 self
.failIf(not HelptxtObj
)
489 self
.assertEqual(HelptxtObj
.GetString(),
490 'hello world\n' + 'second line')
491 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
494 # Normal case3: multiple lines of comment, non comment lines will be skipped
496 def testNormalCase3(self
):
499 This is not comment line'''
501 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
503 TestCommentLinesList
= []
504 for Comment
in CommentList
:
506 TestCommentLinesList
.append((Comment
, LineNum
))
508 HelptxtObj
= ParseGenericComment(TestCommentLinesList
, 'testNormalCase3')
509 self
.failIf(not HelptxtObj
)
510 self
.assertEqual(HelptxtObj
.GetString(),
512 self
.assertEqual(HelptxtObj
.GetLang(), TAB_LANGUAGE_EN_US
)
515 # Test ParseDecPcdGenericComment
517 class ParseDecPcdGenericCommentTest(unittest
.TestCase
):
525 # Normal case1: comments with no special comment
527 def testNormalCase1(self
):
532 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
534 TestCommentLinesList
= []
535 for Comment
in CommentList
:
537 TestCommentLinesList
.append((Comment
, LineNum
))
539 (HelpTxt
, PcdErr
) = \
540 ParseDecPcdGenericComment(TestCommentLinesList
, 'testNormalCase1')
541 self
.failIf(not HelpTxt
)
543 self
.assertEqual(HelpTxt
,
544 'hello world\n' + 'second line')
548 # Normal case2: comments with valid list
550 def testNormalCase2(self
):
557 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
559 TestCommentLinesList
= []
560 for Comment
in CommentList
:
562 TestCommentLinesList
.append((Comment
, LineNum
))
564 (HelpTxt
, PcdErr
) = \
565 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
566 self
.failIf(not HelpTxt
)
567 self
.failIf(not PcdErr
)
568 self
.assertEqual(HelpTxt
,
569 'hello world\n' + 'second line\n' + 'other line')
570 ExpectedList
= GetSplitValueList('1 2 3', TAB_SPACE_SPLIT
)
571 ActualList
= [item
for item
in \
572 GetSplitValueList(PcdErr
.GetValidValue(), TAB_SPACE_SPLIT
) if item
]
573 self
.assertEqual(ExpectedList
, ActualList
)
574 self
.failIf(PcdErr
.GetExpression())
575 self
.failIf(PcdErr
.GetValidValueRange())
578 # Normal case3: comments with valid range
580 def testNormalCase3(self
):
584 # @ValidRange LT 1 AND GT 2
587 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
589 TestCommentLinesList
= []
590 for Comment
in CommentList
:
592 TestCommentLinesList
.append((Comment
, LineNum
))
594 (HelpTxt
, PcdErr
) = \
595 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
596 self
.failIf(not HelpTxt
)
597 self
.failIf(not PcdErr
)
598 self
.assertEqual(HelpTxt
,
599 'hello world\n' + 'second line\n' + 'other line')
600 self
.assertEqual(PcdErr
.GetValidValueRange().strip(), 'LT 1 AND GT 2')
601 self
.failIf(PcdErr
.GetExpression())
602 self
.failIf(PcdErr
.GetValidValue())
605 # Normal case4: comments with valid expression
607 def testNormalCase4(self
):
611 # @Expression LT 1 AND GT 2
614 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
616 TestCommentLinesList
= []
617 for Comment
in CommentList
:
619 TestCommentLinesList
.append((Comment
, LineNum
))
621 (HelpTxt
, PcdErr
) = \
622 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
623 self
.failIf(not HelpTxt
)
624 self
.failIf(not PcdErr
)
625 self
.assertEqual(HelpTxt
,
626 'hello world\n' + 'second line\n' + 'other line')
627 self
.assertEqual(PcdErr
.GetExpression().strip(), 'LT 1 AND GT 2')
628 self
.failIf(PcdErr
.GetValidValueRange())
629 self
.failIf(PcdErr
.GetValidValue())
632 # Normal case5: comments with valid expression and no generic comment
634 def testNormalCase5(self
):
636 '''# @Expression LT 1 AND GT 2'''
638 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
640 TestCommentLinesList
= []
641 for Comment
in CommentList
:
643 TestCommentLinesList
.append((Comment
, LineNum
))
645 (HelpTxt
, PcdErr
) = \
646 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
648 self
.failIf(not PcdErr
)
649 self
.assertEqual(PcdErr
.GetExpression().strip(), 'LT 1 AND GT 2')
650 self
.failIf(PcdErr
.GetValidValueRange())
651 self
.failIf(PcdErr
.GetValidValue())
654 # Normal case6: comments with only generic help text
656 def testNormalCase6(self
):
660 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
662 TestCommentLinesList
= []
663 for Comment
in CommentList
:
665 TestCommentLinesList
.append((Comment
, LineNum
))
667 (HelpTxt
, PcdErr
) = \
668 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
669 self
.assertEqual(HelpTxt
, '\n')
675 # Error case1: comments with both expression and valid list, use later
676 # ignore the former and with a warning message
678 def testErrorCase1(self
):
683 # @Expression LT 1 AND GT 2
686 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
688 TestCommentLinesList
= []
689 for Comment
in CommentList
:
691 TestCommentLinesList
.append((Comment
, LineNum
))
694 ParseDecPcdGenericComment(TestCommentLinesList
, 'UnitTest')
695 except Logger
.FatalError
:
699 # Test ParseDecPcdTailComment
701 class ParseDecPcdTailCommentTest(unittest
.TestCase
):
709 # Normal case1: comments with no SupModeList
711 def testNormalCase1(self
):
713 '''## #hello world'''
715 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
717 TestCommentLinesList
= []
718 for Comment
in CommentList
:
720 TestCommentLinesList
.append((Comment
, LineNum
))
722 (SupModeList
, HelpStr
) = \
723 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
724 self
.failIf(not HelpStr
)
725 self
.failIf(SupModeList
)
726 self
.assertEqual(HelpStr
,
730 # Normal case2: comments with one SupMode
732 def testNormalCase2(self
):
734 '''## BASE #hello world'''
736 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
738 TestCommentLinesList
= []
739 for Comment
in CommentList
:
741 TestCommentLinesList
.append((Comment
, LineNum
))
743 (SupModeList
, HelpStr
) = \
744 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
745 self
.failIf(not HelpStr
)
746 self
.failIf(not SupModeList
)
747 self
.assertEqual(HelpStr
,
749 self
.assertEqual(SupModeList
,
753 # Normal case3: comments with more than one SupMode
755 def testNormalCase3(self
):
757 '''## BASE UEFI_APPLICATION #hello world'''
759 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
761 TestCommentLinesList
= []
762 for Comment
in CommentList
:
764 TestCommentLinesList
.append((Comment
, LineNum
))
766 (SupModeList
, HelpStr
) = \
767 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
768 self
.failIf(not HelpStr
)
769 self
.failIf(not SupModeList
)
770 self
.assertEqual(HelpStr
,
772 self
.assertEqual(SupModeList
,
773 ['BASE', 'UEFI_APPLICATION'])
776 # Normal case4: comments with more than one SupMode, no help text
778 def testNormalCase4(self
):
780 '''## BASE UEFI_APPLICATION'''
782 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
784 TestCommentLinesList
= []
785 for Comment
in CommentList
:
787 TestCommentLinesList
.append((Comment
, LineNum
))
789 (SupModeList
, HelpStr
) = \
790 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
792 self
.failIf(not SupModeList
)
793 self
.assertEqual(SupModeList
,
794 ['BASE', 'UEFI_APPLICATION'])
797 # Normal case5: general comments with no supModList, extract from real case
799 def testNormalCase5(self
):
801 ''' # 1 = 128MB, 2 = 256MB, 3 = MAX'''
803 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
805 TestCommentLinesList
= []
806 for Comment
in CommentList
:
808 TestCommentLinesList
.append((Comment
, LineNum
))
810 (SupModeList
, HelpStr
) = \
811 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
812 self
.failIf(not HelpStr
)
813 self
.assertEqual(HelpStr
,
814 '1 = 128MB, 2 = 256MB, 3 = MAX')
815 self
.failIf(SupModeList
)
819 # Error case2: comments with supModList contains valid and invalid
822 def testErrorCase2(self
):
824 '''## BASE INVALID_MODULE_TYPE #hello world'''
826 CommentList
= GetSplitValueList(TestCommentLines
, "\n")
828 TestCommentLinesList
= []
829 for Comment
in CommentList
:
831 TestCommentLinesList
.append((Comment
, LineNum
))
834 ParseDecPcdTailComment(TestCommentLinesList
, 'UnitTest')
835 except Logger
.FatalError
:
840 # Test _IsCopyrightLine
842 class _IsCopyrightLineTest(unittest
.TestCase
):
853 Line
= 'this is a copyright ( line'
854 Result
= _IsCopyrightLine(Line
)
855 self
.failIf(not Result
)
861 Line
= 'this is a Copyright ( line'
862 Result
= _IsCopyrightLine(Line
)
863 self
.failIf(not Result
)
869 Line
= 'this is not aCopyright ( line'
870 Result
= _IsCopyrightLine(Line
)
877 Line
= 'this is Copyright( line'
878 Result
= _IsCopyrightLine(Line
)
879 self
.failIf(not Result
)
885 Line
= 'this is Copyright (line'
886 Result
= _IsCopyrightLine(Line
)
887 self
.failIf(not Result
)
893 Line
= 'this is not Copyright line'
894 Result
= _IsCopyrightLine(Line
)
901 Line
= 'Copyright (c) line'
902 Result
= _IsCopyrightLine(Line
)
903 self
.failIf(not Result
)
909 Line
= ' Copyright (c) line'
910 Result
= _IsCopyrightLine(Line
)
911 self
.failIf(not Result
)
917 Line
= 'not a Copyright '
918 Result
= _IsCopyrightLine(Line
)
921 if __name__
== '__main__':