我如何写一个使用标准输入输入另外一个功能测试功能? [英] How do I write a testing function for another function that uses stdin input?

查看:203
本文介绍了我如何写一个使用标准输入输入另外一个功能测试功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能作为学院任务的一部分:

I have the following functions as part of a college assignment:

int readMenuOption()
{
   /* local declarations */
   char option[2];
   /* read in 1 char from stdin plus 1 char for string termination character */
   readStdin(1 + 1, option);
   return (int)option[0] <= ASCII_OFFSET ? 0 : (int)option[0] - ASCII_OFFSET;
}

int readStdin(int limit, char *buffer) 
{
   char c;
   int i = 0;
   int read = FALSE;
   while ((c = fgetc(stdin)) != '\n') {
      /* if the input string buffer has already reached it maximum
       limit, then abandon any other excess characters. */
      if (i <= limit) {
         *(buffer + i) = c;
         i++;
         read = TRUE;
      }
   }
   /* clear the remaining elements of the input buffer with a null character. */
   for (i = i; i < strlen(buffer); i++) {
      *(buffer + i) = '\0';
   }
   return read;
}

它完美地为我需要做的(以输入通过键盘)。我已经使用标准输入(比如我),因为一些我的教授列出的要求去做。

It works perfectly for what I need it to do (take in input from keyboard). I've had to do it using stdin (like I have) because of a number of requirements outlined by my professor.

我想写一系列的单元测试的任务,但我不知道如何让我的测试功能调用 readMenuOption(),并通过输入它(而不必在运行时做到这一点)。

I want to write a series of "unit tests" for the assignment, but I dont know how to get my testing functions to call readMenuOption() and pass in input to it (without having to do it at run-time).

这是可能的,如果是这样,我该怎么办呢? (即,是否有可能写入标准输入)?

Is this possible, and if so, how can I do it? (i.e., is it possible to write to stdin)?

推荐答案

有一件事你的 的可以做的是简单地修改 readStdin 允许它无论是从真正的标准输入获取数据,或从一个辅助功能,是这样的:

One thing you could do is to simply modify readStdin to allow it to either get data from the real standard input, or from a helper function, something like:

char *fakeStdIn = "";
int myfgetc (FILE *fin) {
    if (*fakeStdIn == '\0')
        return fgetc (fin);
    return *fakeStdIn++;
}

int readStdin(int limit, char *buffer) 
{
   char c;
   int i = 0;
   int read = FALSE;
   while ((c = myfgetc(stdin)) != '\n') {
      /* if the input string buffer has already reached it maximum
       limit, then abandon any other excess characters. */
      if (i <= limit) {
         *(buffer + i) = c;
         i++;
         read = TRUE;
      }
   }
   /* clear the remaining elements of the input buffer with a null character. */
   for (i = i; i < strlen(buffer); i++) {
      *(buffer + i) = '\0';
   }
   return read;
}

然后,把它从你的单元测试打电话,你可以这样做:

Then, to call it from your unit test, you can do:

fakeStdIn = "1\npaxdiablo\nnice guy\n";
// Call your top-level input functions like  readMenuOption().

通过将钩子在较低的水平,你可以注入自己的字符序列,而不是使用标准输入。而且,如果在任何时候,假的标准输入被耗尽,还原为真实的。

By putting a hook at the lower levels, you can inject your own character sequence instead of using standard input. And if, at any point, the fake standard input is exhausted, it reverts to the real one.

显然,这是用字如此,如果要注入EOF事件,你需要的整数,而不是一个数组,但是这将是一个小的修改方案。

Obviously, this is using characters so, if you want to inject EOF events, you'll need an array of integers instead, but that would be a minor modification to the scheme.

这篇关于我如何写一个使用标准输入输入另外一个功能测试功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆