使用 NUnit 和 Console 进行单元测试

发布于 2024-11-25 18:26:42 字数 713 浏览 3 评论 0原文

我一直在使用curses Sharp 库(pdcurses 的ac# 包装器),编写一些单元测试代码来获取api 的句柄及其工作原理,并且我提出了一个问题。

我可以使用以下代码从 DLL 中运行curses Sharp(以便 nUnit 可以测试它):

        bool consoleAllocated = AllocConsole();
        if (!consoleAllocated)
            throw new Exception("Unable to allocate a new console.");

        Curses.InitScr();

        Stdscr.Add(4, 6, "This is a test title");

        Curses.EndWin();

        FreeConsole();

AllocConsole 和 FreeConsole 是从 kernel32 导入的 extern。

我想做的是能够将控制台输出从位置 4,6 读取到字符串,以便以编程方式检查我输入的字符串是否已正确输出。例如,为了使用 TDD 创建一个 Curses 风格的应用程序,能够进行这样的检查是非常重要的。

我已经查看了 Curses 和 Stdscr 对象(都是 Curses Sharp 对象)以及 Console 对象(来自 Windows 库),但还没有找到方法。有人有什么想法吗?

I've been playing around with the curses sharp library (a c# wrapper for pdcurses), writing some unit test code to get a handle on the api and how it works, and I've come up with a question.

I can run curses sharp from within a DLL (so that nUnit can test it), using the following code:

        bool consoleAllocated = AllocConsole();
        if (!consoleAllocated)
            throw new Exception("Unable to allocate a new console.");

        Curses.InitScr();

        Stdscr.Add(4, 6, "This is a test title");

        Curses.EndWin();

        FreeConsole();

AllocConsole and FreeConsole are extern's imported from kernel32.

What I would like to do is be able to read the console output from position 4,6 to a string so as to programmatically check that the string I've entered has been output correctly. It would be pretty important to be able to do checks like this in order to create a curses-style app using TDD for instance.

I've looked over the Curses and Stdscr objects (both Curses Sharp objects), and the Console object (from the windows library) and haven't been able to find a way yet. Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-12-02 18:26:42

我设法找到了答案,如果有人感兴趣,我已经包含了下面的代码。它很乱,因为我还没有清理它,但它应该作为如何做到这一点的示例。

感谢 pinvoke.net 提供的优秀签名合集。

    [DllImport("kernel32", SetLastError = true)]
    static extern bool AllocConsole();

    [DllImport("kernel32", SetLastError = true)]
    static extern bool FreeConsole();

    [DllImport("kernel32", SetLastError = true)]
    static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32", SetLastError = true)]
    static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput,
        [Out]StringBuilder lpCharacter, uint nLength, COORD dwReadCoord,
        out uint lpNumberOfCharsRead);

    const int STD_OUTPUT_HANDLE = -11;

    [StructLayout(LayoutKind.Sequential)]
    struct COORD
    {
        public short X;
        public short Y;
    }

    [Test]
    public void WriteTitle()
    {
        bool consoleAllocated = AllocConsole();
        if (!consoleAllocated)
            throw new Exception("Unable to allocate a new console.");

        Curses.InitScr();

        Stdscr.Add(4, 6, "This is a test title");
        Stdscr.Refresh();

        IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        uint length = 20;
        StringBuilder consoleOutput = new StringBuilder((int)length);
        COORD readCoord;
        readCoord.X = 6;
        readCoord.Y = 4;
        uint numOfCharsRead = 0;

        ReadConsoleOutputCharacter(stdOut, consoleOutput, length, readCoord, out numOfCharsRead);

        string outputString = consoleOutput.ToString();
        Assert.That(outputString, Is.EqualTo("This is a test title"));

        Curses.EndWin();

        FreeConsole();
    }

I managed to find an answer, in case anyone was interested I've included the code below. It's messy, as I haven't cleaned it up yet, but it should serve as an example as to how to do this.

Thanks to pinvoke.net for their excellent signature collection.

    [DllImport("kernel32", SetLastError = true)]
    static extern bool AllocConsole();

    [DllImport("kernel32", SetLastError = true)]
    static extern bool FreeConsole();

    [DllImport("kernel32", SetLastError = true)]
    static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32", SetLastError = true)]
    static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput,
        [Out]StringBuilder lpCharacter, uint nLength, COORD dwReadCoord,
        out uint lpNumberOfCharsRead);

    const int STD_OUTPUT_HANDLE = -11;

    [StructLayout(LayoutKind.Sequential)]
    struct COORD
    {
        public short X;
        public short Y;
    }

    [Test]
    public void WriteTitle()
    {
        bool consoleAllocated = AllocConsole();
        if (!consoleAllocated)
            throw new Exception("Unable to allocate a new console.");

        Curses.InitScr();

        Stdscr.Add(4, 6, "This is a test title");
        Stdscr.Refresh();

        IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        uint length = 20;
        StringBuilder consoleOutput = new StringBuilder((int)length);
        COORD readCoord;
        readCoord.X = 6;
        readCoord.Y = 4;
        uint numOfCharsRead = 0;

        ReadConsoleOutputCharacter(stdOut, consoleOutput, length, readCoord, out numOfCharsRead);

        string outputString = consoleOutput.ToString();
        Assert.That(outputString, Is.EqualTo("This is a test title"));

        Curses.EndWin();

        FreeConsole();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文