Faction

出自 ArchWiki

Faction 是一个用于测试驱动软件开发的 C 语言库。

安装

安装 libfactionAUR 软件包。

用法

该库提供了几个 C 宏,以加快编写测试的速度。

  • FI 代表 Faction 初始化
  • FT 表示 Faction 测试
  • FC 代表 Faction 关闭

使用 FT 宏,需要三个字段。

  • AUTHORS() 接受以逗号分隔的双引号括起来的作者姓名列表
  • SPEC() 接受单个双引号括起来的测试规范描述
  • 一个 C 布尔表达式(就像使用 C assert 宏时一样)

约定规定 Faction 测试应写在包含要测试代码的源文件底部。测试应被 FACTION 宏保护(见下例)包围,以便在编译时启用/禁用它们。C 编译器(如 GNU C 编译器 (GCC))提供了一种在命令行上启用宏的方法(即 -D 标志)

示例

/* This is the function to be tested */
int
increment(int input)
{
   return (input + 1);
}

#ifdef FACTION
#include <faction.h>
#include <limits.h>
FI

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 1 when given 0" ),
    increment(0) == 1
  );

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 0 when given the largest integer value" ),
    increment(INT_MAX) == 0
  );

FC
#endif

这可以使用 gcc filename.c -D FACTION 编译

模式

Faction 可以编译为两种模式:minimal 模式和 extended 模式。

上面的示例以 minimal 模式编译 Faction。minimal 编译只有三个库依赖项:stdlib、stdio 和 getopt。extended 编译有额外的依赖项,包括一些仅通过 GNU 功能测试宏可用的函数。

因此,要在 extended 模式下编译,只需在文件顶部定义 GNU 功能测试宏。例如,修改为在 extended 模式下编译的先前示例将如下所示

#ifdef FACTION
#define _GNU_SOURCE
#endif
/* This is the function to be tested */
increment(int input)
{
  return (input + 1);
}

#ifdef FACTION
#include <faction.h>
#include <limits.h>
FI

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 1 when given 0" ),
    increment(0) == 1
  );

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 0 when given the largest integer value" ),
    increment(INT_MAX) == 0
  );

FC
#endif

扩展模式特性

在扩展模式下,

  • 可以使用运行时 -l 标志选择性地将输出镜像到用户指定的日志文件。
  • 结果表将动态调整大小以适应所用终端的宽度。否则,它默认为 78 个字符宽度。

参见