1## @file
2# This file contain unit test for CommentParsing
3#
4# Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
5#
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
10#
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.
13
14import os
15import unittest
16
17import Logger.Log as Logger
18from GenMetaFile.GenInfFile import GenGuidSections
19from GenMetaFile.GenInfFile import GenProtocolPPiSections
20from GenMetaFile.GenInfFile import GenPcdSections
21from GenMetaFile.GenInfFile import GenSpecialSections
22from Library.CommentGenerating import GenGenericCommentF
23from Library.CommentGenerating import _GetHelpStr
24from Object.POM.CommonObject import TextObject
25from Object.POM.CommonObject import GuidObject
26from Object.POM.CommonObject import ProtocolObject
27from Object.POM.CommonObject import PpiObject
28from Object.POM.CommonObject import PcdObject
29from Object.POM.ModuleObject import HobObject
30
31from Library.String import GetSplitValueList
32from Library.DataType import TAB_SPACE_SPLIT
33from Library.DataType import TAB_LANGUAGE_EN_US
34from Library.DataType import TAB_LANGUAGE_ENG
35from Library.DataType import ITEM_UNDEFINED
36from Library.DataType import TAB_INF_FEATURE_PCD
37from Library import GlobalData
38from Library.Misc import CreateDirectory
39
40#
41# Test _GetHelpStr
42#
43class _GetHelpStrTest(unittest.TestCase):
44    def setUp(self):
45        pass
46
47    def tearDown(self):
48        pass
49
50    #
51    # Normal case1: have one help text object with Lang = 'en-US'
52    #
53    def testNormalCase1(self):
54        HelpStr = 'Hello world'
55        HelpTextObj = TextObject()
56        HelpTextObj.SetLang(TAB_LANGUAGE_EN_US)
57        HelpTextObj.SetString(HelpStr)
58
59        HelpTextList = [HelpTextObj]
60        Result = _GetHelpStr(HelpTextList)
61        self.assertEqual(Result, HelpStr)
62
63    #
64    # Normal case2: have two help text object with Lang = 'en-US' and other
65    #
66    def testNormalCase2(self):
67        HelpStr = 'Hello world'
68        HelpTextObj = TextObject()
69        HelpTextObj.SetLang(TAB_LANGUAGE_ENG)
70        HelpTextObj.SetString(HelpStr)
71
72        HelpTextList = [HelpTextObj]
73
74        ExpectedStr = 'Hello world1'
75        HelpTextObj = TextObject()
76        HelpTextObj.SetLang(TAB_LANGUAGE_EN_US)
77        HelpTextObj.SetString(ExpectedStr)
78
79        HelpTextList.append(HelpTextObj)
80
81        Result = _GetHelpStr(HelpTextList)
82        self.assertEqual(Result, ExpectedStr)
83
84    #
85    # Normal case3: have two help text object with Lang = '' and 'eng'
86    #
87    def testNormalCase3(self):
88        HelpStr = 'Hello world'
89        HelpTextObj = TextObject()
90        HelpTextObj.SetLang('')
91        HelpTextObj.SetString(HelpStr)
92
93        HelpTextList = [HelpTextObj]
94
95        ExpectedStr = 'Hello world1'
96        HelpTextObj = TextObject()
97        HelpTextObj.SetLang(TAB_LANGUAGE_ENG)
98        HelpTextObj.SetString(ExpectedStr)
99
100        HelpTextList.append(HelpTextObj)
101
102        Result = _GetHelpStr(HelpTextList)
103        self.assertEqual(Result, ExpectedStr)
104
105    #
106    # Normal case4: have two help text object with Lang = '' and ''
107    #
108    def testNormalCase4(self):
109
110        ExpectedStr = 'Hello world1'
111        HelpTextObj = TextObject()
112        HelpTextObj.SetLang(TAB_LANGUAGE_ENG)
113        HelpTextObj.SetString(ExpectedStr)
114        HelpTextList = [HelpTextObj]
115
116        HelpStr = 'Hello world'
117        HelpTextObj = TextObject()
118        HelpTextObj.SetLang('')
119        HelpTextObj.SetString(HelpStr)
120        HelpTextList.append(HelpTextObj)
121
122        Result = _GetHelpStr(HelpTextList)
123        self.assertEqual(Result, ExpectedStr)
124
125    #
126    # Normal case: have three help text object with Lang = '','en', 'en-US'
127    #
128    def testNormalCase5(self):
129
130        ExpectedStr = 'Hello world1'
131        HelpTextObj = TextObject()
132        HelpTextObj.SetLang(TAB_LANGUAGE_EN_US)
133        HelpTextObj.SetString(ExpectedStr)
134        HelpTextList = [HelpTextObj]
135
136        HelpStr = 'Hello unknown world'
137        HelpTextObj = TextObject()
138        HelpTextObj.SetLang('')
139        HelpTextObj.SetString(HelpStr)
140        HelpTextList.append(HelpTextObj)
141
142        HelpStr = 'Hello mysterious world'
143        HelpTextObj = TextObject()
144        HelpTextObj.SetLang('')
145        HelpTextObj.SetString(HelpStr)
146        HelpTextList.append(HelpTextObj)
147
148        Result = _GetHelpStr(HelpTextList)
149        self.assertEqual(Result, ExpectedStr)
150
151        HelpTextList.sort()
152        self.assertEqual(Result, ExpectedStr)
153
154        HelpTextList.sort(reverse=True)
155        self.assertEqual(Result, ExpectedStr)
156
157
158#
159# Test GenGuidSections
160#
161class GenGuidSectionsTest(unittest.TestCase):
162    def setUp(self):
163        pass
164
165    def tearDown(self):
166        pass
167
168    #
169    # This is the API to generate Guid Object to help UnitTest
170    #
171    def GuidFactory(self, CName, FFE, Usage, GuidType, VariableName, HelpStr):
172        Guid = GuidObject()
173        Guid.SetCName(CName)
174        Guid.SetFeatureFlag(FFE)
175        Guid.SetGuidTypeList([GuidType])
176        Guid.SetUsage(Usage)
177        Guid.SetVariableName(VariableName)
178
179        HelpTextObj = TextObject()
180        HelpTextObj.SetLang('')
181        HelpTextObj.SetString(HelpStr)
182        Guid.SetHelpTextList([HelpTextObj])
183
184        return Guid
185
186    #
187    # Normal case: have two GuidObject
188    #
189    def testNormalCase1(self):
190        GuidList = []
191
192        CName = 'Guid1'
193        FFE = 'FFE1'
194        Usage = 'PRODUCES'
195        GuidType = 'Event'
196        VariableName = ''
197        HelpStr = 'Usage comment line 1'
198        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
199                                 VariableName, HelpStr)
200        GuidList.append(Guid1)
201
202        CName = 'Guid1'
203        FFE = 'FFE1'
204        Usage = 'CONSUMES'
205        GuidType = 'Variable'
206        VariableName = ''
207        HelpStr = 'Usage comment line 2'
208        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
209                                 VariableName, HelpStr)
210        GuidList.append(Guid1)
211
212        Result = GenGuidSections(GuidList)
213        Expected = '''[Guids]
214## PRODUCES ## Event # Usage comment line 1
215## CONSUMES ## Variable: # Usage comment line 2
216Guid1|FFE1'''
217        self.assertEqual(Result.strip(), Expected)
218
219    #
220    # Normal case: have two GuidObject
221    #
222    def testNormalCase2(self):
223        GuidList = []
224
225        CName = 'Guid1'
226        FFE = 'FFE1'
227        Usage = 'PRODUCES'
228        GuidType = 'Event'
229        VariableName = ''
230        HelpStr = 'Usage comment line 1'
231        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
232                                 VariableName, HelpStr)
233        GuidList.append(Guid1)
234
235        CName = 'Guid1'
236        FFE = 'FFE1'
237        Usage = 'UNDEFINED'
238        GuidType = 'UNDEFINED'
239        VariableName = ''
240        HelpStr = 'Generic comment line 1\n Generic comment line 2'
241        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
242                                 VariableName, HelpStr)
243        GuidList.append(Guid1)
244
245        Result = GenGuidSections(GuidList)
246        Expected = '''[Guids]
247## PRODUCES ## Event # Usage comment line 1
248# Generic comment line 1
249# Generic comment line 2
250Guid1|FFE1'''
251
252        self.assertEqual(Result.strip(), Expected)
253
254    #
255    # Normal case: have two GuidObject, one help goes to generic help,
256    # the other go into usage comment
257    #
258    def testNormalCase3(self):
259        GuidList = []
260
261        CName = 'Guid1'
262        FFE = 'FFE1'
263        Usage = 'UNDEFINED'
264        GuidType = 'UNDEFINED'
265        VariableName = ''
266        HelpStr = 'Generic comment'
267        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
268                                 VariableName, HelpStr)
269        GuidList.append(Guid1)
270
271        CName = 'Guid1'
272        FFE = 'FFE1'
273        Usage = 'PRODUCES'
274        GuidType = 'Event'
275        VariableName = ''
276        HelpStr = 'Usage comment line 1'
277        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
278                                 VariableName, HelpStr)
279        GuidList.append(Guid1)
280
281        Result = GenGuidSections(GuidList)
282        Expected = '''[Guids]
283# Generic comment
284## PRODUCES ## Event # Usage comment line 1
285Guid1|FFE1'''
286
287        self.assertEqual(Result.strip(), Expected)
288
289    #
290    # Normal case: have one GuidObject, generic comment multiple lines
291    #
292    def testNormalCase5(self):
293        GuidList = []
294
295        CName = 'Guid1'
296        FFE = 'FFE1'
297        Usage = 'UNDEFINED'
298        GuidType = 'UNDEFINED'
299        VariableName = ''
300        HelpStr = 'Generic comment line1 \n generic comment line 2'
301        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
302                                 VariableName, HelpStr)
303        GuidList.append(Guid1)
304
305        Result = GenGuidSections(GuidList)
306        Expected = '''[Guids]
307# Generic comment line1
308# generic comment line 2
309Guid1|FFE1'''
310
311        self.assertEqual(Result.strip(), Expected)
312
313    #
314    # Normal case: have one GuidObject, usage comment multiple lines
315    #
316    def testNormalCase6(self):
317        GuidList = []
318
319        CName = 'Guid1'
320        FFE = 'FFE1'
321        Usage = 'PRODUCES'
322        GuidType = 'Event'
323        VariableName = ''
324        HelpStr = 'Usage comment line 1\n Usage comment line 2'
325        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
326                                 VariableName, HelpStr)
327        GuidList.append(Guid1)
328
329        Result = GenGuidSections(GuidList)
330        Expected = '''[Guids]
331Guid1|FFE1 ## PRODUCES ## Event # Usage comment line 1  Usage comment line 2
332'''
333        self.assertEqual(Result.strip(), Expected.strip())
334
335    #
336    # Normal case: have one GuidObject, usage comment one line
337    #
338    def testNormalCase7(self):
339        GuidList = []
340
341        CName = 'Guid1'
342        FFE = 'FFE1'
343        Usage = 'UNDEFINED'
344        GuidType = 'UNDEFINED'
345        VariableName = ''
346        HelpStr = 'Usage comment line 1'
347        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
348                                 VariableName, HelpStr)
349        GuidList.append(Guid1)
350
351        Result = GenGuidSections(GuidList)
352        Expected = '''[Guids]
353Guid1|FFE1 # Usage comment line 1
354'''
355        self.assertEqual(Result.strip(), Expected.strip())
356
357    #
358    # Normal case: have two GuidObject
359    #
360    def testNormalCase8(self):
361        GuidList = []
362
363        CName = 'Guid1'
364        FFE = 'FFE1'
365        Usage = 'PRODUCES'
366        GuidType = 'Event'
367        VariableName = ''
368        HelpStr = 'Usage comment line 1\n Usage comment line 2'
369        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
370                                 VariableName, HelpStr)
371        GuidList.append(Guid1)
372
373        CName = 'Guid1'
374        FFE = 'FFE1'
375        Usage = 'PRODUCES'
376        GuidType = 'Event'
377        VariableName = ''
378        HelpStr = 'Usage comment line 3'
379        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
380                                 VariableName, HelpStr)
381        GuidList.append(Guid1)
382
383        Result = GenGuidSections(GuidList)
384        Expected = '''[Guids]
385## PRODUCES ## Event # Usage comment line 1  Usage comment line 2
386## PRODUCES ## Event # Usage comment line 3
387Guid1|FFE1
388'''
389        self.assertEqual(Result.strip(), Expected.strip())
390
391    #
392    # Normal case: have no GuidObject
393    #
394    def testNormalCase9(self):
395        GuidList = []
396
397        Result = GenGuidSections(GuidList)
398        Expected = ''
399        self.assertEqual(Result.strip(), Expected.strip())
400
401    #
402    # Normal case: have one GuidObject with no comment generated
403    #
404    def testNormalCase10(self):
405        GuidList = []
406
407        CName = 'Guid1'
408        FFE = 'FFE1'
409        Usage = 'UNDEFINED'
410        GuidType = 'UNDEFINED'
411        VariableName = ''
412        HelpStr = ''
413        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
414                                 VariableName, HelpStr)
415        GuidList.append(Guid1)
416
417        Result = GenGuidSections(GuidList)
418        Expected = '''[Guids]
419Guid1|FFE1
420'''
421        self.assertEqual(Result.strip(), Expected.strip())
422
423    #
424    # Normal case: have three GuidObject
425    #
426    def testNormalCase11(self):
427        GuidList = []
428
429        CName = 'Guid1'
430        FFE = 'FFE1'
431        Usage = 'UNDEFINED'
432        GuidType = 'UNDEFINED'
433        VariableName = ''
434        HelpStr = 'general comment line 1'
435        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
436                                 VariableName, HelpStr)
437        GuidList.append(Guid1)
438
439        CName = 'Guid1'
440        FFE = 'FFE1'
441        Usage = 'PRODUCES'
442        GuidType = 'Event'
443        VariableName = ''
444        HelpStr = 'Usage comment line 3'
445        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
446                                 VariableName, HelpStr)
447        GuidList.append(Guid1)
448
449        CName = 'Guid1'
450        FFE = 'FFE1'
451        Usage = 'UNDEFINED'
452        GuidType = 'UNDEFINED'
453        VariableName = ''
454        HelpStr = 'general comment line 2'
455        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
456                                 VariableName, HelpStr)
457        GuidList.append(Guid1)
458
459        Result = GenGuidSections(GuidList)
460        Expected = '''[Guids]
461# general comment line 1
462## PRODUCES ## Event # Usage comment line 3
463# general comment line 2
464Guid1|FFE1
465'''
466        self.assertEqual(Result.strip(), Expected.strip())
467
468    #
469    # Normal case: have three GuidObject, with Usage/Type and no help
470    #
471    def testNormalCase12(self):
472        GuidList = []
473
474        CName = 'Guid1'
475        FFE = 'FFE1'
476        Usage = 'PRODUCES'
477        GuidType = 'GUID'
478        VariableName = ''
479        HelpStr = ''
480        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
481                                 VariableName, HelpStr)
482        GuidList.append(Guid1)
483
484        CName = 'Guid1'
485        FFE = 'FFE1'
486        Usage = 'PRODUCES'
487        GuidType = 'Event'
488        VariableName = ''
489        HelpStr = ''
490        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
491                                 VariableName, HelpStr)
492        GuidList.append(Guid1)
493
494        CName = 'Guid1'
495        FFE = 'FFE1'
496        Usage = 'CONSUMES'
497        GuidType = 'Event'
498        VariableName = ''
499        HelpStr = ''
500        Guid1 = self.GuidFactory(CName, FFE, Usage, GuidType,
501                                 VariableName, HelpStr)
502        GuidList.append(Guid1)
503
504        Result = GenGuidSections(GuidList)
505        Expected = '''[Guids]
506## PRODUCES ## GUID
507## PRODUCES ## Event
508## CONSUMES ## Event
509Guid1|FFE1
510'''
511        self.assertEqual(Result.strip(), Expected.strip())
512
513#
514# Test GenProtocolPPiSections
515#
516class GenProtocolPPiSectionsTest(unittest.TestCase):
517    def setUp(self):
518        pass
519
520    def tearDown(self):
521        pass
522
523    #
524    # This is the API to generate Protocol/Ppi Object to help UnitTest
525    #
526    def ObjectFactory(self, CName, FFE, Usage, Notify, HelpStr, IsProtocol):
527        if IsProtocol:
528            Object = ProtocolObject()
529        else:
530            Object = PpiObject()
531
532        Object.SetCName(CName)
533        Object.SetFeatureFlag(FFE)
534        Object.SetUsage(Usage)
535        Object.SetNotify(Notify)
536
537        HelpTextObj = TextObject()
538        HelpTextObj.SetLang('')
539        HelpTextObj.SetString(HelpStr)
540        Object.SetHelpTextList([HelpTextObj])
541
542        return Object
543
544    #    Usage        Notify    Help    INF Comment
545    #1   UNDEFINED    true    Present    ## UNDEFINED ## NOTIFY # Help
546    #2   UNDEFINED    true    Not Present    ## UNDEFINED ## NOTIFY
547    #3   UNDEFINED    false    Present    ## UNDEFINED # Help
548    #4   UNDEFINED    false     Not Present    ## UNDEFINED
549    #5   UNDEFINED    Not Present    Present    # Help
550    #6   UNDEFINED    Not Present    Not Present    <empty>
551    #7   Other        true    Present    ## Other ## NOTIFY # Help
552    #8   Other        true    Not Present    ## Other ## NOTIFY
553    #9   Other        false    Present    ## Other # Help
554    #A   Other        false     Not Present    ## Other
555    #B   Other        Not Present    Present    ## Other # Help
556    #C   Other        Not Present    Not Present    ## Other
557
558    def testNormalCase1(self):
559        ObjectList = []
560
561        CName = 'Guid1'
562        FFE = 'FFE1'
563
564        Usage = 'UNDEFINED'
565        Notify = True
566        HelpStr = 'Help'
567        IsProtocol = True
568        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
569                                 HelpStr, IsProtocol)
570        ObjectList.append(Object)
571
572
573        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
574        Expected = '''[Protocols]
575Guid1|FFE1 ## UNDEFINED ## NOTIFY # Help'''
576        self.assertEqual(Result.strip(), Expected)
577
578        IsProtocol = False
579        ObjectList = []
580        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
581                                 HelpStr, IsProtocol)
582        ObjectList.append(Object)
583
584
585        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
586        Expected = '''[Ppis]
587Guid1|FFE1 ## UNDEFINED ## NOTIFY # Help'''
588        self.assertEqual(Result.strip(), Expected)
589
590    def testNormalCase2(self):
591        ObjectList = []
592
593        CName = 'Guid1'
594        FFE = 'FFE1'
595
596        Usage = 'UNDEFINED'
597        Notify = True
598        HelpStr = ''
599        IsProtocol = True
600        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
601                                 HelpStr, IsProtocol)
602        ObjectList.append(Object)
603
604
605        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
606        Expected = '''[Protocols]
607Guid1|FFE1 ## UNDEFINED ## NOTIFY'''
608        self.assertEqual(Result.strip(), Expected)
609
610    def testNormalCase3(self):
611        ObjectList = []
612
613        CName = 'Guid1'
614        FFE = 'FFE1'
615
616        Usage = 'UNDEFINED'
617        Notify = False
618        HelpStr = 'Help'
619        IsProtocol = True
620        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
621                                 HelpStr, IsProtocol)
622        ObjectList.append(Object)
623
624
625        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
626        Expected = '''[Protocols]
627Guid1|FFE1 ## UNDEFINED # Help'''
628        self.assertEqual(Result.strip(), Expected)
629
630    def testNormalCase4(self):
631        ObjectList = []
632
633        CName = 'Guid1'
634        FFE = 'FFE1'
635
636        Usage = 'UNDEFINED'
637        Notify = False
638        HelpStr = ''
639        IsProtocol = True
640        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
641                                 HelpStr, IsProtocol)
642        ObjectList.append(Object)
643
644
645        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
646        Expected = '''[Protocols]
647Guid1|FFE1 ## UNDEFINED'''
648        self.assertEqual(Result.strip(), Expected)
649
650    def testNormalCase5(self):
651        ObjectList = []
652
653        CName = 'Guid1'
654        FFE = 'FFE1'
655
656        Usage = 'UNDEFINED'
657        Notify = ''
658        HelpStr = 'Help'
659        IsProtocol = True
660        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
661                                 HelpStr, IsProtocol)
662        ObjectList.append(Object)
663
664
665        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
666        Expected = '''[Protocols]
667Guid1|FFE1 # Help'''
668        self.assertEqual(Result.strip(), Expected)
669
670    def testNormalCase6(self):
671        ObjectList = []
672
673        CName = 'Guid1'
674        FFE = 'FFE1'
675
676        Usage = 'UNDEFINED'
677        Notify = ''
678        HelpStr = ''
679        IsProtocol = True
680        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
681                                 HelpStr, IsProtocol)
682        ObjectList.append(Object)
683
684
685        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
686        Expected = '''[Protocols]
687Guid1|FFE1'''
688        self.assertEqual(Result.strip(), Expected)
689
690    def testNormalCase7(self):
691        ObjectList = []
692
693        CName = 'Guid1'
694        FFE = 'FFE1'
695
696        Usage = 'PRODUCES'
697        Notify = True
698        HelpStr = 'Help'
699        IsProtocol = True
700        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
701                                 HelpStr, IsProtocol)
702        ObjectList.append(Object)
703
704
705        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
706        Expected = '''[Protocols]
707Guid1|FFE1 ## PRODUCES ## NOTIFY # Help'''
708        self.assertEqual(Result.strip(), Expected)
709
710    def testNormalCase8(self):
711        ObjectList = []
712
713        CName = 'Guid1'
714        FFE = 'FFE1'
715
716        Usage = 'PRODUCES'
717        Notify = True
718        HelpStr = ''
719        IsProtocol = True
720        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
721                                 HelpStr, IsProtocol)
722        ObjectList.append(Object)
723
724
725        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
726        Expected = '''[Protocols]
727Guid1|FFE1 ## PRODUCES ## NOTIFY'''
728        self.assertEqual(Result.strip(), Expected)
729
730    def testNormalCase9(self):
731        ObjectList = []
732
733        CName = 'Guid1'
734        FFE = 'FFE1'
735
736        Usage = 'PRODUCES'
737        Notify = False
738        HelpStr = 'Help'
739        IsProtocol = True
740        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
741                                 HelpStr, IsProtocol)
742        ObjectList.append(Object)
743
744
745        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
746        Expected = '''[Protocols]
747Guid1|FFE1 ## PRODUCES # Help'''
748        self.assertEqual(Result.strip(), Expected)
749
750    def testNormalCaseA(self):
751        ObjectList = []
752
753        CName = 'Guid1'
754        FFE = 'FFE1'
755
756        Usage = 'PRODUCES'
757        Notify = False
758        HelpStr = ''
759        IsProtocol = True
760        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
761                                 HelpStr, IsProtocol)
762        ObjectList.append(Object)
763
764
765        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
766        Expected = '''[Protocols]
767Guid1|FFE1 ## PRODUCES'''
768        self.assertEqual(Result.strip(), Expected)
769
770    def testNormalCaseB(self):
771        ObjectList = []
772
773        CName = 'Guid1'
774        FFE = 'FFE1'
775
776        Usage = 'PRODUCES'
777        Notify = ''
778        HelpStr = 'Help'
779        IsProtocol = True
780        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
781                                 HelpStr, IsProtocol)
782        ObjectList.append(Object)
783
784
785        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
786        Expected = '''[Protocols]
787Guid1|FFE1 ## PRODUCES # Help'''
788        self.assertEqual(Result.strip(), Expected)
789
790    def testNormalCaseC(self):
791        ObjectList = []
792
793        CName = 'Guid1'
794        FFE = 'FFE1'
795
796        Usage = 'PRODUCES'
797        Notify = ''
798        HelpStr = ''
799        IsProtocol = True
800        Object = self.ObjectFactory(CName, FFE, Usage, Notify,
801                                 HelpStr, IsProtocol)
802        ObjectList.append(Object)
803
804
805        Result = GenProtocolPPiSections(ObjectList, IsProtocol)
806        Expected = '''[Protocols]
807Guid1|FFE1 ## PRODUCES'''
808        self.assertEqual(Result.strip(), Expected)
809
810#
811# Test GenPcdSections
812#
813class GenPcdSectionsTest(unittest.TestCase):
814    def setUp(self):
815        pass
816
817    def tearDown(self):
818        pass
819
820    #
821    # This is the API to generate Pcd Object to help UnitTest
822    #
823    def ObjectFactory(self, ItemType, TSCName, CName, DValue, FFE, Usage, Str):
824        Object = PcdObject()
825        HelpStr = Str
826
827        Object.SetItemType(ItemType)
828        Object.SetTokenSpaceGuidCName(TSCName)
829        Object.SetCName(CName)
830        Object.SetDefaultValue(DValue)
831        Object.SetFeatureFlag(FFE)
832        Object.SetValidUsage(Usage)
833
834        HelpTextObj = TextObject()
835        HelpTextObj.SetLang('')
836        HelpTextObj.SetString(HelpStr)
837        Object.SetHelpTextList([HelpTextObj])
838
839        return Object
840
841
842    #    Usage        Help    INF Comment
843    #1   UNDEFINED    Present    # Help
844    #2   UNDEFINED    Not Present    <empty>
845    #3   Other        Present    ## Other # Help
846    #4   Other        Not Present    ## Other
847
848    def testNormalCase1(self):
849        ObjectList = []
850        ItemType = 'Pcd'
851        TSCName = 'TSCName'
852        CName = 'CName'
853        DValue = 'DValue'
854        FFE = 'FFE'
855
856        Usage = 'UNDEFINED'
857        Str = 'Help'
858
859        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
860                                    Usage, Str)
861        ObjectList.append(Object)
862
863        Result = GenPcdSections(ObjectList)
864        Expected = \
865            '[Pcd]\n' + \
866            'TSCName.CName|DValue|FFE # Help'
867        self.assertEqual(Result.strip(), Expected)
868
869    def testNormalCase2(self):
870        ObjectList = []
871        ItemType = 'Pcd'
872        TSCName = 'TSCName'
873        CName = 'CName'
874        DValue = 'DValue'
875        FFE = 'FFE'
876
877        Usage = 'UNDEFINED'
878        Str = ''
879
880        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
881                                    Usage, Str)
882        ObjectList.append(Object)
883
884        Result = GenPcdSections(ObjectList)
885        Expected = '[Pcd]\nTSCName.CName|DValue|FFE'
886        self.assertEqual(Result.strip(), Expected)
887
888    def testNormalCase3(self):
889        ObjectList = []
890        ItemType = 'Pcd'
891        TSCName = 'TSCName'
892        CName = 'CName'
893        DValue = 'DValue'
894        FFE = 'FFE'
895
896        Usage = 'CONSUMES'
897        Str = 'Help'
898
899        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
900                                    Usage, Str)
901        ObjectList.append(Object)
902
903        Result = GenPcdSections(ObjectList)
904        Expected = '[Pcd]\nTSCName.CName|DValue|FFE ## CONSUMES # Help'
905        self.assertEqual(Result.strip(), Expected)
906
907    def testNormalCase4(self):
908        ObjectList = []
909        ItemType = 'Pcd'
910        TSCName = 'TSCName'
911        CName = 'CName'
912        DValue = 'DValue'
913        FFE = 'FFE'
914
915        Usage = 'CONSUMES'
916        Str = ''
917
918        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
919                                    Usage, Str)
920        ObjectList.append(Object)
921
922        Result = GenPcdSections(ObjectList)
923        Expected = '[Pcd]\nTSCName.CName|DValue|FFE ## CONSUMES'
924        self.assertEqual(Result.strip(), Expected)
925
926    #
927    # multiple lines for normal usage
928    #
929    def testNormalCase5(self):
930        ObjectList = []
931        ItemType = 'Pcd'
932        TSCName = 'TSCName'
933        CName = 'CName'
934        DValue = 'DValue'
935        FFE = 'FFE'
936
937        Usage = 'CONSUMES'
938        Str = 'commment line 1\ncomment line 2'
939        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
940                                    Usage, Str)
941        ObjectList.append(Object)
942
943        Result = GenPcdSections(ObjectList)
944        Expected = '''[Pcd]
945TSCName.CName|DValue|FFE ## CONSUMES # commment line 1 comment line 2'''
946        self.assertEqual(Result.strip(), Expected)
947
948    #
949    # multiple lines for UNDEFINED usage
950    #
951    def testNormalCase6(self):
952        ObjectList = []
953        ItemType = 'Pcd'
954        TSCName = 'TSCName'
955        CName = 'CName'
956        DValue = 'DValue'
957        FFE = 'FFE'
958
959        Usage = 'UNDEFINED'
960        Str = 'commment line 1\ncomment line 2'
961        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
962                                    Usage, Str)
963        ObjectList.append(Object)
964
965        Usage = 'UNDEFINED'
966        Str = 'commment line 3'
967        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
968                                    Usage, Str)
969        ObjectList.append(Object)
970
971        Result = GenPcdSections(ObjectList)
972        Expected = '''[Pcd]
973# commment line 1
974# comment line 2
975# commment line 3
976TSCName.CName|DValue|FFE'''
977        self.assertEqual(Result.strip(), Expected)
978
979    #
980    # multiple lines for UNDEFINED and normal usage
981    #
982    def testNormalCase7(self):
983        ObjectList = []
984        ItemType = 'Pcd'
985        TSCName = 'TSCName'
986        CName = 'CName'
987        DValue = 'DValue'
988        FFE = 'FFE'
989
990        Usage = 'UNDEFINED'
991        Str = 'commment line 1\ncomment line 2'
992        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
993                                    Usage, Str)
994        ObjectList.append(Object)
995
996        Usage = 'CONSUMES'
997        Str = 'Foo'
998        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
999                                    Usage, Str)
1000        ObjectList.append(Object)
1001
1002        Usage = 'UNDEFINED'
1003        Str = 'commment line 3'
1004        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
1005                                    Usage, Str)
1006        ObjectList.append(Object)
1007
1008        Result = GenPcdSections(ObjectList)
1009        Expected = '''[Pcd]
1010# commment line 1
1011# comment line 2
1012## CONSUMES # Foo
1013# commment line 3
1014TSCName.CName|DValue|FFE'''
1015        self.assertEqual(Result.strip(), Expected)
1016
1017    #    Usage        Help    INF Comment
1018    #    CONSUMES    Present    # Help (keep <EOL> and insert '#' at beginning of each new line)
1019    #    CONSUMES    Not Present   <empty>
1020
1021    #
1022    # TAB_INF_FEATURE_PCD
1023    #
1024    def testNormalCase8(self):
1025        ObjectList = []
1026        ItemType = TAB_INF_FEATURE_PCD
1027        TSCName = 'TSCName'
1028        CName = 'CName'
1029        DValue = 'DValue'
1030        FFE = 'FFE'
1031
1032        Usage = 'CONSUMES'
1033        Str = 'commment line 1\ncomment line 2'
1034        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
1035                                    Usage, Str)
1036        ObjectList.append(Object)
1037
1038        Result = GenPcdSections(ObjectList)
1039        Expected = '''[FeaturePcd]
1040# commment line 1
1041# comment line 2
1042TSCName.CName|DValue|FFE'''
1043        self.assertEqual(Result.strip(), Expected)
1044
1045    #
1046    # TAB_INF_FEATURE_PCD
1047    #
1048    def testNormalCase9(self):
1049        ObjectList = []
1050        ItemType = TAB_INF_FEATURE_PCD
1051        TSCName = 'TSCName'
1052        CName = 'CName'
1053        DValue = 'DValue'
1054        FFE = 'FFE'
1055
1056        Usage = 'CONSUMES'
1057        Str = ''
1058        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
1059                                    Usage, Str)
1060        ObjectList.append(Object)
1061
1062        Result = GenPcdSections(ObjectList)
1063        Expected = '''[FeaturePcd]
1064TSCName.CName|DValue|FFE'''
1065        self.assertEqual(Result.strip(), Expected)
1066
1067    #
1068    # TAB_INF_FEATURE_PCD
1069    #
1070    def testNormalCase10(self):
1071        ObjectList = []
1072        ItemType = TAB_INF_FEATURE_PCD
1073        TSCName = 'TSCName'
1074        CName = 'CName'
1075        DValue = 'DValue'
1076        FFE = 'FFE'
1077
1078        Usage = 'PRODUCES'
1079        Str = 'commment line 1\ncomment line 2'
1080        Object = self.ObjectFactory(ItemType, TSCName, CName, DValue, FFE,
1081                                    Usage, Str)
1082        ObjectList.append(Object)
1083
1084        Result = GenPcdSections(ObjectList)
1085        Expected = '''
1086
1087[FeaturePcd]
1088# commment line 1
1089# comment line 2
1090TSCName.CName|DValue|FFE
1091'''
1092        self.assertEqual(Result, Expected)
1093
1094
1095#
1096# Test GenSpecialSections of Hob
1097#
1098class GenHobSectionsTest(unittest.TestCase):
1099    def setUp(self):
1100        pass
1101
1102    def tearDown(self):
1103        pass
1104
1105    #
1106    # This is the API to generate Event Object to help UnitTest
1107    #
1108    def ObjectFactory(self, SupArchList, Type, Usage, Str):
1109        Object = HobObject()
1110        HelpStr = Str
1111
1112        Object.SetHobType(Type)
1113        Object.SetUsage(Usage)
1114        Object.SetSupArchList(SupArchList)
1115
1116        HelpTextObj = TextObject()
1117        HelpTextObj.SetLang('')
1118        HelpTextObj.SetString(HelpStr)
1119        Object.SetHelpTextList([HelpTextObj])
1120
1121        return Object
1122
1123    def testNormalCase1(self):
1124        ObjectList = []
1125        SupArchList = ['X64']
1126        Type = 'Foo'
1127        Usage = 'UNDEFINED'
1128        Str = 'Help'
1129
1130        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1131        ObjectList.append(Object)
1132
1133        Result = GenSpecialSections(ObjectList, 'Hob')
1134        Expected = '''# [Hob.X64]
1135# ##
1136# # Help
1137# #
1138# Foo ## UNDEFINED
1139#
1140#
1141'''
1142        self.assertEqual(Result, Expected)
1143
1144    def testNormalCase2(self):
1145        ObjectList = []
1146        SupArchList = []
1147        Type = 'Foo'
1148        Usage = 'UNDEFINED'
1149        Str = 'Help'
1150
1151        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1152        ObjectList.append(Object)
1153
1154        Result = GenSpecialSections(ObjectList, 'Hob')
1155        Expected = '''# [Hob]
1156# ##
1157# # Help
1158# #
1159# Foo ## UNDEFINED
1160#
1161#
1162'''
1163        self.assertEqual(Result, Expected)
1164
1165    def testNormalCase3(self):
1166        ObjectList = []
1167        SupArchList = ['X64']
1168        Type = 'Foo'
1169        Usage = 'UNDEFINED'
1170        Str = '\nComment Line 1\n\n'
1171
1172        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1173        ObjectList.append(Object)
1174
1175        Result = GenSpecialSections(ObjectList, 'Hob')
1176        Expected = '''# [Hob.X64]
1177# ##
1178# # Comment Line 1
1179# #
1180# Foo ## UNDEFINED
1181#
1182#
1183'''
1184        self.assertEqual(Result, Expected)
1185
1186    def testNormalCase4(self):
1187        ObjectList = []
1188        SupArchList = ['X64']
1189        Type = 'Foo'
1190        Usage = 'UNDEFINED'
1191        Str = '\nComment Line 1\n'
1192
1193        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1194        ObjectList.append(Object)
1195
1196        Result = GenSpecialSections(ObjectList, 'Hob')
1197        Expected = '''# [Hob.X64]
1198# ##
1199# # Comment Line 1
1200# #
1201# Foo ## UNDEFINED
1202#
1203#
1204'''
1205        self.assertEqual(Result, Expected)
1206
1207    def testNormalCase5(self):
1208        ObjectList = []
1209        SupArchList = ['X64']
1210        Type = 'Foo'
1211        Usage = 'UNDEFINED'
1212        Str = 'Comment Line 1\n\n'
1213
1214        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1215        ObjectList.append(Object)
1216
1217        Result = GenSpecialSections(ObjectList, 'Hob')
1218        Expected = '''# [Hob.X64]
1219# ##
1220# # Comment Line 1
1221# #
1222# Foo ## UNDEFINED
1223#
1224#
1225'''
1226        self.assertEqual(Result, Expected)
1227
1228    def testNormalCase6(self):
1229        ObjectList = []
1230        SupArchList = ['X64']
1231        Type = 'Foo'
1232        Usage = 'UNDEFINED'
1233        Str = ''
1234
1235        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1236        ObjectList.append(Object)
1237
1238        Result = GenSpecialSections(ObjectList, 'Hob')
1239        Expected = '''# [Hob.X64]
1240# Foo ## UNDEFINED
1241#
1242#
1243'''
1244        self.assertEqual(Result, Expected)
1245
1246    def testNormalCase7(self):
1247        ObjectList = []
1248        SupArchList = ['X64']
1249        Type = 'Foo'
1250        Usage = 'UNDEFINED'
1251        Str = '\nNew Stack HoB'
1252
1253
1254        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1255        ObjectList.append(Object)
1256
1257        Result = GenSpecialSections(ObjectList, 'Hob')
1258        Expected = '''# [Hob.X64]
1259# ##
1260# # New Stack HoB
1261# #
1262# Foo ## UNDEFINED
1263#
1264#
1265'''
1266        self.assertEqual(Result, Expected)
1267
1268    def testNormalCase8(self):
1269        ObjectList = []
1270        SupArchList = ['X64']
1271        Type = 'Foo'
1272        Usage = 'UNDEFINED'
1273        Str = '\nNew Stack HoB\n\nTail Comment'
1274
1275
1276        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1277        ObjectList.append(Object)
1278
1279        Result = GenSpecialSections(ObjectList, 'Hob')
1280        Expected = '''# [Hob.X64]
1281# ##
1282# # New Stack HoB
1283# #
1284# # Tail Comment
1285# #
1286# Foo ## UNDEFINED
1287#
1288#
1289'''
1290        self.assertEqual(Result, Expected)
1291
1292    def testNormalCase9(self):
1293        ObjectList = []
1294        SupArchList = ['X64']
1295        Type = 'Foo'
1296        Usage = 'UNDEFINED'
1297        Str = '\n\n'
1298
1299
1300        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1301        ObjectList.append(Object)
1302
1303        Result = GenSpecialSections(ObjectList, 'Hob')
1304        Expected = '''# [Hob.X64]
1305# ##
1306# #
1307# #
1308# Foo ## UNDEFINED
1309#
1310#
1311'''
1312        self.assertEqual(Result, Expected)
1313
1314    def testNormalCase10(self):
1315        ObjectList = []
1316        SupArchList = ['X64']
1317        Type = 'Foo'
1318        Usage = 'UNDEFINED'
1319        Str = '\n'
1320
1321        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1322        ObjectList.append(Object)
1323
1324        Result = GenSpecialSections(ObjectList, 'Hob')
1325        Expected = '''# [Hob.X64]
1326# ##
1327# #
1328# #
1329# Foo ## UNDEFINED
1330#
1331#
1332'''
1333        self.assertEqual(Result, Expected)
1334
1335    def testNormalCase11(self):
1336        ObjectList = []
1337        SupArchList = ['X64']
1338        Type = 'Foo'
1339        Usage = 'UNDEFINED'
1340        Str = '\n\n\n'
1341
1342        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1343        ObjectList.append(Object)
1344
1345        Result = GenSpecialSections(ObjectList, 'Hob')
1346        Expected = '''# [Hob.X64]
1347# ##
1348# #
1349# #
1350# Foo ## UNDEFINED
1351#
1352#
1353'''
1354        self.assertEqual(Result, Expected)
1355
1356    def testNormalCase12(self):
1357        ObjectList = []
1358        SupArchList = ['X64']
1359        Type = 'Foo'
1360        Usage = 'UNDEFINED'
1361        Str = '\n\n\n\n'
1362
1363        Object = self.ObjectFactory(SupArchList, Type, Usage, Str)
1364        ObjectList.append(Object)
1365
1366        Result = GenSpecialSections(ObjectList, 'Hob')
1367        Expected = '''# [Hob.X64]
1368# ##
1369# #
1370# #
1371# #
1372# Foo ## UNDEFINED
1373#
1374#
1375'''
1376        self.assertEqual(Result, Expected)
1377
1378#
1379# Test GenGenericCommentF
1380#
1381class GenGenericCommentFTest(unittest.TestCase):
1382    def setUp(self):
1383        pass
1384
1385    def tearDown(self):
1386        pass
1387
1388    def testNormalCase1(self):
1389        CommentLines = 'Comment Line 1'
1390        Result = GenGenericCommentF(CommentLines)
1391        Expected = '# Comment Line 1\n'
1392        self.assertEqual(Result, Expected)
1393
1394    def testNormalCase2(self):
1395        CommentLines = '\n'
1396        Result = GenGenericCommentF(CommentLines)
1397        Expected = '#\n'
1398        self.assertEqual(Result, Expected)
1399
1400    def testNormalCase3(self):
1401        CommentLines = '\n\n\n'
1402        Result = GenGenericCommentF(CommentLines)
1403        Expected = '#\n#\n#\n'
1404        self.assertEqual(Result, Expected)
1405
1406    def testNormalCase4(self):
1407        CommentLines = 'coment line 1\n'
1408        Result = GenGenericCommentF(CommentLines)
1409        Expected = '# coment line 1\n'
1410        self.assertEqual(Result, Expected)
1411
1412    def testNormalCase5(self):
1413        CommentLines = 'coment line 1\n coment line 2\n'
1414        Result = GenGenericCommentF(CommentLines)
1415        Expected = '# coment line 1\n# coment line 2\n'
1416        self.assertEqual(Result, Expected)
1417
1418if __name__ == '__main__':
1419    Logger.Initialize()
1420    unittest.main()