]>
Commit | Line | Data |
---|---|---|
df91e0f9 JJ |
1 | ## @file\r |
2 | # Unit tests for AutoGen.UniClassObject\r | |
3 | #\r | |
4 | # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r | |
5 | #\r | |
6 | # This program and the accompanying materials\r | |
7 | # are licensed and made available under the terms and conditions of the BSD License\r | |
8 | # which accompanies this distribution. The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.php\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | #\r | |
14 | \r | |
15 | ##\r | |
16 | # Import Modules\r | |
17 | #\r | |
18 | import os\r | |
19 | import unittest\r | |
20 | \r | |
21 | import codecs\r | |
22 | \r | |
23 | import TestTools\r | |
24 | \r | |
25 | from Common.Misc import PathClass\r | |
26 | import AutoGen.UniClassObject as BtUni\r | |
27 | \r | |
28 | from Common import EdkLogger\r | |
29 | EdkLogger.InitializeForUnitTest()\r | |
30 | \r | |
31 | class Tests(TestTools.BaseToolsTest):\r | |
32 | \r | |
33 | SampleData = u'''\r | |
34 | #langdef en-US "English"\r | |
35 | #string STR_A #language en-US "STR_A for en-US"\r | |
36 | '''\r | |
37 | \r | |
38 | def EncodeToFile(self, encoding, string=None):\r | |
39 | if string is None:\r | |
40 | string = self.SampleData\r | |
41 | data = codecs.encode(string, encoding)\r | |
42 | path = 'input.uni'\r | |
43 | self.WriteTmpFile(path, data)\r | |
44 | return PathClass(self.GetTmpFilePath(path))\r | |
45 | \r | |
46 | def ErrorFailure(self, error, encoding, shouldPass):\r | |
47 | msg = error + ' should '\r | |
48 | if shouldPass:\r | |
49 | msg += 'not '\r | |
50 | msg += 'be generated for '\r | |
51 | msg += '%s data in a .uni file' % encoding\r | |
52 | self.fail(msg)\r | |
53 | \r | |
54 | def UnicodeErrorFailure(self, encoding, shouldPass):\r | |
55 | self.ErrorFailure('UnicodeError', encoding, shouldPass)\r | |
56 | \r | |
57 | def EdkErrorFailure(self, encoding, shouldPass):\r | |
58 | self.ErrorFailure('EdkLogger.FatalError', encoding, shouldPass)\r | |
59 | \r | |
60 | def CheckFile(self, encoding, shouldPass, string=None):\r | |
61 | path = self.EncodeToFile(encoding, string)\r | |
62 | try:\r | |
63 | BtUni.UniFileClassObject([path])\r | |
64 | if shouldPass:\r | |
65 | return\r | |
66 | except UnicodeError:\r | |
67 | if not shouldPass:\r | |
68 | return\r | |
69 | else:\r | |
70 | self.UnicodeErrorFailure(encoding, shouldPass)\r | |
71 | except EdkLogger.FatalError:\r | |
72 | if not shouldPass:\r | |
73 | return\r | |
74 | else:\r | |
75 | self.EdkErrorFailure(encoding, shouldPass)\r | |
76 | except Exception:\r | |
77 | pass\r | |
78 | \r | |
79 | self.EdkErrorFailure(encoding, shouldPass)\r | |
80 | \r | |
81 | def testUtf16InUniFile(self):\r | |
82 | self.CheckFile('utf_16', shouldPass=True)\r | |
83 | \r | |
84 | TheTestSuite = TestTools.MakeTheTestSuite(locals())\r | |
85 | \r | |
86 | if __name__ == '__main__':\r | |
87 | allTests = TheTestSuite()\r | |
88 | unittest.TextTestRunner().run(allTests)\r |