]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Tests/CheckUnicodeSourceFiles.py
BaseTools/Tests: Add unit test for AutoGen.UniClassObject
[mirror_edk2.git] / BaseTools / Tests / CheckUnicodeSourceFiles.py
1 ## @file
2 # Unit tests for AutoGen.UniClassObject
3 #
4 # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this 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 #
14
15 ##
16 # Import Modules
17 #
18 import os
19 import unittest
20
21 import codecs
22
23 import TestTools
24
25 from Common.Misc import PathClass
26 import AutoGen.UniClassObject as BtUni
27
28 from Common import EdkLogger
29 EdkLogger.InitializeForUnitTest()
30
31 class Tests(TestTools.BaseToolsTest):
32
33 SampleData = u'''
34 #langdef en-US "English"
35 #string STR_A #language en-US "STR_A for en-US"
36 '''
37
38 def EncodeToFile(self, encoding, string=None):
39 if string is None:
40 string = self.SampleData
41 data = codecs.encode(string, encoding)
42 path = 'input.uni'
43 self.WriteTmpFile(path, data)
44 return PathClass(self.GetTmpFilePath(path))
45
46 def ErrorFailure(self, error, encoding, shouldPass):
47 msg = error + ' should '
48 if shouldPass:
49 msg += 'not '
50 msg += 'be generated for '
51 msg += '%s data in a .uni file' % encoding
52 self.fail(msg)
53
54 def UnicodeErrorFailure(self, encoding, shouldPass):
55 self.ErrorFailure('UnicodeError', encoding, shouldPass)
56
57 def EdkErrorFailure(self, encoding, shouldPass):
58 self.ErrorFailure('EdkLogger.FatalError', encoding, shouldPass)
59
60 def CheckFile(self, encoding, shouldPass, string=None):
61 path = self.EncodeToFile(encoding, string)
62 try:
63 BtUni.UniFileClassObject([path])
64 if shouldPass:
65 return
66 except UnicodeError:
67 if not shouldPass:
68 return
69 else:
70 self.UnicodeErrorFailure(encoding, shouldPass)
71 except EdkLogger.FatalError:
72 if not shouldPass:
73 return
74 else:
75 self.EdkErrorFailure(encoding, shouldPass)
76 except Exception:
77 pass
78
79 self.EdkErrorFailure(encoding, shouldPass)
80
81 def testUtf16InUniFile(self):
82 self.CheckFile('utf_16', shouldPass=True)
83
84 TheTestSuite = TestTools.MakeTheTestSuite(locals())
85
86 if __name__ == '__main__':
87 allTests = TheTestSuite()
88 unittest.TextTestRunner().run(allTests)