MCPcopy Index your code
hub / github.com/python/cpython / test_basics

Method test_basics

Lib/test/test_codecs.py:2158–2236  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2156
2157class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
2158 def test_basics(self):
2159 s = "abc123" # all codecs should be able to encode these
2160 for encoding in all_unicode_encodings:
2161 name = codecs.lookup(encoding).name
2162 if encoding.endswith("_codec"):
2163 name += "_codec"
2164 elif encoding == "latin_1":
2165 name = "latin_1"
2166 # Skip the mbcs alias on Windows
2167 if name != "mbcs":
2168 self.assertEqual(encoding.replace("_", "-"),
2169 name.replace("_", "-"))
2170
2171 (b, size) = codecs.getencoder(encoding)(s)
2172 self.assertEqual(size, len(s), "encoding=%r" % encoding)
2173 (chars, size) = codecs.getdecoder(encoding)(b)
2174 self.assertEqual(chars, s, "encoding=%r" % encoding)
2175
2176 if encoding not in broken_unicode_with_stateful:
2177 # check stream reader/writer
2178 q = Queue(b"")
2179 writer = codecs.getwriter(encoding)(q)
2180 encodedresult = b""
2181 for c in s:
2182 writer.write(c)
2183 chunk = q.read()
2184 self.assertTrue(type(chunk) is bytes, type(chunk))
2185 encodedresult += chunk
2186 q = Queue(b"")
2187 reader = codecs.getreader(encoding)(q)
2188 decodedresult = ""
2189 for c in encodedresult:
2190 q.write(bytes([c]))
2191 decodedresult += reader.read()
2192 self.assertEqual(decodedresult, s, "encoding=%r" % encoding)
2193
2194 if encoding not in broken_unicode_with_stateful:
2195 # check incremental decoder/encoder and iterencode()/iterdecode()
2196 try:
2197 encoder = codecs.getincrementalencoder(encoding)()
2198 except LookupError: # no IncrementalEncoder
2199 pass
2200 else:
2201 # check incremental decoder/encoder
2202 encodedresult = b""
2203 for c in s:
2204 encodedresult += encoder.encode(c)
2205 encodedresult += encoder.encode("", True)
2206 decoder = codecs.getincrementaldecoder(encoding)()
2207 decodedresult = ""
2208 for c in encodedresult:
2209 decodedresult += decoder.decode(bytes([c]))
2210 decodedresult += decoder.decode(b"", True)
2211 self.assertEqual(decodedresult, s,
2212 "encoding=%r" % encoding)
2213
2214 # check iterencode()/iterdecode()
2215 result = "".join(codecs.iterdecode(

Callers

nothing calls this directly

Calls 13

writeMethod · 0.95
readMethod · 0.95
assertTrueMethod · 0.80
getreaderMethod · 0.80
iterencodeMethod · 0.80
QueueClass · 0.70
lookupMethod · 0.45
endswithMethod · 0.45
assertEqualMethod · 0.45
replaceMethod · 0.45
encodeMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected