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 def testValidUtf8File(self
):
118 self
.CheckFile(encoding
='utf_8', shouldPass
=True)
120 def testValidUtf8FileWithBom(self
):
122 # Same test as testValidUtf8File, but add the UTF-8 BOM
124 data
= codecs
.BOM_UTF8
+ codecs
.encode(self
.SampleData
, 'utf_8')
126 self
.CheckFile(encoding
=None, shouldPass
=True, string
=data
)
128 def test32bitUnicodeCharInUtf8File(self
):
130 #langdef en-US "English"
131 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
134 self
.CheckFile('utf_16', shouldPass
=False, string
=data
)
136 def test32bitUnicodeCharInUtf8File(self
):
138 #langdef en-US "English"
139 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
142 self
.CheckFile('utf_8', shouldPass
=False, string
=data
)
144 def test32bitUnicodeCharInUtf8Comment(self
):
146 // Even in comments, we reject non-UCS-2 chars: \U00010300
147 #langdef en-US "English"
148 #string STR_A #language en-US "A"
151 self
.CheckFile('utf_8', shouldPass
=False, string
=data
)
153 def testSurrogatePairUnicodeCharInUtf8File(self
):
155 # Surrogate Pair code points are used in UTF-16 files to
156 # encode the Supplementary Plane characters. In UTF-8, it is
157 # trivial to encode these code points, but they are not valid
158 # code points for characters, since they are reserved for the
159 # UTF-16 Surrogate Pairs.
161 # This test makes sure that BaseTools rejects these characters
162 # if seen in a .uni file.
164 data
= '\xed\xa0\x81'
166 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
168 def testSurrogatePairUnicodeCharInUtf8FileWithBom(self
):
170 # Same test as testSurrogatePairUnicodeCharInUtf8File, but add
173 data
= codecs
.BOM_UTF8
+ '\xed\xa0\x81'
175 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
177 TheTestSuite
= TestTools
.MakeTheTestSuite(locals())
179 if __name__
== '__main__':
180 allTests
= TheTestSuite()
181 unittest
.TextTestRunner().run(allTests
)