]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/UnitTest/CommentParsingUnitTest.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / UnitTest / CommentParsingUnitTest.py
CommitLineData
4234283c
LG
1## @file\r
2# This file contain unit test for CommentParsing\r
3#\r
64285f15 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
4234283c
LG
7\r
8import unittest\r
9\r
10import Logger.Log as Logger\r
11from Library.CommentParsing import ParseHeaderCommentSection, \\r
12 ParseGenericComment, \\r
13 ParseDecPcdGenericComment, \\r
14 ParseDecPcdTailComment\r
15from Library.CommentParsing import _IsCopyrightLine\r
64285f15 16from Library.StringUtils import GetSplitValueList\r
4234283c 17from Library.DataType import TAB_SPACE_SPLIT\r
421ccda3 18from Library.DataType import TAB_LANGUAGE_EN_US\r
4234283c
LG
19\r
20#\r
21# Test ParseHeaderCommentSection\r
22#\r
23class ParseHeaderCommentSectionTest(unittest.TestCase):\r
24 def setUp(self):\r
25 pass\r
26\r
27 def tearDown(self):\r
28 pass\r
f7496d71 29\r
4234283c
LG
30 #\r
31 # Normal case1: have license/copyright/license above @file\r
32 #\r
33 def testNormalCase1(self):\r
34 TestCommentLines1 = \\r
35 '''# License1\r
36 # License2\r
37 #\r
38 ## @file\r
f7496d71
LG
39 # example abstract\r
40 #\r
4234283c 41 # example description\r
f7496d71
LG
42 #\r
43 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
44 #\r
45 # License3\r
4234283c 46 #'''\r
f7496d71 47\r
4234283c
LG
48 CommentList = GetSplitValueList(TestCommentLines1, "\n")\r
49 LineNum = 0\r
50 TestCommentLinesList = []\r
51 for Comment in CommentList:\r
52 LineNum += 1\r
53 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 54\r
4234283c
LG
55 Abstract, Description, Copyright, License = \\r
56 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 57\r
4234283c
LG
58 ExpectedAbstract = 'example abstract'\r
59 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 60\r
4234283c
LG
61 ExpectedDescription = 'example description'\r
62 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 63\r
4234283c
LG
64 ExpectedCopyright = \\r
65 'Copyright (c) 2007 - 2010,'\\r
66 ' Intel Corporation. All rights reserved.<BR>'\r
67 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 68\r
4234283c
LG
69 ExpectedLicense = 'License1\nLicense2\n\nLicense3'\r
70 self.assertEqual(License, ExpectedLicense)\r
71\r
72 #\r
73 # Normal case2: have license/copyright above @file, but no copyright after\r
74 #\r
75 def testNormalCase2(self):\r
76 TestCommentLines2 = \\r
77 ''' # License1\r
78 # License2\r
79 #\r
80 ## @file\r
f7496d71
LG
81 # example abstract\r
82 #\r
4234283c
LG
83 # example description\r
84 #\r
f7496d71 85 #Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
86 #\r
87 ##'''\r
f7496d71 88\r
4234283c
LG
89 CommentList = GetSplitValueList(TestCommentLines2, "\n")\r
90 LineNum = 0\r
91 TestCommentLinesList = []\r
92 for Comment in CommentList:\r
93 LineNum += 1\r
94 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 95\r
4234283c
LG
96 Abstract, Description, Copyright, License = \\r
97 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 98\r
4234283c
LG
99 ExpectedAbstract = 'example abstract'\r
100 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 101\r
4234283c
LG
102 ExpectedDescription = 'example description'\r
103 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 104\r
4234283c 105 ExpectedCopyright = \\r
f7496d71 106 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
107 ' All rights reserved.<BR>'\r
108 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 109\r
4234283c
LG
110 ExpectedLicense = 'License1\nLicense2'\r
111 self.assertEqual(License, ExpectedLicense)\r
f7496d71 112\r
4234283c
LG
113\r
114 #\r
f7496d71 115 # Normal case2: have license/copyright/license above @file,\r
4234283c
LG
116 # but no abstract/description\r
117 #\r
118 def testNormalCase3(self):\r
119 TestCommentLines3 = \\r
120 ''' # License1\r
121 # License2\r
122 #\r
f7496d71
LG
123 ## @file\r
124 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
125 #\r
126 # License3 Line1\r
127 # License3 Line2\r
128 ##'''\r
f7496d71 129\r
4234283c
LG
130 CommentList = GetSplitValueList(TestCommentLines3, "\n")\r
131 LineNum = 0\r
132 TestCommentLinesList = []\r
133 for Comment in CommentList:\r
134 LineNum += 1\r
135 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 136\r
4234283c
LG
137 Abstract, Description, Copyright, License = \\r
138 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 139\r
4234283c
LG
140 ExpectedAbstract = ''\r
141 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 142\r
4234283c
LG
143 ExpectedDescription = ''\r
144 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 145\r
4234283c
LG
146 ExpectedCopyright = \\r
147 'Copyright (c) 2007 - 2010,'\\r
148 ' Intel Corporation. All rights reserved.<BR>'\r
149 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 150\r
4234283c
LG
151 ExpectedLicense = \\r
152 'License1\n' \\r
153 'License2\n\n' \\r
154 'License3 Line1\n' \\r
155 'License3 Line2'\r
f7496d71
LG
156 self.assertEqual(License, ExpectedLicense)\r
157\r
4234283c
LG
158 #\r
159 # Normal case4: format example in spec\r
160 #\r
161 def testNormalCase4(self):\r
162 TestCommentLines = \\r
163 '''\r
164 ## @file\r
165 # Abstract\r
166 #\r
167 # Description\r
168 #\r
f7496d71 169 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
170 #\r
171 # License\r
172 #\r
173 ##'''\r
f7496d71 174\r
4234283c
LG
175 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
176 LineNum = 0\r
177 TestCommentLinesList = []\r
178 for Comment in CommentList:\r
179 LineNum += 1\r
180 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 181\r
4234283c
LG
182 Abstract, Description, Copyright, License = \\r
183 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 184\r
4234283c
LG
185 ExpectedAbstract = 'Abstract'\r
186 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 187\r
4234283c
LG
188 ExpectedDescription = 'Description'\r
189 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 190\r
4234283c 191 ExpectedCopyright = \\r
f7496d71 192 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
193 ' All rights reserved.<BR>'\r
194 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 195\r
4234283c
LG
196 ExpectedLicense = \\r
197 'License'\r
198 self.assertEqual(License, ExpectedLicense)\r
199\r
200 #\r
201 # Normal case5: other line between copyright\r
202 #\r
203 def testNormalCase5(self):\r
204 TestCommentLines = \\r
205 '''\r
206 ## @file\r
207 # Abstract\r
208 #\r
209 # Description\r
210 #\r
f7496d71
LG
211 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
212 # other line\r
213 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
214 #\r
215 # License\r
216 #\r
217 ##'''\r
f7496d71 218\r
4234283c
LG
219 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
220 LineNum = 0\r
221 TestCommentLinesList = []\r
222 for Comment in CommentList:\r
223 LineNum += 1\r
224 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 225\r
4234283c
LG
226 Abstract, Description, Copyright, License = \\r
227 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 228\r
4234283c
LG
229 ExpectedAbstract = 'Abstract'\r
230 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 231\r
4234283c
LG
232 ExpectedDescription = 'Description'\r
233 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 234\r
4234283c 235 ExpectedCopyright = \\r
f7496d71 236 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c 237 ' All rights reserved.<BR>\n'\\r
f7496d71 238 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
239 ' All rights reserved.<BR>'\r
240 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 241\r
4234283c
LG
242 ExpectedLicense = \\r
243 'License'\r
244 self.assertEqual(License, ExpectedLicense)\r
245\r
246 #\r
247 # Normal case6: multiple lines of copyright\r
248 #\r
249 def testNormalCase6(self):\r
250 TestCommentLines = \\r
251 '''\r
252 ## @file\r
253 # Abstract\r
254 #\r
255 # Description\r
256 #\r
f7496d71
LG
257 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
258 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>\r
4234283c
LG
259 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>\r
260 #\r
261 # License\r
262 #\r
263 ##'''\r
f7496d71 264\r
4234283c
LG
265 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
266 LineNum = 0\r
267 TestCommentLinesList = []\r
268 for Comment in CommentList:\r
269 LineNum += 1\r
270 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 271\r
4234283c
LG
272 Abstract, Description, Copyright, License = \\r
273 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 274\r
4234283c
LG
275 ExpectedAbstract = 'Abstract'\r
276 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 277\r
4234283c
LG
278 ExpectedDescription = 'Description'\r
279 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 280\r
4234283c 281 ExpectedCopyright = \\r
f7496d71 282 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
283 ' All rights reserved.<BR>\n'\\r
284 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\\r
285 ' All rights reserved.<BR>\n'\\r
286 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\\r
287 ' All rights reserved.<BR>'\r
288 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 289\r
4234283c
LG
290 ExpectedLicense = \\r
291 'License'\r
292 self.assertEqual(License, ExpectedLicense)\r
293\r
294 #\r
295 # Normal case7: Abstract not present\r
296 #\r
297 def testNormalCase7(self):\r
298 TestCommentLines = \\r
299 '''\r
300 ## @file\r
301 #\r
302 # Description\r
303 #\r
f7496d71
LG
304 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
305 # Copyright (c) 2007 - 2010, FOO1 Corporation. All rights reserved.<BR>\r
4234283c
LG
306 # Copyright (c) 2007 - 2010, FOO2 Corporation. All rights reserved.<BR>\r
307 #\r
308 # License\r
309 #\r
310 ##'''\r
f7496d71 311\r
4234283c
LG
312 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
313 LineNum = 0\r
314 TestCommentLinesList = []\r
315 for Comment in CommentList:\r
316 LineNum += 1\r
317 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 318\r
4234283c
LG
319 Abstract, Description, Copyright, License = \\r
320 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 321\r
4234283c
LG
322 ExpectedAbstract = ''\r
323 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 324\r
4234283c
LG
325 ExpectedDescription = 'Description'\r
326 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 327\r
4234283c 328 ExpectedCopyright = \\r
f7496d71 329 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
330 ' All rights reserved.<BR>\n'\\r
331 'Copyright (c) 2007 - 2010, FOO1 Corporation.'\\r
332 ' All rights reserved.<BR>\n'\\r
333 'Copyright (c) 2007 - 2010, FOO2 Corporation.'\\r
334 ' All rights reserved.<BR>'\r
335 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 336\r
4234283c
LG
337 ExpectedLicense = \\r
338 'License'\r
339 self.assertEqual(License, ExpectedLicense)\r
340\r
341 #\r
342 # Normal case8: Description not present\r
343 #\r
344 def testNormalCase8(self):\r
345 TestCommentLines = \\r
346 '''\r
347 ## @file\r
348 # Abstact\r
349 #\r
f7496d71 350 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
351 #\r
352 # License\r
353 #\r
354 ##'''\r
f7496d71 355\r
4234283c
LG
356 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
357 LineNum = 0\r
358 TestCommentLinesList = []\r
359 for Comment in CommentList:\r
360 LineNum += 1\r
361 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 362\r
4234283c
LG
363 Abstract, Description, Copyright, License = \\r
364 ParseHeaderCommentSection(TestCommentLinesList, "PhonyFile")\r
f7496d71 365\r
4234283c
LG
366 ExpectedAbstract = 'Abstact'\r
367 self.assertEqual(Abstract, ExpectedAbstract)\r
f7496d71 368\r
4234283c
LG
369 ExpectedDescription = ''\r
370 self.assertEqual(Description, ExpectedDescription)\r
f7496d71 371\r
4234283c 372 ExpectedCopyright = \\r
f7496d71 373 'Copyright (c) 2007 - 2018, Intel Corporation.'\\r
4234283c
LG
374 ' All rights reserved.<BR>'\r
375 self.assertEqual(Copyright, ExpectedCopyright)\r
f7496d71 376\r
4234283c
LG
377 ExpectedLicense = \\r
378 'License'\r
379 self.assertEqual(License, ExpectedLicense)\r
f7496d71 380\r
4234283c
LG
381 #\r
382 # Error case1: No copyright found\r
383 #\r
384 def testErrorCase1(self):\r
385 TestCommentLines = \\r
386 '''\r
387 ## @file\r
388 # Abstract\r
389 #\r
390 # Description\r
391 #\r
392 # License\r
393 #\r
394 ##'''\r
f7496d71 395\r
4234283c
LG
396 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
397 LineNum = 0\r
398 TestCommentLinesList = []\r
399 for Comment in CommentList:\r
400 LineNum += 1\r
401 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71
LG
402\r
403 self.assertRaises(Logger.FatalError,\r
404 ParseHeaderCommentSection,\r
4234283c 405 TestCommentLinesList,\r
f7496d71 406 "PhonyFile")\r
4234283c
LG
407\r
408 #\r
409 # Error case2: non-empty non-comment lines passed in\r
410 #\r
411 def testErrorCase2(self):\r
412 TestCommentLines = \\r
413 '''\r
414 ## @file\r
415 # Abstract\r
416 #\r
417 this is invalid line\r
418 # Description\r
419 #\r
f7496d71 420 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c
LG
421 # License\r
422 #\r
423 ##'''\r
f7496d71 424\r
4234283c
LG
425 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
426 LineNum = 0\r
427 TestCommentLinesList = []\r
428 for Comment in CommentList:\r
429 LineNum += 1\r
430 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71
LG
431\r
432 self.assertRaises(Logger.FatalError,\r
433 ParseHeaderCommentSection,\r
4234283c 434 TestCommentLinesList,\r
f7496d71 435 "PhonyFile")\r
4234283c
LG
436\r
437#\r
438# Test ParseGenericComment\r
439#\r
440class ParseGenericCommentTest(unittest.TestCase):\r
441 def setUp(self):\r
442 pass\r
443\r
444 def tearDown(self):\r
445 pass\r
f7496d71 446\r
4234283c
LG
447 #\r
448 # Normal case1: one line of comment\r
449 #\r
450 def testNormalCase1(self):\r
451 TestCommentLines = \\r
452 '''# hello world'''\r
f7496d71 453\r
4234283c
LG
454 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
455 LineNum = 0\r
456 TestCommentLinesList = []\r
457 for Comment in CommentList:\r
458 LineNum += 1\r
459 TestCommentLinesList.append((Comment, LineNum))\r
460\r
461 HelptxtObj = ParseGenericComment(TestCommentLinesList, 'testNormalCase1')\r
462 self.failIf(not HelptxtObj)\r
463 self.assertEqual(HelptxtObj.GetString(), 'hello world')\r
421ccda3 464 self.assertEqual(HelptxtObj.GetLang(), TAB_LANGUAGE_EN_US)\r
4234283c
LG
465\r
466 #\r
467 # Normal case2: multiple lines of comment\r
468 #\r
469 def testNormalCase2(self):\r
470 TestCommentLines = \\r
471 '''## hello world\r
472 # second line'''\r
f7496d71 473\r
4234283c
LG
474 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
475 LineNum = 0\r
476 TestCommentLinesList = []\r
477 for Comment in CommentList:\r
478 LineNum += 1\r
479 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 480\r
4234283c
LG
481 HelptxtObj = ParseGenericComment(TestCommentLinesList, 'testNormalCase2')\r
482 self.failIf(not HelptxtObj)\r
f7496d71 483 self.assertEqual(HelptxtObj.GetString(),\r
4234283c 484 'hello world\n' + 'second line')\r
421ccda3 485 self.assertEqual(HelptxtObj.GetLang(), TAB_LANGUAGE_EN_US)\r
4234283c
LG
486\r
487 #\r
488 # Normal case3: multiple lines of comment, non comment lines will be skipped\r
489 #\r
490 def testNormalCase3(self):\r
491 TestCommentLines = \\r
492 '''## hello world\r
493 This is not comment line'''\r
f7496d71 494\r
4234283c
LG
495 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
496 LineNum = 0\r
497 TestCommentLinesList = []\r
498 for Comment in CommentList:\r
499 LineNum += 1\r
500 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 501\r
4234283c
LG
502 HelptxtObj = ParseGenericComment(TestCommentLinesList, 'testNormalCase3')\r
503 self.failIf(not HelptxtObj)\r
f7496d71 504 self.assertEqual(HelptxtObj.GetString(),\r
4234283c 505 'hello world\n\n')\r
421ccda3 506 self.assertEqual(HelptxtObj.GetLang(), TAB_LANGUAGE_EN_US)\r
4234283c
LG
507\r
508#\r
509# Test ParseDecPcdGenericComment\r
510#\r
511class ParseDecPcdGenericCommentTest(unittest.TestCase):\r
512 def setUp(self):\r
513 pass\r
514\r
515 def tearDown(self):\r
516 pass\r
f7496d71 517\r
4234283c
LG
518 #\r
519 # Normal case1: comments with no special comment\r
520 #\r
521 def testNormalCase1(self):\r
522 TestCommentLines = \\r
523 '''## hello world\r
524 # second line'''\r
f7496d71 525\r
4234283c
LG
526 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
527 LineNum = 0\r
528 TestCommentLinesList = []\r
529 for Comment in CommentList:\r
530 LineNum += 1\r
531 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 532\r
4234283c
LG
533 (HelpTxt, PcdErr) = \\r
534 ParseDecPcdGenericComment(TestCommentLinesList, 'testNormalCase1')\r
535 self.failIf(not HelpTxt)\r
536 self.failIf(PcdErr)\r
f7496d71 537 self.assertEqual(HelpTxt,\r
4234283c 538 'hello world\n' + 'second line')\r
f7496d71
LG
539\r
540\r
4234283c
LG
541 #\r
542 # Normal case2: comments with valid list\r
543 #\r
544 def testNormalCase2(self):\r
545 TestCommentLines = \\r
546 '''## hello world\r
547 # second line\r
548 # @ValidList 1, 2, 3\r
549 # other line'''\r
f7496d71 550\r
4234283c
LG
551 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
552 LineNum = 0\r
553 TestCommentLinesList = []\r
554 for Comment in CommentList:\r
555 LineNum += 1\r
556 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 557\r
4234283c
LG
558 (HelpTxt, PcdErr) = \\r
559 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
560 self.failIf(not HelpTxt)\r
561 self.failIf(not PcdErr)\r
f7496d71 562 self.assertEqual(HelpTxt,\r
4234283c
LG
563 'hello world\n' + 'second line\n' + 'other line')\r
564 ExpectedList = GetSplitValueList('1 2 3', TAB_SPACE_SPLIT)\r
565 ActualList = [item for item in \\r
566 GetSplitValueList(PcdErr.GetValidValue(), TAB_SPACE_SPLIT) if item]\r
567 self.assertEqual(ExpectedList, ActualList)\r
568 self.failIf(PcdErr.GetExpression())\r
569 self.failIf(PcdErr.GetValidValueRange())\r
570\r
571 #\r
572 # Normal case3: comments with valid range\r
573 #\r
574 def testNormalCase3(self):\r
575 TestCommentLines = \\r
576 '''## hello world\r
577 # second line\r
578 # @ValidRange LT 1 AND GT 2\r
579 # other line'''\r
f7496d71 580\r
4234283c
LG
581 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
582 LineNum = 0\r
583 TestCommentLinesList = []\r
584 for Comment in CommentList:\r
585 LineNum += 1\r
586 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 587\r
4234283c
LG
588 (HelpTxt, PcdErr) = \\r
589 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
590 self.failIf(not HelpTxt)\r
591 self.failIf(not PcdErr)\r
f7496d71 592 self.assertEqual(HelpTxt,\r
4234283c
LG
593 'hello world\n' + 'second line\n' + 'other line')\r
594 self.assertEqual(PcdErr.GetValidValueRange().strip(), 'LT 1 AND GT 2')\r
595 self.failIf(PcdErr.GetExpression())\r
596 self.failIf(PcdErr.GetValidValue())\r
597\r
598 #\r
599 # Normal case4: comments with valid expression\r
600 #\r
601 def testNormalCase4(self):\r
602 TestCommentLines = \\r
603 '''## hello world\r
604 # second line\r
605 # @Expression LT 1 AND GT 2\r
606 # other line'''\r
f7496d71 607\r
4234283c
LG
608 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
609 LineNum = 0\r
610 TestCommentLinesList = []\r
611 for Comment in CommentList:\r
612 LineNum += 1\r
613 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 614\r
4234283c
LG
615 (HelpTxt, PcdErr) = \\r
616 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
617 self.failIf(not HelpTxt)\r
618 self.failIf(not PcdErr)\r
f7496d71 619 self.assertEqual(HelpTxt,\r
4234283c
LG
620 'hello world\n' + 'second line\n' + 'other line')\r
621 self.assertEqual(PcdErr.GetExpression().strip(), 'LT 1 AND GT 2')\r
622 self.failIf(PcdErr.GetValidValueRange())\r
623 self.failIf(PcdErr.GetValidValue())\r
624\r
625 #\r
626 # Normal case5: comments with valid expression and no generic comment\r
627 #\r
628 def testNormalCase5(self):\r
629 TestCommentLines = \\r
630 '''# @Expression LT 1 AND GT 2'''\r
f7496d71 631\r
4234283c
LG
632 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
633 LineNum = 0\r
634 TestCommentLinesList = []\r
635 for Comment in CommentList:\r
636 LineNum += 1\r
637 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 638\r
4234283c
LG
639 (HelpTxt, PcdErr) = \\r
640 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
641 self.failIf(HelpTxt)\r
642 self.failIf(not PcdErr)\r
643 self.assertEqual(PcdErr.GetExpression().strip(), 'LT 1 AND GT 2')\r
644 self.failIf(PcdErr.GetValidValueRange())\r
645 self.failIf(PcdErr.GetValidValue())\r
f7496d71 646\r
4234283c
LG
647 #\r
648 # Normal case6: comments with only generic help text\r
649 #\r
650 def testNormalCase6(self):\r
651 TestCommentLines = \\r
652 '''#'''\r
f7496d71 653\r
4234283c
LG
654 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
655 LineNum = 0\r
656 TestCommentLinesList = []\r
657 for Comment in CommentList:\r
658 LineNum += 1\r
659 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 660\r
4234283c
LG
661 (HelpTxt, PcdErr) = \\r
662 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
663 self.assertEqual(HelpTxt, '\n')\r
664 self.failIf(PcdErr)\r
665\r
f7496d71
LG
666\r
667\r
4234283c
LG
668 #\r
669 # Error case1: comments with both expression and valid list, use later\r
670 # ignore the former and with a warning message\r
671 #\r
672 def testErrorCase1(self):\r
673 TestCommentLines = \\r
674 '''## hello world\r
675 # second line\r
f7496d71 676 # @ValidList 1, 2, 3\r
4234283c
LG
677 # @Expression LT 1 AND GT 2\r
678 # other line'''\r
f7496d71 679\r
4234283c
LG
680 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
681 LineNum = 0\r
682 TestCommentLinesList = []\r
683 for Comment in CommentList:\r
684 LineNum += 1\r
685 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 686\r
4234283c
LG
687 try:\r
688 ParseDecPcdGenericComment(TestCommentLinesList, 'UnitTest')\r
689 except Logger.FatalError:\r
690 pass\r
691\r
692#\r
693# Test ParseDecPcdTailComment\r
694#\r
695class ParseDecPcdTailCommentTest(unittest.TestCase):\r
696 def setUp(self):\r
697 pass\r
698\r
699 def tearDown(self):\r
700 pass\r
f7496d71 701\r
4234283c
LG
702 #\r
703 # Normal case1: comments with no SupModeList\r
704 #\r
705 def testNormalCase1(self):\r
706 TestCommentLines = \\r
707 '''## #hello world'''\r
f7496d71 708\r
4234283c
LG
709 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
710 LineNum = 0\r
711 TestCommentLinesList = []\r
712 for Comment in CommentList:\r
713 LineNum += 1\r
714 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 715\r
4234283c
LG
716 (SupModeList, HelpStr) = \\r
717 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
718 self.failIf(not HelpStr)\r
719 self.failIf(SupModeList)\r
f7496d71 720 self.assertEqual(HelpStr,\r
4234283c
LG
721 'hello world')\r
722\r
723 #\r
724 # Normal case2: comments with one SupMode\r
725 #\r
726 def testNormalCase2(self):\r
727 TestCommentLines = \\r
728 '''## BASE #hello world'''\r
f7496d71 729\r
4234283c
LG
730 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
731 LineNum = 0\r
732 TestCommentLinesList = []\r
733 for Comment in CommentList:\r
734 LineNum += 1\r
735 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 736\r
4234283c
LG
737 (SupModeList, HelpStr) = \\r
738 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
739 self.failIf(not HelpStr)\r
740 self.failIf(not SupModeList)\r
f7496d71 741 self.assertEqual(HelpStr,\r
4234283c 742 'hello world')\r
f7496d71 743 self.assertEqual(SupModeList,\r
4234283c 744 ['BASE'])\r
f7496d71 745\r
4234283c
LG
746 #\r
747 # Normal case3: comments with more than one SupMode\r
748 #\r
749 def testNormalCase3(self):\r
750 TestCommentLines = \\r
751 '''## BASE UEFI_APPLICATION #hello world'''\r
f7496d71 752\r
4234283c
LG
753 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
754 LineNum = 0\r
755 TestCommentLinesList = []\r
756 for Comment in CommentList:\r
757 LineNum += 1\r
758 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 759\r
4234283c
LG
760 (SupModeList, HelpStr) = \\r
761 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
762 self.failIf(not HelpStr)\r
763 self.failIf(not SupModeList)\r
f7496d71 764 self.assertEqual(HelpStr,\r
4234283c 765 'hello world')\r
f7496d71 766 self.assertEqual(SupModeList,\r
4234283c
LG
767 ['BASE', 'UEFI_APPLICATION'])\r
768\r
769 #\r
770 # Normal case4: comments with more than one SupMode, no help text\r
771 #\r
772 def testNormalCase4(self):\r
773 TestCommentLines = \\r
774 '''## BASE UEFI_APPLICATION'''\r
f7496d71 775\r
4234283c
LG
776 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
777 LineNum = 0\r
778 TestCommentLinesList = []\r
779 for Comment in CommentList:\r
780 LineNum += 1\r
781 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 782\r
4234283c
LG
783 (SupModeList, HelpStr) = \\r
784 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
785 self.failIf(HelpStr)\r
786 self.failIf(not SupModeList)\r
f7496d71 787 self.assertEqual(SupModeList,\r
4234283c
LG
788 ['BASE', 'UEFI_APPLICATION'])\r
789\r
790 #\r
f7496d71 791 # Normal case5: general comments with no supModList, extract from real case\r
4234283c
LG
792 #\r
793 def testNormalCase5(self):\r
794 TestCommentLines = \\r
795 ''' # 1 = 128MB, 2 = 256MB, 3 = MAX'''\r
f7496d71 796\r
4234283c
LG
797 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
798 LineNum = 0\r
799 TestCommentLinesList = []\r
800 for Comment in CommentList:\r
801 LineNum += 1\r
802 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 803\r
4234283c
LG
804 (SupModeList, HelpStr) = \\r
805 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
806 self.failIf(not HelpStr)\r
f7496d71 807 self.assertEqual(HelpStr,\r
4234283c
LG
808 '1 = 128MB, 2 = 256MB, 3 = MAX')\r
809 self.failIf(SupModeList)\r
f7496d71 810\r
4234283c
LG
811\r
812 #\r
f7496d71 813 # Error case2: comments with supModList contains valid and invalid\r
4234283c
LG
814 # module type\r
815 #\r
816 def testErrorCase2(self):\r
817 TestCommentLines = \\r
818 '''## BASE INVALID_MODULE_TYPE #hello world'''\r
f7496d71 819\r
4234283c
LG
820 CommentList = GetSplitValueList(TestCommentLines, "\n")\r
821 LineNum = 0\r
822 TestCommentLinesList = []\r
823 for Comment in CommentList:\r
824 LineNum += 1\r
825 TestCommentLinesList.append((Comment, LineNum))\r
f7496d71 826\r
4234283c
LG
827 try:\r
828 ParseDecPcdTailComment(TestCommentLinesList, 'UnitTest')\r
829 except Logger.FatalError:\r
830 pass\r
831\r
832\r
833#\r
834# Test _IsCopyrightLine\r
835#\r
836class _IsCopyrightLineTest(unittest.TestCase):\r
837 def setUp(self):\r
838 pass\r
839\r
840 def tearDown(self):\r
841 pass\r
842\r
843 #\r
844 # Normal case\r
845 #\r
846 def testCase1(self):\r
847 Line = 'this is a copyright ( line'\r
848 Result = _IsCopyrightLine(Line)\r
849 self.failIf(not Result)\r
850\r
851 #\r
852 # Normal case\r
853 #\r
854 def testCase2(self):\r
855 Line = 'this is a Copyright ( line'\r
856 Result = _IsCopyrightLine(Line)\r
857 self.failIf(not Result)\r
858\r
859 #\r
860 # Normal case\r
861 #\r
862 def testCase3(self):\r
863 Line = 'this is not aCopyright ( line'\r
864 Result = _IsCopyrightLine(Line)\r
865 self.failIf(Result)\r
f7496d71 866\r
4234283c
LG
867 #\r
868 # Normal case\r
869 #\r
870 def testCase4(self):\r
871 Line = 'this is Copyright( line'\r
872 Result = _IsCopyrightLine(Line)\r
873 self.failIf(not Result)\r
874\r
875 #\r
876 # Normal case\r
877 #\r
878 def testCase5(self):\r
879 Line = 'this is Copyright (line'\r
880 Result = _IsCopyrightLine(Line)\r
881 self.failIf(not Result)\r
882\r
883 #\r
884 # Normal case\r
885 #\r
886 def testCase6(self):\r
887 Line = 'this is not Copyright line'\r
888 Result = _IsCopyrightLine(Line)\r
889 self.failIf(Result)\r
890\r
891 #\r
892 # Normal case\r
893 #\r
894 def testCase7(self):\r
895 Line = 'Copyright (c) line'\r
896 Result = _IsCopyrightLine(Line)\r
897 self.failIf(not Result)\r
898\r
899 #\r
900 # Normal case\r
901 #\r
902 def testCase8(self):\r
903 Line = ' Copyright (c) line'\r
904 Result = _IsCopyrightLine(Line)\r
905 self.failIf(not Result)\r
906\r
907 #\r
908 # Normal case\r
909 #\r
910 def testCase9(self):\r
911 Line = 'not a Copyright '\r
912 Result = _IsCopyrightLine(Line)\r
913 self.failIf(Result)\r
f7496d71 914\r
4234283c
LG
915if __name__ == '__main__':\r
916 Logger.Initialize()\r
f7496d71 917 unittest.main()\r