public void TestIncompleteSequence(Encoding codec, int charWidth) { Encoding penc = new PythonSurrogateEscapeEncoding(codec); Assert.That(() => penc.GetBytes(_chars), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(3) .And.Property("CharUnknown").EqualTo(_chars[3])); var enc = penc.GetEncoder(); var bytes = new byte[_chars.Length * charWidth]; Assert.That(enc.GetByteCount(_chars, 0, 3, flush: false), Is.EqualTo(3 * charWidth)); Assert.That(() => enc.GetBytes(_chars, 0, 3, bytes, 0, flush: false), Throws.Nothing); Assert.That(() => enc.GetBytes(_chars, 3, 2, bytes, 3 * charWidth, flush: false), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(0) .And.Property("CharUnknown").EqualTo(_chars[3])); enc.Reset(); Assert.That(enc.GetByteCount(_chars, 0, 4, flush: false), Is.EqualTo(3 * charWidth)); Assert.That(() => enc.GetBytes(_chars, 0, 4, bytes, 0, flush: false), Throws.Nothing); Assert.That(() => enc.GetByteCount(_chars, 4, 1, flush: false), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(-1) // last char from previous increment .And.Property("CharUnknown").EqualTo(_chars[3])); }
public void TestAsciiByte(Encoding codec, int charWidth) { Encoding penc = new PythonSurrogateEscapeEncoding(codec); Assert.That <byte[]>(() => penc.GetBytes(_chars), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(3) .And.Property("CharUnknown").EqualTo(_chars[3])); var enc = penc.GetEncoder(); var bytes = new byte[_chars.Length * charWidth]; Assert.That(enc.GetByteCount(_chars, 0, 3, flush: false), Is.EqualTo(3 * charWidth)); Assert.That <int>(() => enc.GetBytes(_chars, 0, 3, bytes, 0, flush: false), Throws.Nothing); Assert.That <int>(() => enc.GetBytes(_chars, 3, 2, bytes, 3 * charWidth, flush: false), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(0) .And.Property("CharUnknown").EqualTo(_chars[3])); enc.Reset(); Assert.That <int>(() => enc.GetBytes(_chars, 0, 5, bytes, 3 * charWidth, flush: false), Throws.TypeOf <EncoderFallbackException>() .With.Property("Index").EqualTo(3) .And.Property("CharUnknown").EqualTo(_chars[3])); }