2 # Unit tests for AutoGen.UniClassObject
4 # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
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
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.
25 from Common
.Misc
import PathClass
26 import AutoGen
.UniClassObject
as BtUni
28 from Common
import EdkLogger
29 EdkLogger
.InitializeForUnitTest()
31 class Tests(TestTools
.BaseToolsTest
):
34 #langdef en-US "English"
35 #string STR_A #language en-US "STR_A for en-US"
38 def EncodeToFile(self
, encoding
, string
=None):
40 string
= self
.SampleData
41 if encoding
is not None:
42 data
= codecs
.encode(string
, encoding
)
46 self
.WriteTmpFile(path
, data
)
47 return PathClass(self
.GetTmpFilePath(path
))
49 def ErrorFailure(self
, error
, encoding
, shouldPass
):
50 msg
= error
+ ' should '
53 msg
+= 'be generated for '
54 msg
+= '%s data in a .uni file' % encoding
57 def UnicodeErrorFailure(self
, encoding
, shouldPass
):
58 self
.ErrorFailure('UnicodeError', encoding
, shouldPass
)
60 def EdkErrorFailure(self
, encoding
, shouldPass
):
61 self
.ErrorFailure('EdkLogger.FatalError', encoding
, shouldPass
)
63 def CheckFile(self
, encoding
, shouldPass
, string
=None):
64 path
= self
.EncodeToFile(encoding
, string
)
66 BtUni
.UniFileClassObject([path
])
73 self
.UnicodeErrorFailure(encoding
, shouldPass
)
74 except EdkLogger
.FatalError
:
78 self
.EdkErrorFailure(encoding
, shouldPass
)
82 self
.EdkErrorFailure(encoding
, shouldPass
)
84 def testUtf16InUniFile(self
):
85 self
.CheckFile('utf_16', shouldPass
=True)
87 def testSupplementaryPlaneUnicodeCharInUtf16File(self
):
89 # Supplementary Plane characters can exist in UTF-16 files,
90 # but they are not valid UCS-2 characters.
92 # This test makes sure that BaseTools rejects these characters
93 # if seen in a .uni file.
96 #langdef en-US "English"
97 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
100 self
.CheckFile('utf_16', shouldPass
=False, string
=data
)
102 def testSurrogatePairUnicodeCharInUtf16File(self
):
104 # Surrogate Pair code points are used in UTF-16 files to
105 # encode the Supplementary Plane characters. But, a Surrogate
106 # Pair code point which is not followed by another Surrogate
107 # Pair code point might be interpreted as a single code point
108 # with the Surrogate Pair code point.
110 # This test makes sure that BaseTools rejects these characters
111 # if seen in a .uni file.
113 data
= codecs
.BOM_UTF16_LE
+ '//\x01\xd8 '
115 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
117 TheTestSuite
= TestTools
.MakeTheTestSuite(locals())
119 if __name__
== '__main__':
120 allTests
= TheTestSuite()
121 unittest
.TextTestRunner().run(allTests
)