Pex and unicode encoding

本文关键字:encoding unicode and Pex | 更新日期: 2023-09-27 18:14:28

我正在玩Pex,并有一个简单的类。代码是:

public void WriteLine(string line)
{
    Contract.Requires(line != null);
    if (_stream == null)
        _stream = getStream();
    var writer = new StreamWriter(_stream);
    writer.WriteLine(line);
}
private Stream getStream()
{
    return File.Open(Path, FileMode.Append, FileAccess.Write);
}

我创建了以下PexMethod:

[PexMethod(MaxRunsWithoutNewTests = 200)]
public void WriteLine(string line)
{
    var ms = new MemoryStream();
    MFile.BehaveAsNotImplemented();
    MFileStream.BehaveAsNotImplemented();
    MStreamWriter.BehaveAsNotImplemented( );
    MFile.OpenStringFileModeFileAccess = (p, m, a) => new FileStream(p, m);
    MFileStream.ConstructorStringFileMode = (s, p, m) => new StreamWriter(ms);
    MStreamWriter.AllInstances.BaseStreamGet = sw => ms;
    MStreamWriter.ConstructorStream = (sw, s) =>
    {
        ;
    };
    MTextWriter.AllInstances.WriteLineString = (tw, l) =>
    {
        var buf = Encoding.Unicode.GetBytes(line);
        ms.Write(buf, 0, buf.Length);
     };
     var path = "C:'test.txt";
     var target = new FileWriter(path);
     target.WriteLine(line);
     var buffer = ms.ToArray();
     var result = Encoding.Unicode.GetString(buffer);
     PexAssert.AreEqual<string>(line, result);
}

Pex Exploration提出了这个单元测试:

[TestMethod]
[PexGeneratedBy(typeof(FileWriterTest))]
[PexRaisedException(typeof(PexAssertFailedException))]
[HostType("Moles")]
public void WriteLineThrowsPexAssertFailedException25()
{
    this.WriteLine("'udc00");
}

奇怪的是,一旦堆栈离开带有udc00的单元测试并进入参数化测试,参数行就由:' ` `

表示

正如你所看到的,我正在用Unicode做所有的缓冲。当我最终尝试从内存流中读取字符串时,我得到一个奇怪的符号,看起来像一个钻石,里面有一个问号。

我得到的错误是:" peexassertfailedexception " "预期'奇怪的符号',得到' ' '"

有人知道发生了什么事吗?

Pex and unicode encoding

Encoding.Unicode是UTF-16,根本不能表示代理对(U+D800到U+DFFF)。当您尝试时,它们将被替换为"替换字符"字符(U+FFFD)。

令人惊讶的是,. net可以在字符串中存储代理对。因此,当您对该字符进行编码和解码时,它将不再与原始字符串匹配。