如何使用正则表达式在 SimpleTest 中编写 AssertTags 测试?

发布于 2024-07-06 06:43:38 字数 353 浏览 4 评论 0原文

我希望测试一个生成 lorem ipsum 文本的函数,但它是在 html 标签内执行的。 所以我无法提前知道文本内容,但我知道html结构。 这就是我想测试的。 也许文本的长度在一定的限制之内。 所以我想知道的是,assertTags 是否可以通过以下方式做到这一点:

Result = "<p>Some text</p>";
Expected = array( 
   '<p' ,
   'regex',
   '/p'
);
assertTags(resutl, expected)

我正在使用 SimpleTest 和 CakePHP,但我认为这应该是一个普遍问题。

I wish to test a function that will generate lorem ipsum text, but it does so within html tags. So I cant know in advance the textual content, but i know the html structure. That is what I want to test. And maybe that the length of the texts are within certain limits.
So what I am wondering is if the assertTags can do this in a way paraphrased bellow:

Result = "<p>Some text</p>";
Expected = array( 
   '<p' ,
   'regex',
   '/p'
);
assertTags(resutl, expected)

I am using SimpleTest with CakePHP, but I think it should be a general question.

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

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

发布评论

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

评论(2

多彩岁月 2024-07-13 06:43:38
$expected = array(
    '<p',
    'preg:/[A-Za-z\.\s\,]+/',
    '/p'
);
$expected = array(
    '<p',
    'preg:/[A-Za-z\.\s\,]+/',
    '/p'
);
相思碎 2024-07-13 06:43:38

扩展 SimpleExpectation 类,然后在断言语句中使用新的 Expectation 类,

请参阅: http://www .lastcraft.com/expectation_documentation.php#extending

给出的示例用于验证 IP 地址,但应该适用于您的问题:

class ValidIp extends SimpleExpectation {

  function test($ip) {
    return (ip2long($ip) != -1);
  }

  function testMessage($ip) {
    return "Address [$ip] should be a valid IP address";
  }
}

然后在您的测试中

$this->assert(new ValidIp(),$server->getIp());

Extend the SimpleExpectation class and then use your new Expectation class in the assert statement

see: http://www.lastcraft.com/expectation_documentation.php#extending

the example given is for validating an IP address but should be applicable to your problem:

class ValidIp extends SimpleExpectation {

  function test($ip) {
    return (ip2long($ip) != -1);
  }

  function testMessage($ip) {
    return "Address [$ip] should be a valid IP address";
  }
}

then in your test

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